public char FindLowestCommonAncestorBinaryTree(NodeChar root, char n1, char n2)
        {
            List <char> path1 = new List <char>();
            List <char> path2 = new List <char>();

            if (!findPath(root, n1, path1) || !findPath(root, n2, path2))
            {
                Console.WriteLine((path1.Count > 0) ? "n1 is present" : "n1 is missing");
                Console.WriteLine((path2.Count > 0) ? "n2 is present" : "n2 is missing");
                return('$');
            }

            int i;

            for (i = 0; i < path1.Count && i < path2.Count; i++)
            {
                // System.out.println(path1.get(i) + " " + path2.get(i));
                if (!path1[i].Equals(path2[i]))
                {
                    break;
                }
            }

            return(path1[i - 1]);
        }
Exemplo n.º 2
0
        public void TreeTraversalsM()
        {
            TreeTraversals treeTraversals = new TreeTraversals();

            NodeChar root = new NodeChar('A')
            {
                left  = new NodeChar('B'),
                right = new NodeChar('C')
            };

            root.left.left = new NodeChar('D')
            {
                //left = new Node('F'),
                //right = new Node('G')
            };

            root.left.right = new NodeChar('E')
            {
                //left = new Node('H'),
                //right = new Node('I')
            };

            treeTraversals.PreOrderTreeTraversal(root);
            Console.WriteLine();

            treeTraversals.InOrderTreeTraversal(root);
            Console.WriteLine();

            treeTraversals.PostOrderTreeTraversal(root);
            Console.WriteLine();
        }
        private bool findPath(NodeChar root, char n, List <char> path)
        {
            // base case
            if (root == null)
            {
                return(false);
            }

            // Store this node . The node will be removed if
            // not in path from root to n.
            path.Add(root.Value);

            if (root.Value == n)
            {
                return(true);
            }

            if (root.left != null && findPath(root.left, n, path))
            {
                return(true);
            }

            if (root.right != null && findPath(root.right, n, path))
            {
                return(true);
            }

            // If not present in subtree rooted with root, remove root from
            // path[] and return false
            path.RemoveAt(path.Count - 1);

            return(false);
        }
Exemplo n.º 4
0
        public void ConstructTreeFromTraversalsM()
        {
            char[] preOrderTraversal = { 'A', 'B', 'D', 'C' };
            char[] inOrderTraversal  = { 'D', 'B', 'A', 'C' };
            //char[] postOrderTraversal = { 'D', 'E', 'B', 'C', 'A' };

            ConstructTreeFromTraversal constructTreeFromTraversal = new ConstructTreeFromTraversal();
            NodeChar       root           = constructTreeFromTraversal.BuildTree(inOrderTraversal, preOrderTraversal, 0, inOrderTraversal.Length - 1);
            TreeTraversals treeTraversals = new TreeTraversals();

            treeTraversals.PostOrderTreeTraversal(root);
        }
Exemplo n.º 5
0
        // left, right, root
        public void PostOrderTreeTraversal(NodeChar node)
        {
            if (node == null)
            {
                return;
            }

            PostOrderTreeTraversal(node.left);

            PostOrderTreeTraversal(node.right);

            Console.Write(node.Value + " ");
        }
Exemplo n.º 6
0
        /* Computes the diameter of binary
         * tree with given root. */
        public int FindDiameterOfBinaryTree(NodeChar root)
        {
            if (root == null)
            {
                return(0);
            }

            // This will store the final answer
            A   a = new A();
            int height_of_tree = height(root, a);

            return(a.ans);
        }
Exemplo n.º 7
0
        /* Function to find height of a tree */
        public int height(NodeChar root, A a)
        {
            if (root == null)
            {
                return(0);
            }

            int left_height = height(root.left, a);

            int right_height = height(root.right, a);

            // update the answer, because diameter of a
            // tree is nothing but maximum value of
            // (left_height + right_height + 1) for each node
            a.ans = Math.Max(a.ans, 1 + left_height +
                             right_height);

            return(1 + Math.Max(left_height, right_height));
        }
        public NodeChar BuildTree(char[] inOrderTraversal, char[] preOrderTraversal,
                                  int inOrderStart, int inOrderEnd)
        {
            if (inOrderStart > inOrderEnd)
            {
                return(null);
            }

            NodeChar tNode = new NodeChar(preOrderTraversal[preOrderIndex++]);

            if (inOrderStart == inOrderEnd)
            {
                return(tNode);
            }

            int inOrderIndex = Search(inOrderTraversal, inOrderStart, inOrderEnd, tNode.Value);

            tNode.left  = BuildTree(inOrderTraversal, preOrderTraversal, inOrderStart, inOrderIndex - 1);
            tNode.right = BuildTree(inOrderTraversal, preOrderTraversal, inOrderIndex + 1, inOrderEnd);

            return(tNode);
        }
