Пример #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            KarthicBinaryTree <int> tree = TreeHelper.SetUpBinaryTree();

            //Pre-Order Traversal
            //Traverse the binary tree in the follwing order current node, left childrens  and right childrens

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

            this.textBox1.Text = output;

            //Expected output:  1, 2, 4, 5, 3, 6, 7
        }
        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);
        }
Пример #3
0
        private void button3_Click(object sender, EventArgs e)
        {
            KarthicBinaryTree <int> t1 = TreeHelper.SetUpBinaryTree();


            KarthicBinaryTree <int> t2 = TreeHelper.SetupBinarySubTree();

            t2.Root.Left.Right = new KarthicBTNode <int>(9);

            //Array solution with special character
            //Make In-order and pre-order travesal by inserting zero for null values and return string
            //Check whether s2 is a substring of s1
            //Key things: We have to do insert zero for null
            //we have to do both in-order and pre-order


            StringBuilder s1 = new StringBuilder();
            StringBuilder s2 = new StringBuilder();
            StringBuilder s3 = new StringBuilder();
            StringBuilder s4 = new StringBuilder();

            t1.InOrderTraversal(t1.Root, ref s1, true);
            t2.InOrderTraversal(t2.Root, ref s2, true);
            t1.PreOrderTraversal(t1.Root, true); //get in

            t2.PreOrderTraversal(t2.Root, true);
            string output = string.Empty;

            //t2 is substring for t1 via in order traversal
            if (t1.ToString().Contains(t2.ToString()) && (t1.sb.ToString().Contains(t2.sb.ToString())))
            {
                output = "T2 is a subtree of T1";
            }
            else
            {
                output = "T2 is not a subtree of T1";
            }
        }