Exemplo n.º 1
0
 internal void DiagonalTraversalofBinaryTree(BinaryTreeNodeSM root, int v, Dictionary <int, HashSet <int> > keyValuePairs)
 {
     if (root != null)
     {
         if (keyValuePairs.ContainsKey(v))
         {
             keyValuePairs[v].Add(root.data);
         }
         else
         {
             keyValuePairs.Add(v, new HashSet <int>()
             {
                 root.data
             });
         }
         if (root.leftChild != null)
         {
             DiagonalTraversalofBinaryTree(root.leftChild, v + 1, keyValuePairs);
         }
         if (root.rightChild != null)
         {
             DiagonalTraversalofBinaryTree(root.rightChild, v, keyValuePairs);
         }
     }
 }
Exemplo n.º 2
0
        internal int FindHeightIterativelySm(BinaryTreeNodeSM root)
        {
            int   height = 0;
            Queue queue  = new Queue();

            queue.Enqueue(root);
            while (true)
            {
                int queueSize = queue.Count;
                if (queueSize > 0)
                {
                    height += 1;
                }
                else
                {
                    break;
                }
                while (queueSize > 0)
                {
                    BinaryTreeNodeSM binaryTreeNodeSM = (BinaryTreeNodeSM)queue.Dequeue();
                    if (binaryTreeNodeSM.leftChild != null)
                    {
                        queue.Enqueue(binaryTreeNodeSM.leftChild);
                    }
                    if (binaryTreeNodeSM.rightChild != null)
                    {
                        queue.Enqueue(binaryTreeNodeSM.rightChild);
                    }
                    queueSize--;
                }
            }
            return(height);
        }
Exemplo n.º 3
0
        public int MaximumWidthOfTreeUsingQueue(BinaryTreeNodeSM root)
        {
            if (root == null)
            {
                return(0);
            }
            Queue <BinaryTreeNodeSM> queu = new Queue <BinaryTreeNodeSM>();

            queu.Enqueue(root);
            int result = 0;

            while (queu.Count > 0)
            {
                int s = queu.Count();
                if (s > result)
                {
                    result = s;
                }
                while (s-- > 0)
                {
                    BinaryTreeNodeSM b = queu.Dequeue();
                    if (b.leftChild != null)
                    {
                        queu.Enqueue(b.leftChild);
                    }
                    if (b.rightChild != null)
                    {
                        queu.Enqueue(b.rightChild);
                    }
                }
            }
            return(result);
        }
Exemplo n.º 4
0
        internal bool CheckSumofCoveredUncoveredNodes(BinaryTreeNodeSM root)
        {
            int sumUc = GetUncoveredSum(root);
            int total = GetSum(root);

            return(sumUc == (total - sumUc));
        }
Exemplo n.º 5
0
        internal void LevelOrderTransversalLineByLineSM(BinaryTreeNodeSM root)
        {
            Queue <BinaryTreeNodeSM> q = new Queue <BinaryTreeNodeSM>();

            q.Enqueue(root);
            while (q.Count > 0)
            {
                int count = q.Count;
                while (count != 0)
                {
                    BinaryTreeNodeSM binaryTreeNodeSM = q.Dequeue();
                    Console.Write(binaryTreeNodeSM.data);
                    if (binaryTreeNodeSM.leftChild != null)
                    {
                        q.Enqueue(binaryTreeNodeSM.leftChild);
                    }
                    if (binaryTreeNodeSM.rightChild != null)
                    {
                        q.Enqueue(binaryTreeNodeSM.rightChild);
                    }
                    count--;
                }
                Console.WriteLine();
            }
        }
Exemplo n.º 6
0
 private int GetSum(BinaryTreeNodeSM root)
 {
     if (root == null)
     {
         return(0);
     }
     return(root.data + GetSum(root.leftChild) + GetSum(root.rightChild));
 }
Exemplo n.º 7
0
 internal void PrintRootToLeafWithoutRecursion(BinaryTreeNodeSM root, Stack <int> s)
 {
     if (root == null)
     {
         return;
     }
     s.Push(root.data);
 }
Exemplo n.º 8
0
        public void BreadthFirstTransversal(BinaryTreeNodeSM node)
        {
            int height = HeightSM(node);

            for (int i = 1; i <= height; i++)
            {
                PrintLevel(i, node);
            }
        }
Exemplo n.º 9
0
 public void PostOrderTransversal(BinaryTreeNodeSM node)
 {
     if (node == null)
     {
         return;
     }
     PostOrderTransversal(node.leftChild);
     PostOrderTransversal(node.rightChild);
     Console.WriteLine(node.data);
 }
