示例#1
0
 public void InorderTraversal(BinaryTreeNode root)
 {
     if (root != null)
     {
         InorderTraversal(root.Left);
         Console.WriteLine(root.Key);
         InorderTraversal(root.Right);
     }
 }
示例#2
0
 internal bool Search(BinaryTreeNode root,int data)
 {
     if (root == null)
         return false;
     if (root.data == data)
         return true;
     else if (root.data < data)
         return Search(root.right, data);
     else
         return Search(root.left, data);
 }
示例#3
0
 internal BinaryTreeNode Insert(ref BinaryTreeNode root,BinaryTreeNode newNode)
 {
     if (root == null)
     {
         root = newNode;
     }
     else {
         if (root.data > newNode.data)
            Insert(ref root.left, newNode);
         else
            Insert(ref root.right, newNode);
     }
     return root;
 }
示例#4
0
        public void BinaryTreeInsert(int k)
        {
            BinaryTreeNode node = new BinaryTreeNode(k);
            if (Root == null)
            {
                Root = node;
            }
            else if (Root.Left == null) Root.Left = node;
            else if (Root.Right == null) Root.Right = node;

            else
            {
                Queue<BinaryTreeNode> q = new Queue<BinaryTreeNode>();

                q.Enqueue(Root);
                while (q.Any())
                {
                    BinaryTreeNode temp = q.Dequeue();
                    if (temp.Left == null)
                    {
                        temp.Left = node;
                        break;
                    }
                    else if (temp.Right == null)
                    {
                        temp.Right = node;
                        break;
                    }
                    else
                    {
                        q.Enqueue(temp.Left);
                        q.Enqueue(temp.Right);
                    }
                }
                //Free Queue and delete
            }
        }
示例#5
0
 void preOrderValues(BinaryTreeNode current)
 {
     if (current != null) {
     preOrder.Add(current.data);
     preOrderValues(current.left);
     preOrderValues(current.right);
     }
 }
示例#6
0
 private BinaryTreeNode SearchIterative(BinaryTreeNode root, int key)
 {
     if (root == null)
     {
         return null;
     }
     else
     {
         BinaryTreeNode p = root;
         while (p != null)
         {
             if (p.Key == key)
             {
                 return p;
             }
             else if (key < p.Key)
             {
                 p = p.Left;
             }
             else
             {
                 p = p.Right;
             }
         }
         return null;
     }
 }
示例#7
0
 private BinaryTreeNode SearchRecursiveBinaryTree(BinaryTreeNode root, int k)
 {
     if (root == null) return null;
     if(root.Key ==k)
     {
         return root;
     }
     BinaryTreeNode leftSearch = SearchRecursiveBinaryTree(root.Left, k);
     if(leftSearch!=null)
     {
         return leftSearch;
     }
     else
     {
         return SearchRecursiveBinaryTree(root.Right, k);
     }
 }
示例#8
0
 private bool isSymmetric(BinaryTreeNode leftRoot, BinaryTreeNode rightRoot)
 {
     if (leftRoot == null && rightRoot == null)
     {
         return true;
     }
     else if (leftRoot != null && rightRoot != null)
     {
         return (leftRoot.Key == rightRoot.Key && isSymmetric(leftRoot.Left, rightRoot.Right) && isSymmetric(leftRoot.Right, rightRoot.Left));
     }
     else
     {
         return false;
     }
 }
示例#9
0
        /// <summary>
        /// Here it is assumed that nodes are present in the binary tree
        /// </summary>
        /// <param name="root"></param>
        /// <param name="p"></param>
        /// <param name="q"></param>
        /// <returns></returns>
        private BinaryTreeNode LCA_BinaryTree(BinaryTreeNode root, BinaryTreeNode p, BinaryTreeNode q)
        {
            if(root == null)
            {
                return null;
            }
            if(root==p|| root== q)
            {
                return root;
            }
            BinaryTreeNode leftLca = LCA_BinaryTree(root.Left, p, q);
            BinaryTreeNode rightLca = LCA_BinaryTree(root.Right, p, q);

            if(leftLca != null && rightLca != null)
            {
                //one on left and one on right scenario
                return root;
            }
            else
            {
                //Both left or both right scenario
                return leftLca != null ? leftLca : rightLca;
            }
        }
