public void LineByLineLevelOrderTraversal(Bnode <int> head)
        {
            var q = new System.Collections.Generic.Queue <Bnode <int> >();

            q.Enqueue(head);
            int curlevel  = 1;
            int nextlevel = 0;

            while (q.Count > 0)
            {
                var node = q.Dequeue();
                curlevel--;

                if (node.left != null)
                {
                    q.Enqueue(node.left);
                    nextlevel++;
                }
                if (node.right != null)
                {
                    q.Enqueue(node.right);
                    nextlevel++;
                }

                if (curlevel == 0)
                {
                    curlevel = nextlevel;
                    curlevel = 0;
                    Console.Write("\n");
                }
            }
        }
Beispiel #2
0
 private void PushAll(Bnode <int> node)
 {
     while (node != null)
     {
         stack.Push(node);
         node = node.left;
     }
 }
Beispiel #3
0
        public int KDistanceNodes(Bnode <int> node, Bnode <int> target, int k)
        {
            if (node == null)
            {
                return(-1);
            }

            if (node == target)
            {
                PrintKDistanceNodeDownTree(target, 0, k);
                return(1);
            }

            var leftHeight = KDistanceNodes(node.left, target, k);

            if (leftHeight != -1)
            {
                if (leftHeight == k)
                {
                    Console.WriteLine(node.data);
                }
                else
                {
                    KDistanceNodes(node.right, target, k - leftHeight - 1);// since you recurs from right node on backtrack so -1
                }

                return(leftHeight + 1);
            }


            var rightHeight = KDistanceNodes(node.right, target, k);

            if (rightHeight != -1)
            {
                if (rightHeight == k)
                {
                    Console.WriteLine(node.data);
                }
                else
                {
                    KDistanceNodes(node.right, target, k - rightHeight - 1);// since you recurse from left node on backtrack so -1
                }

                return(rightHeight + 1);
            }

            //if none of the branch find target node.
            return(-1);
        }
Beispiel #4
0
        private void PrintKDistanceNodeDownTree(Bnode <int> node, int level, int k)
        {
            if (node == null || level > k)
            {
                return;
            }

            if (level == k)
            {
                Console.Write(node.data);
            }

            PrintKDistanceNodeDownTree(node.right, level + 1, k);
            PrintKDistanceNodeDownTree(node.left, level + 1, k);
        }
Beispiel #5
0
        public void PrintZigZag(Bnode <int> root)
        {
            var s1 = new Stack <Bnode <int> >();
            var s2 = new Stack <Bnode <int> >();

            s1.Push(root);

            while (s1.Count > 0 || s2.Count > 0)
            {
                while (s1.Count > 0)
                {
                    var node = s1.Pop();
                    Console.Write(node.data);

                    //right push befor left
                    if (node.right != null)
                    {
                        s2.Push(node.right);
                    }
                    if (node.left != null)
                    {
                        s2.Push(node.left);
                    }
                }

                //next line print
                Console.WriteLine();

                while (s2.Count > 0)
                {
                    var node = s2.Pop();
                    Console.Write(node.data);

                    //left push befor right
                    if (node.left != null)
                    {
                        s1.Push(node.left);
                    }
                    if (node.right != null)
                    {
                        s1.Push(node.right);
                    }
                }

                Console.WriteLine();
            }
        }
        public Bnode <int> GetNextRightNode(Bnode <int> node, Bnode <int> root)
        {
            var nodeQueue  = new Queue <Bnode <int> >();
            var levelQueue = new Queue <int>();

            nodeQueue.Enqueue(root);
            levelQueue.Enqueue(0);

            while (nodeQueue.Count > 0)
            {
                var curNode  = nodeQueue.Dequeue();
                var curlevel = levelQueue.Dequeue();

                if (curNode == node)
                {
                    if (levelQueue.Count == 0 || levelQueue.Dequeue() == curlevel + 1)
                    {
                        return(null);
                    }

                    return(nodeQueue.Dequeue());
                }

                if (curNode.left != null)
                {
                    nodeQueue.Enqueue(curNode.left);
                    levelQueue.Enqueue(curlevel + 1);
                }

                if (curNode.right != null)
                {
                    nodeQueue.Enqueue(curNode.right);
                    levelQueue.Enqueue(curlevel + 1);
                }
            }

            return(null);
        }
 public Node(Bnode multiNode, int position)
 {
     this.multiNode = multiNode;
     this.position  = position;
 }
Beispiel #8
0
 public BSTIterator(Bnode <int> root)
 {
     PushAll(root);
 }