Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
                }
            }
        }
Ejemplo n.º 3
0
        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
            };
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 6
0
        private BstNode <int> FindMin(BstNode <int> node)
        {
            while (node.Left != null)
            {
                node = node.Left;
            }

            return(node);
        }
Ejemplo n.º 7
0
 private void InorderTraversal(BstNode <int> node)
 {
     if (node != null)
     {
         InorderTraversal(node.Left);
         Console.WriteLine($"{node.Data}");
         InorderTraversal(node.Right);
     }
 }
Ejemplo n.º 8
0
 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);
 }
Ejemplo n.º 9
0
        private BstNode <int> FindMaxRecursive(BstNode <int> node)
        {
            if (node == null)
            {
                return(null);
            }

            var result = FindMaxRecursive(node.Right);

            return(result ?? node);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        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);
                    }
                }
            }
        }
Ejemplo n.º 14
0
        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;
        }
Ejemplo n.º 15
0
        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}");
        }
Ejemplo n.º 16
0
        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);
        }
Ejemplo n.º 17
0
        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);
        }
Ejemplo n.º 18
0
        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);
        }
Ejemplo n.º 19
0
        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);
            }
        }
Ejemplo n.º 20
0
        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);
        }
Ejemplo n.º 21
0
        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);
        }
Ejemplo n.º 22
0
        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);
        }
Ejemplo n.º 23
0
        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
            };
        }
Ejemplo n.º 25
0
 private void CalculateHeight(BstNode <int> node)
 {
     Console.WriteLine($"Node height is: {CalculateNodeHeight(node)}");
 }