示例#10
0
 private void InsertIterative(int key)
 {
     BinaryTreeNode node = new BinaryTreeNode(key);
     if (Root == null)
     {
         Root = new BinaryTreeNode(key);
     }
     else
     {
         BinaryTreeNode p = Root;
         BinaryTreeNode prev = null;
         while (p != null)
         {
             prev = p;
             if (key <= p.Key)
             {
                 p = p.Left;
             }
             else
             {
                 p = p.Right;
             }
         }
         if (key <= prev.Key)
         {
             prev.Left = node;
         }
         else
         {
             prev.Right = node;
         }
     }
 }
示例#11
0
 private BinaryTreeNode InsertRecursive(BinaryTreeNode root, int key)
 {
     if (root == null)
     {
         root = new BinaryTreeNode(key);
     }
     else
     {
         if (key <= root.Key)
         {
             root.Left = InsertRecursive(root.Left, key);
         }
         else
         {
             root.Right = InsertRecursive(root.Right, key);
         }
     }
     return root;
 }
示例#12
0
 public BinaryTreeNode(int data, BinaryTreeNode left, BinaryTreeNode right)
 {
     this.data  = data;
     this.left  = left;
     this.right = right;
 }
示例#13
0
        private int HeightIterative(BinaryTreeNode root)
        {
            // Time Complexity : O(n) - have to visit all nodes
            // Space complexity : O(width of tree)
            if (root == null) return 0;
            int level = 0;
            Queue<BinaryTreeNode> q = new Queue<BinaryTreeNode>();

            BinaryTreeNode marker = null;
            q.Enqueue(root);
            q.Enqueue(marker);

            while (q.Any())
            {
                BinaryTreeNode temp = q.Dequeue();

                if (temp == marker)
                {
                    //End of level. Increment
                    level++;

                    if (q.Any())
                    {
                        q.Enqueue(marker);
                    }
                    else
                    { //Reached the end.
                        break;
                    }
                }
                else
                {
                    if (temp.Left != null)
                    {
                        q.Enqueue(temp.Left);
                    }
                    if (temp.Right != null)
                    {
                        q.Enqueue(temp.Right);
                    }
                }
            }
            return level;
        }
示例#14
0
 void PreOrderIterative(BinaryTreeNode current)
 {
     Stack<BinaryTreeNode> s = new Stack<BinaryTreeNode>();
     s.Push(current);
     while(s.Count!=0)
     {
         BinaryTreeNode temp= s.Pop();
         Console.WriteLine(temp);
         s.Push(temp.right);
         s.Push(temp.left);
     }
 }
示例#15
0
 void PreOrder(BinaryTreeNode current)
 {
     if (current == null) return;
     Console.WriteLine(current);
     PreOrder(current.left);
     PreOrder(current.right);
 }
示例#16
0
 public BinarySearchTree()
 {
     root = null;
 }
示例#17
0
 bool MatchTree(BinaryTreeNode n1, BinaryTreeNode n2)
 {
     if (n2 == null)
         return true;
     if (n1 == null)
         return false;
     if (n1.data == n2.data)
         return MatchTree(n1.left, n2.left)
             && MatchTree(n1.right, n2.right);
     else
         return false;
 }
示例#18
0
 void LeveLOrder(BinaryTreeNode current)
 {
     Queue<BinaryTreeNode> q = new Queue<BinaryTreeNode>();
     q.Enqueue(current);
     while (q.Count != 0)
     {
         BinaryTreeNode temp = q.Dequeue();
         Console.WriteLine(temp);
         if (temp.left != null)
             q.Enqueue(temp.left);
         if (temp.right != null)
             q.Enqueue(temp.right);
     }
 }
示例#19
0
 private BinaryTreeNode FindMinInBST(BinaryTreeNode root)
 {
     if (root == null) return null;
     BinaryTreeNode p = root;
     while(p.Left!=null)
     {
         p = p.Left;
     }
     return p;
 }
示例#20
0
 int TreeHeight(BinaryTreeNode root)
 {
     if (root == null)
         return 0;
     return 1 + Math.Max(TreeHeight(root.right), TreeHeight(root.left));
 }
示例#21
0
 private int HeightRecursive(BinaryTreeNode root)
 {
     // Time Complexity : O(n) - have to visit all nodes
     // Space complexity : O(n)
     if (root == null)
     {
         return 0;
     }
     else
     {
         int lheight = HeightRecursive(root.Left);
         int rheight = HeightRecursive(root.Right);
         return 1 + Math.Max(lheight, rheight);
     }
 }