Exemplo n.º 9
0
        public void SpiralPrintingOfBinaryTreeM()
        {
            NodeChar root = new NodeChar('A')
            {
                left  = new NodeChar('B'),
                right = new NodeChar('C')
            };

            root.left.left = new NodeChar('D')
            {
                left  = new NodeChar('F'),
                right = new NodeChar('G')
            };

            root.left.right = new NodeChar('E')
            {
                //left = new NodeChar('H'),
                //right = new NodeChar('I')
            };
            SpiralPrintingOfBinaryTreeC spiralPrintingOfBinaryTreeC = new SpiralPrintingOfBinaryTreeC();

            spiralPrintingOfBinaryTreeC.SpiralPrintingOfABinaryTree(root);
        }
Exemplo n.º 10
0
        public void LowestCommonAncestorBinaryTreeM()
        {
            NodeChar root = new NodeChar('A')
            {
                left  = new NodeChar('B'),
                right = new NodeChar('C')
            };

            root.left.left = new NodeChar('D')
            {
                left  = new NodeChar('F'),
                right = new NodeChar('G')
            };

            root.left.right = new NodeChar('E')
            {
                //left = new NodeChar('H'),
                //right = new NodeChar('I')
            };
            LowestCommonAncestorBinaryTreeC lowestCommonAncestorBinaryTreeC = new LowestCommonAncestorBinaryTreeC();

            Console.WriteLine(lowestCommonAncestorBinaryTreeC.FindLowestCommonAncestorBinaryTree(root, 'F', 'E'));
        }
Exemplo n.º 11
0
        public void DiameterOfBinaryTreeM()
        {
            NodeChar root = new NodeChar('A')
            {
                left  = new NodeChar('B'),
                right = new NodeChar('C')
            };

            root.left.left = new NodeChar('D')
            {
                left  = new NodeChar('F'),
                right = new NodeChar('G')
            };

            root.left.right = new NodeChar('E')
            {
                //left = new NodeChar('H'),
                //right = new NodeChar('I')
            };
            DiameterOfBinaryTreeC diameterOfBinaryTreeC = new DiameterOfBinaryTreeC();

            Console.WriteLine(diameterOfBinaryTreeC.FindDiameterOfBinaryTree(root));
        }
Exemplo n.º 12
0
        public void SpiralPrintingOfABinaryTree(NodeChar node)
        {
            if (node == null)
            {
                return; // NULL check
            }

            // Create two stacks to store alternate levels
            Stack <NodeChar> s1 = new Stack <NodeChar>(); // For levels to be printed
                                                          // from right to left
            Stack <NodeChar> s2 = new Stack <NodeChar>(); // For levels to be printed

            // from left to right

            // Push first level to first stack 's1'
            s1.Push(node);

            // Keep printing while any of the
            // stacks has some nodes
            while (s1.Count > 0 || s2.Count > 0)
            {
                // Print nodes of current level from
                // s1 and push nodes of next level to s2
                while (s1.Count > 0)
                {
                    NodeChar temp = s1.Peek();
                    s1.Pop();
                    Console.Write(temp.Value + " ");

                    // Note that is right is pushed before left
                    if (temp.right != null)
                    {
                        s2.Push(temp.right);
                    }

                    if (temp.left != null)
                    {
                        s2.Push(temp.left);
                    }
                }

                // Print nodes of current level from s2
                // and push nodes of next level to s1
                while (s2.Count > 0)
                {
                    NodeChar temp = s2.Peek();
                    s2.Pop();
                    Console.Write(temp.Value + " ");

                    // Note that is left is pushed before right
                    if (temp.left != null)
                    {
                        s1.Push(temp.left);
                    }
                    if (temp.right != null)
                    {
                        s1.Push(temp.right);
                    }
                }
            }
        }