Exemplo n.º 10
0
        internal int SumOfLeftLeaves(BinaryTreeNodeSM root)
        {
            if (root == null)
            {
                return(0);
            }
            int sum = 0;

            sum = SumOfLeftLeavesUtil(root, false);
            return(sum);
        }
Exemplo n.º 11
0
        internal int FindHeightSm(BinaryTreeNodeSM root)
        {
            if (root == null)
            {
                return(0);
            }
            int lHeight = FindHeightSm(root.leftChild);
            int rHeight = FindHeightSm(root.rightChild);

            return(1 + Math.Max(lHeight, rHeight));
        }
Exemplo n.º 12
0
 internal void PopulateNextInOrderSuccessorNonStaticSM(BinaryTreeNodeSM root, BinaryTreeNodeSM next)
 {
     if (root == null)
     {
         return;
     }
     PopulateNextInOrderSuccessorNonStaticSM(root.rightChild, next);
     root.inorderSuccessorNext = next;
     next = root;
     PopulateNextInOrderSuccessorNonStaticSM(root.leftChild, next);
 }
Exemplo n.º 13
0
 internal void PopulateNextInOrderSuccessorSM(BinaryTreeNodeSM root)
 {
     if (root == null)
     {
         return;
     }
     PopulateNextInOrderSuccessorSM(root.rightChild);
     root.inorderSuccessorNext = nextInOrderSuccess;
     nextInOrderSuccess        = root;
     PopulateNextInOrderSuccessorSM(root.leftChild);
 }
Exemplo n.º 14
0
        internal void BoundaryTraversal(BinaryTreeNodeSM root)
        {
            if (root == null)
            {
                return;
            }
            PrintLeftSide(root);
            PrintLeaves(root.leftChild);
            PrintLeaves(root.rightChild);

            PrintRightSide(root.rightChild);
        }
Exemplo n.º 15
0
 private int SumOfLeftLeavesUtil(BinaryTreeNodeSM root, bool isLeft)
 {
     if (root == null)
     {
         return(0);
     }
     if (isLeft && root.leftChild == null && root.rightChild == null)
     {
         return(root.data);
     }
     return(SumOfLeftLeavesUtil(root.leftChild, true) + SumOfLeftLeavesUtil(root.rightChild, false));
 }
Exemplo n.º 16
0
 private int WidthSM(int i, BinaryTreeNodeSM root)
 {
     if (root == null)
     {
         return(0);
     }
     if (i == 1)
     {
         return(1);
     }
     return(WidthSM(i - 1, root.leftChild) + WidthSM(i - 1, root.rightChild));
 }
Exemplo n.º 17
0
        public int DiameterOfBinaryTree(BinaryTreeNodeSM node)
        {
            if (node == null)
            {
                return(0);
            }
            int lheight   = HeightSM(node.leftChild);
            int rheight   = HeightSM(node.rightChild);
            int ldiameter = DiameterOfBinaryTree(node.leftChild);
            int rDiameter = DiameterOfBinaryTree(node.rightChild);

            return(Math.Max(1 + lheight + rheight, Math.Max(ldiameter, rDiameter)));
        }
Exemplo n.º 18
0
        public void LevelOrderTransversal(BinaryTreeNodeSM node)
        {
            if (node == null)
            {
                return;
            }
            int height = HeightSM(node);

            for (int i = 1; i <= height; i++)
            {
                PrintLevel(i, node);
            }
        }
Exemplo n.º 19
0
        private int GetUncoveredSum(BinaryTreeNodeSM root)
        {
            if (root == null)
            {
                return(0);
            }
            int leftSum  = 0;
            int rightSum = 0;

            GetLeftUncovered(root.leftChild, ref leftSum);
            GetRightUncovered(root.rightChild, ref rightSum);
            return(root.data + leftSum + rightSum);
        }