示例#22
0
 public BinaryTreeNode(int data)
 {
     this.data = data;
     this.left = null;
     this.right = null;
 }
示例#23
0
 public BinaryTreeNode(int data, BinaryTreeNode left, BinaryTreeNode right)
 {
     this.data = data;
     this.left = left;
     this.right = right;
 }
 public clsBinarySearchTree()
 {
     _root = null;
 }
示例#25
0
        private int IsHeightBalanced(BinaryTreeNode root)
        {
            if (root == null)
            {
                return 0;
            }

            int lh = IsHeightBalanced(root.Left);
            if (lh == -2) return -2;

            int rh = IsHeightBalanced(root.Right);
            if (rh == -2) return -2;

            if (Math.Abs(lh - rh) > 1) return -2;

            return (1 + Math.Max(lh, rh));
        }
 public void Clear()
 {
     _root = null;
 }
示例#27
0
        private void KthNodeInorder(BinaryTreeNode root, ref BinaryTreeNode KthNode, ref int count, int k)
        {
            if (root == null)
            {
                return;
            }
            KthNodeInorder(root.Left, ref KthNode, ref count, k);
            count++;
            if (count == k)
            {
                KthNode = root;
                return;
            }

            KthNodeInorder(root.Right,ref KthNode ,ref count, k);
        }
示例#28
0
        public BinaryTreeNode NextRight; //Used in some of the problems

        public BinaryTreeNode(int val, BinaryTreeNode left = null, BinaryTreeNode right = null)
        {
            Value = val;
            Left  = left;
            Right = right;
        }
示例#29
0
        private BinaryTreeNode LCA_BST(BinaryTreeNode root, int p , int q)
        {
            if (root == null) return null;

            BinaryTreeNode temp = root;

            while(temp!=null)
            {

                if(p< temp.Key && q < temp.Key)
                {
                    //Go left
                    temp = temp.Left;
                }
                else if(p> temp.Key && q> temp.Key)
                {
                    //Go right
                    temp = temp.Right;
                }
                else
                {
                    //found lca
                    return temp;
                }

            }

            return null;
        }
示例#30
0
 public void KthNodeInorder(ref BinaryTreeNode KthNode, int k)
 {
     int count = 0;
     KthNodeInorder(Root, ref KthNode, ref count, k);
 }
示例#31
0
 private BinaryTreeNode SearchRecursive(BinaryTreeNode root, int key)
 {
     if (root == null) return null;
     if (root.Key == key) return root;
     if (key < root.Key)
     {
         return SearchRecursive(root.Left, key);
     }
     else
     {
         return SearchRecursive(root.Right, key);
     }
 }
示例#32
0
        public void PrintDoublyLinkedList(BinaryTreeNode root)
        {
            //Print the doubly linked list
            if (Root == null) return;
            if(Root.Left==null && Root.Right == null)
            {
                Console.WriteLine(Root.Key);
                return;
            }

            BinaryTreeNode p = Root;
            while (p.Left != null) p = p.Left;

            while(p!=null)
            {
                Console.WriteLine(p.Key);
                p = p.Right;
            }
        }
示例#33
0
 public void BinaryTreeToDoublyLinkedList(ref BinaryTreeNode visited)
 {
     BinaryTreeToDoublyLinkedList(Root, ref visited);
 }
示例#34
0
        private void BinaryTreeToDoublyLinkedList(BinaryTreeNode root, ref BinaryTreeNode visited)
        {
            if (root == null) return;
            BinaryTreeToDoublyLinkedList(root.Left, ref visited);
            if(visited!=null)
            {
                visited.Right = root;
                root.Left = visited;
            }
            visited = root;

            //traverse right
            BinaryTreeToDoublyLinkedList(root.Right, ref visited);
        }
示例#35
0
 public void Insert(int key, bool isIterative = false)
 {
     if (isIterative)
     {
         InsertIterative(key);
     }
     else
     {
         BinaryTreeNode temp = InsertRecursive(Root, key);
         if (Root == null)
         {
             Root = temp;
         }
     }
 }
示例#36
0
 public BinaryTreeNode(int data)
 {
     this.data  = data;
     this.left  = null;
     this.right = null;
 }