// Function to find in-order successor in a BST private BstNode PGetSuccessor(BstNode node, int data) { // Search the node O(h) BstNode current = Find(node, data); if (current == null) { return(null); } if (current.Right != null) // Case 1: Node has right sub-tree { return(PFindMin(current.Right)); } else // No right sub-tree { BstNode successor = null; BstNode ancestor = node; while (ancestor != current) { if (current.Data < ancestor.Data) { successor = ancestor; // So far this is the deepest node for which current node is in left ancestor = ancestor.Left; } else { ancestor = ancestor.Right; } } return(successor); } }
private void TravserseBinarySearchTree(BstNode node) { if (node == null) { return; } TravserseBinarySearchTree(node.Left); Numbers.Add(node.Data); TravserseBinarySearchTree(node.Right); }
private void PInOrderPrint(BstNode node) { if (node == null) { return; } PInOrderPrint(node.Left); Console.WriteLine(node.Data); PInOrderPrint(node.Right); }
public int IterativeFindMax() { if (Root == null) { Console.WriteLine("The tree is empty"); return(-1); } BstNode current = Root; // The node with no right-child is the maximum one while (current.Right != null) { current = current.Right; } return(current.Data); }
private BstNode PFindMax(BstNode node) { if (node == null) { return(null); } else if (node.Right == null) { return(node); } else { // Search in the right sub-tree return(PFindMax(node.Right)); } }
private BstNode PFindMin(BstNode node) { if (node == null) { return(null); } else if (node.Left == null) { return(node); } else { // Search in the left sub-tree return(PFindMin(node.Left)); } }
// Insert the data at the specified node, insertion takes place when link is null private BstNode InsertAtNode(BstNode node, int data) { // Insertion only occurs when the pointer is pointing to null if (node == null) { node = new BstNode(data); } // Insert on the left else if (data <= node.Data) { node.Left = InsertAtNode(node.Left, data); } else { node.Right = InsertAtNode(node.Right, data); } return(node); }
private int PFindHeight(BstNode node) { if (node == null) { return(-1); } int leftHeight = PFindHeight(node.Left); int rightHeight = PFindHeight(node.Right); if (leftHeight > rightHeight) { return(leftHeight + 1); } else { return(rightHeight + 1); } }
private BstNode Find(BstNode node, int data) { if (node == null) { return(null); } else if (node.Data == data) { return(node); } else if (data <= node.Data) { return(Find(node.Left, data)); } else { return(Find(node.Right, data)); } }
private bool IsInTree(BstNode node, int data) { if (node == null) { return(false); } else if (node.Data == data) { return(true); } else if (data <= node.Data) { return(IsInTree(node.Left, data)); } else { return(IsInTree(node.Right, data)); } }
private BstNode PDelete(BstNode node, int data) { if (node == null) { node = null; } else if (data < node.Data) // The data to be deleted is located in the left sub-tree { node.Left = PDelete(node.Left, data); } else if (data > node.Data) // The data to be deleted is located in the right sub-tree { node.Right = PDelete(node.Right, data); } else // Wohoo... I found, get ready to be deleted { // Case 1: No child if (node.Left == null && node.Right == null) { node = null; } // Case 2: One right child else if (node.Left == null) { node = node.Right; } // Case 3: One left child else if (node.Right == null) { node = node.Left; } else { BstNode current = PFindMin(node.Right); node.Data = current.Data; node.Right = PDelete(node.Right, current.Data); } } return(node); }
private void PLevelOrderPrint(BstNode node) { if (node == null) { return; } Queue <BstNode> bstNodes = new Queue <BstNode>(); bstNodes.Enqueue(node); // While there is atleast one discovered node while (bstNodes.Count > 0) { BstNode current = bstNodes.Dequeue(); Console.WriteLine(current.Data); if (current.Left != null) { bstNodes.Enqueue(current.Left); } if (current.Right != null) { bstNodes.Enqueue(current.Right); } } }
public void Insert(int data) { Root = InsertAtNode(Root, data); }