Exemplo n.º 20
0
        internal bool IsBinaryTreeSumTreeFaster(BinaryTreeNodeSM root)
        {
            if (root == null)
            {
                return(true);
            }
            if (root.leftChild == null && root.rightChild == null)
            {
                return(true);
            }

            if (IsBinaryTreeSumTreeFaster(root.leftChild) && IsBinaryTreeSumTreeFaster(root.rightChild))
            {
                int leftData   = 0;
                int rightChild = 0;
                if (root.leftChild == null)
                {
                    leftData = 0;
                }
                else if (isLeaf(root.leftChild))
                {
                    leftData = root.leftChild.data;
                }
                else
                {
                    leftData = root.leftChild.data * 2;
                }

                if (root.rightChild == null)
                {
                    rightChild = 0;
                }
                else if (isLeaf(root.rightChild))
                {
                    rightChild = root.rightChild.data;
                }
                else
                {
                    rightChild = root.rightChild.data * 2;
                }
                if (root.data == leftData + rightChild)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(false);
        }
Exemplo n.º 21
0
        private void PrintLeaves(BinaryTreeNodeSM node)
        {
            if (node == null)
            {
                return;
            }
            PrintLeaves(node.leftChild);
            if (node.leftChild == null && node.rightChild == null)
            {
                Console.WriteLine(node.data);
            }

            PrintLeaves(node.rightChild);
        }
Exemplo n.º 22
0
 public void PrintNodeAtKDistance(int k, BinaryTreeNodeSM root)
 {
     if (root == null)
     {
         return;
     }
     if (k == 1)
     {
         Console.WriteLine(root.data);
         return;
     }
     PrintNodeAtKDistance(k - 1, root.leftChild);
     PrintNodeAtKDistance(k - 1, root.rightChild);
 }
Exemplo n.º 23
0
 internal void FindNthNodeInpostTransversal(BinaryTreeNodeSM root, ref int n)
 {
     if (root == null)
     {
         return;
     }
     FindNthNodeInpostTransversal(root.leftChild, ref n);
     FindNthNodeInpostTransversal(root.rightChild, ref n);
     n--;
     if (n == 0)
     {
         Console.WriteLine(root.data);
     }
 }
Exemplo n.º 24
0
 internal bool CheckForFullBinaryTree(BinaryTreeNodeSM root)
 {
     if (root == null)
     {
         return(true);
     }
     if (root.leftChild == null && root.rightChild == null)
     {
         return(true);
     }
     if (root.leftChild == null || root.rightChild == null)
     {
         return(false);
     }
     return(CheckForFullBinaryTree(root.leftChild) && CheckForFullBinaryTree(root.rightChild));
 }
Exemplo n.º 25
0
        internal bool IsBinaryTreeSumTree(BinaryTreeNodeSM root)
        {
            if (root == null)
            {
                return(true);
            }
            if (root.leftChild == null && root.rightChild == null)
            {
                return(true);
            }
            int leftSum  = FindSum(root.leftChild);
            int rightSum = FindSum(root.rightChild);

            return(root.data == leftSum + rightSum && IsBinaryTreeSumTree(root.leftChild) &&
                   IsBinaryTreeSumTree(root.rightChild));
        }
Exemplo n.º 26
0
        public void InOrderTreeTransversalWithoutRecursion(BinaryTreeNodeSM root)
        {
            Stack <BinaryTreeNodeSM> s = new Stack <BinaryTreeNodeSM>();

            while (root != null || s.Count > 0)
            {
                while (root != null)
                {
                    s.Push(root);
                    root = root.leftChild;
                }
                root = s.Pop();
                Console.WriteLine(root.data);
                root = root.rightChild;
            }
        }
Exemplo n.º 27
0
        internal bool ChildrenSumProperty(BinaryTreeNodeSM root)
        {
            if (root == null)
            {
                return(true);
            }
            if (root.leftChild == null && root.rightChild == null)
            {
                return(true);
            }
            int leftData  = root.leftChild != null ? root.leftChild.data : 0;
            int rightData = root.rightChild != null ? root.rightChild.data : 0;

            return(root.data == leftData + rightData &&
                   ChildrenSumProperty(root.rightChild) && ChildrenSumProperty(root.leftChild));
        }
Exemplo n.º 28
0
 internal void SumOfParentNodes(BinaryTreeNodeSM root, int data, ref int sum)
 {
     if (root == null)
     {
         return;
     }
     if (root.leftChild == null && root.rightChild == null)
     {
         return;
     }
     if (root.leftChild.data == data || root.rightChild.data == data)
     {
         sum = sum + root.data;
     }
     SumOfParentNodes(root.leftChild, data, ref sum);
     SumOfParentNodes(root.rightChild, data, ref sum);
 }
Exemplo n.º 29
0
 private void PrintLeftSide(BinaryTreeNodeSM root)
 {
     if (root == null)
     {
         return;
     }
     if (root.leftChild != null)
     {
         Console.WriteLine(root.data);
         PrintLeftSide(root.leftChild);
     }
     else if (root.rightChild != null)
     {
         Console.WriteLine(root.data);
         PrintLeftSide(root.rightChild);
     }
 }
Exemplo n.º 30
0
 private void PrintLevel(int level, BinaryTreeNodeSM node)
 {
     if (node == null)
     {
         return;
     }
     if (level == 1)
     {
         Console.WriteLine(node.data);
         return;
     }
     else
     {
         level--;
         PrintLevel(level, node.leftChild);
         PrintLevel(level, node.rightChild);
     }
 }