private void ConvertBstToDll(BstNode <int> current) { if (current == null) { return; } ConvertBstToDll(current.Left); if (previous == null) { head = current; } else { previous.Right = current; current.Left = previous; } // Below three lines are to make the Doubly Linked List Circular. var right = current.Right; head.Left = current; current.Right = head; previous = current; ConvertBstToDll(right); }
private void BreadthFirstSearch(BstNode <int> node) { Queue <BstNode <int> > bstNodeStack = new Queue <BstNode <int> >(); if (node != null) { bstNodeStack.Enqueue(node); Console.WriteLine("Breadth first search traversal......"); } while (bstNodeStack.Count > 0) { var currentNode = bstNodeStack.Dequeue(); Console.WriteLine($"{currentNode.Data} "); if (currentNode.Left != null) { bstNodeStack.Enqueue(currentNode.Left); } if (currentNode.Right != null) { bstNodeStack.Enqueue(currentNode.Right); } } }
private void Initialize() { this.root = new BstNode <int> { Data = 20 }; this.root.Left = new BstNode <int> { Data = 10 }; this.root.Right = new BstNode <int> { Data = 30 }; //this.root.Left.Left = new Node<int> { Data = 5 }; this.root.Left.Right = new BstNode <int> { Data = 15 }; this.root.Right.Left = new BstNode <int> { Data = 25 }; //this.root.Right.Right = new Node<int> { Data = 35 }; this.root.Right.Left.Right = new BstNode <int> { Data = 27 }; this.root.Right.Left.Right.Right = new BstNode <int> { Data = 28 }; }
private void ConstructBst() { BstNode <int> root = BuildBst(this.sortedItems, 0, this.sortedItems.Length - 1); Console.WriteLine("Built Binary Search Tree"); InorderTraversal(root); }
private void PrepareAndPrintCousins() { // Select the node for which cousins have to printed. BstNode <int> node = this.root.Left.Left; int nodeLevel = this.CalculateLevel(this.root, node, 1); PrintAllCousins(this.root, node, 1, nodeLevel); }
private BstNode <int> FindMin(BstNode <int> node) { while (node.Left != null) { node = node.Left; } return(node); }
private void InorderTraversal(BstNode <int> node) { if (node != null) { InorderTraversal(node.Left); Console.WriteLine($"{node.Data}"); InorderTraversal(node.Right); } }
public void Process() { this.root = Insert(this.root, 20); this.root = Insert(this.root, 10); this.root = Insert(this.root, 15); this.root = Insert(this.root, 25); this.root = Insert(this.root, 5); this.root = Insert(this.root, 30); this.root = Insert(this.root, 35); }
private BstNode <int> FindMaxRecursive(BstNode <int> node) { if (node == null) { return(null); } var result = FindMaxRecursive(node.Right); return(result ?? node); }
private void FindMinMax(BstNode <int> node) { var min = FindMinRecursive(node); var max = FindMaxRecursive(node); Console.WriteLine($"Min value: {min.Data}"); Console.WriteLine($"Max value: {max.Data}"); FindMinIterative(node); FindMaxIterative(node); }
private BstNode <int> FindMinRecursive(BstNode <int> node) { if (node == null) { return(null); } //var result = FindMin((node.Left != null) ? node.Left : node.Right); var result = FindMinRecursive(node.Left); return(result ?? node); }
private int CalculateNodeHeight1(BstNode <int> node) { if (node == null) { return(-1); } var leftHeight = CalculateNodeHeight(node.Left); var rightHeight = CalculateNodeHeight(node.Right); return(Math.Max(leftHeight, rightHeight) + 1); }
private void SpiralPrinting(BstNode <int> node) { if (node == null) { return; } Stack <BstNode <int> > leftToRightStack = new Stack <BstNode <int> >(); Stack <BstNode <int> > rightToLeftStack = new Stack <BstNode <int> >(); leftToRightStack.Push(node); while (leftToRightStack.Count > 0 || rightToLeftStack.Count > 0) { Console.WriteLine(); while (leftToRightStack.Count > 0) { var current = leftToRightStack.Pop(); Console.Write($"{current.Data} "); if (current.Left != null) { rightToLeftStack.Push(current.Left); } if (current.Right != null) { rightToLeftStack.Push(current.Right); } } Console.WriteLine(); while (rightToLeftStack.Count > 0) { var current = rightToLeftStack.Pop(); Console.Write($"{current.Data} "); if (current.Right != null) { leftToRightStack.Push(current.Right); } if (current.Left != null) { leftToRightStack.Push(current.Left); } } } }
private int CalculateNodeHeight(BstNode <int> node) { if (node == null) { return(-1); } var leftHeight = CalculateNodeHeight(node.Left) + 1; var rightHeight = CalculateNodeHeight(node.Right) + 1; return(Math.Max(leftHeight, rightHeight)); //return (leftHeight > rightHeight) ? leftHeight : rightHeight; }
private void FindMinIterative(BstNode <int> node) { if (node == null) { Console.WriteLine("Invalid tree"); } while (node.Left != null) { node = node.Left; } Console.WriteLine($"Min values is {node.Data}"); }
private BstNode <int> Delete(BstNode <int> node, int data) { if (node == null) { return(node); } else if (node.Data > data) { node.Left = Delete(node.Left, data); //return node; } else if (node.Data < data) { node.Right = Delete(node.Right, data); //return node; } else { // (node.Data == data) // case 1: No child if (node.Left == null && node.Right == null) { node = null; //return node; } // Case 2: Single child else if (node.Left == null) { node = node.Right; //return node; } else if (node.Right == null) { node = node.Left; //return node; } // Case 3: Two children else { var minRightNode = FindMin(node.Right); // Find the minimum value from right. Max value from left also will work. node.Data = minRightNode.Data; // Copy the value so that the hierarchy is maintained. node.Right = Delete(node.Right, node.Data); // Delete the duplicate node - this will end up in Case 1 or Case 2. //return node; } } return(node); }
private BstNode <int> BuildBst(int[] sortedArrary, int start, int end) { if (start > end) { return(null); } int mid = (end + start) / 2; BstNode <int> bstNode = new BstNode <int> { Data = sortedArrary[mid] }; bstNode.Left = BuildBst(sortedArrary, start, mid - 1); bstNode.Right = BuildBst(sortedArrary, mid + 1, end); return(bstNode); }
private void PrintAllCousins(BstNode <int> currentRoot, BstNode <int> node, int currentRootLevel, int nodeLevel) { if (currentRoot == null) { return; } if (currentRootLevel == nodeLevel - 1) { if (!((currentRoot.Left != null && currentRoot.Left == node) || (currentRoot.Right != null && currentRoot.Right == node))) { Console.Write($"{currentRoot.Left.Data} "); Console.Write($"{currentRoot.Right.Data} "); } } PrintAllCousins(currentRoot.Left, node, currentRootLevel + 1, nodeLevel); PrintAllCousins(currentRoot.Right, node, currentRootLevel + 1, nodeLevel); }
private bool IsBst1(BstNode <int> node, int minValue, int maxValue) { if (node == null) { return(true); } if (node.Data > minValue && node.Data < maxValue && IsBst1(node.Left, minValue, node.Data) && IsBst1(node.Right, node.Data, maxValue)) { return(true); } else { return(false); } }
private BstNode <int> LowestCommonAncestorRecursive(BstNode <int> node, int value1, int value2) { if (node == null) { return(null); } if (node.Data > value1 && node.Data > value2) { return(LowestCommonAncestorRecursive(node.Left, value1, value2)); } if (node.Data < value1 && node.Data < value2) { return(LowestCommonAncestorRecursive(node.Right, value1, value2)); } return(node); }
private BstNode <int> LowestCommonAncestorIterative(BstNode <int> node, int value1, int value2) { while (node != null) { if (node.Data > value1 && node.Data > value2) { node = node.Left; } else if (node.Data < value1 && node.Data < value2) { node = node.Right; } else { break; } } return(node); }
private BstNode <int> Insert(BstNode <int> node, int data) { if (node == null) { node = new BstNode <int> { Data = data }; return(node); } else if (data < node.Data) { node.Left = Insert(node.Left, data); } else { node.Right = Insert(node.Right, data); } return(node); }
private int CalculateLevel(BstNode <int> currentRoot, BstNode <int> node, int currentLevel) { if (currentRoot == null) { return(0); } if (currentRoot == node) { return(currentLevel); } int leftTraveralLevel = CalculateLevel(currentRoot.Left, node, currentLevel + 1); if (leftTraveralLevel > 0) { return(leftTraveralLevel); } return(CalculateLevel(root.Right, node, currentLevel + 1)); }
private void Initialize() { this.root = new BstNode <int> { Data = 1 }; this.root.Left = new BstNode <int> { Data = 2 }; this.root.Right = new BstNode <int> { Data = 3 }; this.root.Left.Left = new BstNode <int> { Data = 4 }; this.root.Left.Right = new BstNode <int> { Data = 5 }; this.root.Right.Left = new BstNode <int> { Data = 6 }; this.root.Right.Right = new BstNode <int> { Data = 7 }; this.root.Left.Left.Left = new BstNode <int> { Data = 8 }; this.root.Left.Right.Left = new BstNode <int> { Data = 9 }; this.root.Right.Right.Right = new BstNode <int> { Data = 10 }; }
private void CalculateHeight(BstNode <int> node) { Console.WriteLine($"Node height is: {CalculateNodeHeight(node)}"); }