Example #1
0
        public CustomNode <int> BuildSpecialTreeFromPreorderArray(int[] preArray, char[] LeafArray, int index)
        {
            if (index == preArray.Length)
            {
                CustomNode <int> nodenull = new CustomNode <int>();
                nodenull.treenode   = null;
                nodenull.StartIndex = index;
                return(nodenull);
            }

            KarthicBTNode <int> node = new KarthicBTNode <int>(preArray[index]);
            int actualnodeindex      = index;

            index++;

            CustomNode <int> custom = new CustomNode <int>();

            custom.treenode   = node;
            custom.StartIndex = index;
            //No leaf node
            if (LeafArray[actualnodeindex] == 'N')
            {
                CustomNode <int> ltree = BuildSpecialTreeFromPreorderArray(preArray, LeafArray, index);
                custom.treenode.Left = ltree.treenode;

                CustomNode <int> rtree = BuildSpecialTreeFromPreorderArray(preArray, LeafArray, ltree.StartIndex);
                custom.treenode.Right = rtree.treenode;

                custom.StartIndex = rtree.StartIndex;
            }


            return(custom);
        }
        private void button5_Click(object sender, EventArgs e)
        {
            //source: http://www.geeksforgeeks.org/construct-a-special-tree-from-given-preorder-traversal/
            int[]  preorderarray = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox5.Text);
            char[] leafarray     = AlgorithmHelper.ConvertCommaSeparetedStringToCharArray(this.textBox4.Text);

            CustomNode <int>        tree1 = new CustomNode <int>();
            KarthicBinaryTree <int> tree  = new KarthicBinaryTree <int>();

            tree.Root = BuildSpecialTreeFromPreorderArray(preorderarray, leafarray, 0).treenode;

            string output = tree.PreOrderTraversal(tree.Root);


            bool result = String.Equals(this.textBox5.Text, output.Substring(0, output.LastIndexOf(',')), StringComparison.OrdinalIgnoreCase);
        }
        //souce: http://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/
        //Logic
        //Inorder sequence: D B E A F C
        //Preorder sequence: A B D E C F
        //Given inorder and preorder sequence
        //pick the first elemnet in preorder and build root element
        //increment the value of preorderindex and maintain in the recurrsion..no bubble needed
        //find the root element in the inorder array and get it's index
        //the element to the left of index are leftsubtree and the element right of the index forms rightsubtree.
        //recurse and build the tree in this logic

        //The result tree
        //        //A
        //       /   \
        //     /       \
        //    B         C
        //   / \        /
        // /     \    /
        //D       E  F


        public CustomNode <char> BuildBSTreeFromInOrderAndPreOrderArray(char[] preArray, int preIndex, char[] inArray, int inLow, int inHigh)
        {
            //base case
            if (inHigh < inLow)
            {
                CustomNode <char> nodenull = new CustomNode <char>();
                nodenull.treenode   = null;
                nodenull.StartIndex = preIndex;
                return(nodenull);
            }

            //build the current node
            KarthicBTNode <char> current = new KarthicBTNode <char>(Convert.ToChar(preArray[preIndex]));

            //increment
            preIndex++;

            CustomNode <char> result = new CustomNode <char>();

            result.treenode   = current;
            result.StartIndex = preIndex;

            //after building the node..check for low and high
            //for case low = 2 and high = 3..it mean this is leaf node..left and right will be null
            //we stop the recursion here
            if (inLow == inHigh)
            {
                return(result);
            }

            int indexofcurrent = SearchArray(inArray, current.Data, inLow, inHigh);

            //divide the tree
            CustomNode <char> lsubtree = BuildBSTreeFromInOrderAndPreOrderArray(preArray, preIndex, inArray, inLow, indexofcurrent - 1);

            result.treenode.Left = lsubtree.treenode;

            CustomNode <char> rsubtree = BuildBSTreeFromInOrderAndPreOrderArray(preArray, lsubtree.StartIndex, inArray, indexofcurrent + 1, inHigh);

            result.treenode.Right = rsubtree.treenode;

            result.StartIndex = rsubtree.StartIndex;


            return(result);
        }
        private void button4_Click(object sender, EventArgs e)
        {
            char[] inorderarray  = AlgorithmHelper.ConvertCommaSeparetedStringToCharArray(this.textBox2.Text);
            char[] preorderarray = AlgorithmHelper.ConvertCommaSeparetedStringToCharArray(this.textBox3.Text);

            CustomNode <char> tree = new CustomNode <char>();
            KarthicBST <char> root = new KarthicBST <char>();

            tree = BuildBSTreeFromInOrderAndPreOrderArray(preorderarray, 0, inorderarray, 0, inorderarray.Length - 1);

            //The result tree
            //        //A
            //       /   \
            //     /       \
            //    B         C
            //   / \        /
            // /     \    /
            //D       E  F
        }