private void button8_Click(object sender, EventArgs e)
        {
            //        26
            //      /   \
            //    10     3
            //  /    \     \
            //4      6      3

            KarthicBinaryTree <int> bt = new KarthicBinaryTree <int>();

            bt.Root = new KarthicBTNode <int>(26);

            //set level 1
            bt.Root.Left  = new KarthicBTNode <int>(10);
            bt.Root.Right = new KarthicBTNode <int>(3);

            //set level 2
            bt.Root.Left.Left  = new KarthicBTNode <int>(4);
            bt.Root.Left.Right = new KarthicBTNode <int>(6);

            bt.Root.Right.Right = new KarthicBTNode <int>(3);
            //http://www.geeksforgeeks.org/check-if-a-given-binary-tree-is-sumtree/

            bool result = IsSumTreeOptimized(bt.Root);
        }
Beispiel #2
0
        private void button13_Click(object sender, EventArgs e)
        {
            //set tree with the following nodes

            KarthicBinaryTree <int> bt = new KarthicBinaryTree <int>();

            bt.Root = new KarthicBTNode <int>(1);

            //set level 1
            bt.Root.Left  = new KarthicBTNode <int>(2);
            bt.Root.Right = new KarthicBTNode <int>(3);

            //set level 2

            bt.Root.Left.Left   = new KarthicBTNode <int>(4);
            bt.Root.Left.Right  = new KarthicBTNode <int>(5);
            bt.Root.Right.Left  = new KarthicBTNode <int>(6);
            bt.Root.Right.Right = new KarthicBTNode <int>(7);

            //set level 3

            bt.Root.Left.Left.Left = new KarthicBTNode <int>(8);

            //making the tree unbalaced
            bt.Root.Left.Left.Left.Left = new KarthicBTNode <int>(9);

            //

            //In this problem, they have given the defenition of balanced tree..
            //For any node in the tree, the difference btw the height of left and height of right should not be greater than 1..
            //If it greater than 1 it is not balanced tree else it is balanced tree

            bool result = TreeHelper.IsBalanced(bt.Root);
        }
Beispiel #3
0
        public static KarthicBinaryTree <int> SetUpBinaryTree()
        {
            KarthicBinaryTree <int> bt = new KarthicBinaryTree <int>();

            bt.Root = new KarthicBTNode <int>(1);

            //set level 1
            bt.Root.Left  = new KarthicBTNode <int>(2);
            bt.Root.Right = new KarthicBTNode <int>(3);

            //set level 2

            bt.Root.Left.Left   = new KarthicBTNode <int>(4);
            bt.Root.Left.Right  = new KarthicBTNode <int>(5);
            bt.Root.Right.Left  = new KarthicBTNode <int>(6);
            bt.Root.Right.Right = new KarthicBTNode <int>(7);

            //set level 3

            //bt.Root.Left.Left.Left = new KarthicBTNode<int>(8);

            ////making the tree unbalaced
            //bt.Root.Left.Left.Left.Left = new KarthicBTNode<int>(9);

            return(bt);
        }
Beispiel #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            //set tree with the following nodes

            KarthicBinaryTree <int> bt = new KarthicBinaryTree <int>();

            bt.Root = new KarthicBTNode <int>(1);

            //set level 1
            bt.Root.Left  = new KarthicBTNode <int>(2);
            bt.Root.Right = new KarthicBTNode <int>(3);

            //set level 2

            bt.Root.Left.Left   = new KarthicBTNode <int>(4);
            bt.Root.Left.Right  = new KarthicBTNode <int>(5);
            bt.Root.Right.Left  = new KarthicBTNode <int>(6);
            bt.Root.Right.Right = new KarthicBTNode <int>(7);

            //set level 3

            bt.Root.Left.Left.Left = new KarthicBTNode <int>(8);

            //making the tree unbalaced
            bt.Root.Left.Left.Left.Left = new KarthicBTNode <int>(9);

            //If tree is unbalaced returns -1 else if balanced returns the actual height
            int result = TreeHelper.GetHeightAndBalaced(bt.Root);
        }
Beispiel #5
0
        private void button2_Click(object sender, EventArgs e)
        {
            //set tree with the following nodes

            KarthicBinaryTree <int> bt = new KarthicBinaryTree <int>();

            bt.Root = new KarthicBTNode <int>(20);

            //set level 1
            bt.Root.Left  = new KarthicBTNode <int>(8);
            bt.Root.Right = new KarthicBTNode <int>(22);

            //set level 2

            bt.Root.Left.Left  = new KarthicBTNode <int>(4);
            bt.Root.Left.Right = new KarthicBTNode <int>(12);


            bt.Root.Right.Right = new KarthicBTNode <int>(25);

            bt.Root.Left.Right.Left  = new KarthicBTNode <int>(10);
            bt.Root.Left.Right.Right = new KarthicBTNode <int>(14);


            this.textBox4.Text = PrintBorderOfBinaryTree(bt.Root);
        }
Beispiel #6
0
        private void button4_Click(object sender, EventArgs e)
        {
            //We really don't have to do level by level traversal..all we have to do is traverse by knowing the level during each recurrsion/iteration

            KarthicBinaryTree <int> tree = TreeHelper.SetUpBinaryTree();
            //List<KarthicLinkedList> colllist = new List<KarthicLinkedList>();
            List <LinkedList <KarthicBTNode <int> > > colllist = new List <LinkedList <KarthicBTNode <int> > >();

            CreateLinkedListForNodes(tree.Root, colllist, 0);
            StringBuilder sb = new StringBuilder();

            foreach (LinkedList <KarthicBTNode <int> > list in colllist)
            {
                if (sb.ToString() != string.Empty)
                {
                    sb.AppendLine();
                }
                foreach (KarthicBTNode <int> value in list)
                {
                    sb.Append(value.Data).Append(',');
                }
            }

            string output = sb.ToString();
        }
Beispiel #7
0
        private void button2_Click(object sender, EventArgs e)
        {
            KarthicBinaryTree <int> tree = TreeHelper.SetUpBinaryTree();
            StringBuilder           sb   = new StringBuilder();

            tree.LevelZigZacTraversal(tree.Root, sb);

            this.textBox1.Text = sb.ToString();
        }
Beispiel #8
0
        private void button5_Click(object sender, EventArgs e)
        {
            //            .e.g

            //_________________5___________________
            //__________6____________7_____________
            //_____1_________2

            //Possible path..updated 4/7/14...It doesn't have to be from root..can be any path
            //5, 6,1
            //5,6,2
            //5, 7



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

            //level 0
            tree.Root = new KarthicBTNode <int>(7);

            //level 1
            tree.Root.Left  = new KarthicBTNode <int>(5);
            tree.Root.Right = new KarthicBTNode <int>(12);

            //level 2
            tree.Root.Left.Left   = new KarthicBTNode <int>(3);
            tree.Root.Left.Right  = new KarthicBTNode <int>(6);
            tree.Root.Right.Left  = new KarthicBTNode <int>(9);
            tree.Root.Right.Right = new KarthicBTNode <int>(15);

            //level 3
            tree.Root.Left.Left.Left    = new KarthicBTNode <int>(1);
            tree.Root.Left.Left.Right   = new KarthicBTNode <int>(4);
            tree.Root.Right.Left.Left   = new KarthicBTNode <int>(8);
            tree.Root.Right.Left.Right  = new KarthicBTNode <int>(10);
            tree.Root.Right.Right.Left  = new KarthicBTNode <int>(13);
            tree.Root.Right.Right.Right = new KarthicBTNode <int>(17);

            int sum = Convert.ToInt16(this.textBox1.Text);

            List <string> output = new List <string>();

            int depth = tree.GetDepth(tree.Root);

            int[] array = new int[depth];

            //output = FindSum(tree.Root, sum, new ArrayList(), 0, output);

            output = FindSumUsingArray(tree.Root, sum, array, 0, output);


            foreach (string s in output)
            {
                this.listView1.Items.Add(s);
            }
        }
Beispiel #9
0
        public static KarthicBinaryTree <int> SetupBinarySubTree()
        {
            KarthicBinaryTree <int> bt = new KarthicBinaryTree <int>();

            bt.Root = new KarthicBTNode <int>(3);

            bt.Root.Left  = new KarthicBTNode <int>(6);
            bt.Root.Right = new KarthicBTNode <int>(7);

            return(bt);
        }
Beispiel #10
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            KarthicBinaryTree <int> tree = TreeHelper.SetUpBinaryTree();

            //In-Order Traversal
            //Traverse the binary tree in the follwing leftnode, current, right node
            StringBuilder sb = new StringBuilder();

            tree.LevelTraversal(tree.Root, sb);

            this.textBox10.Text = sb.ToString();
        }
Beispiel #11
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
        }
Beispiel #12
0
        private void button4_Click(object sender, EventArgs e)
        {
            KarthicBinaryTree <int> tree = TreeHelper.SetUpBinaryTree();

            //In-Order Traversal
            //Traverse the binary tree in the follwing leftnode, current, right node
            StringBuilder sb = new StringBuilder();

            tree.PostOrderTraversal(tree.Root, ref sb);

            this.textBox3.Text = sb.ToString();
            //Expected output: 4,5,2,6,7,3,1
        }
Beispiel #13
0
        private void button3_Click(object sender, EventArgs e)
        {
            string filepath = this.textBox5.Text;
            KarthicBinaryTree <int> tree = TreeHelper.SetUpBinaryTree();

            //we need to store the binary tree in file system/database or send across network
            //we can store either in xml, txt file or byte[] etc
            //i am going to store in txt file

            //To convert bt into string there are two methods
            //Method 1: Store both pre-order and In-order array traversal and construct tree from using both array
            //Method 2: We need to handle null value and do pre-order traversal (current, left, right) Note: In-order traversal doesn't work for this method becasue we can't figure what caused null pointer

            StringBuilder sb = new StringBuilder();

            PreOrderTraversalWithNullHandle(tree.Root, sb);

            //Method1: simple way to write string to text file
            // System.IO.File.WriteAllText(filepath, sb.ToString());

            //Method2: To convert the string to byte[]

            byte[] buffer = GetBytes(sb.ToString());
            StoreByteSIntofile(filepath, buffer);


            ///Deserailization is done here..
            //Take the byte[] array and convert into binary tree or take the file and convert into bt

            byte[] filebuffer = ConvertFileIntoByte(filepath);

            //we know that this file contains only binary tree char..convert this to string
            string output = GetString(filebuffer);

            string[] nodes = output.Split(',');

            //build tree with the string

            //KarthicBinaryTree<int> tree2 = new KarthicBinaryTree<int>();
            //tree2.Root = BuildTree(nodes, 0);

            NodeIndex test = DeserializeAndBuildTree(nodes, 0);


            StringBuilder testoutput = new StringBuilder();

            PreOrderTraversalWithNullHandle(test.treenode, testoutput);

            this.textBox6.Text = testoutput.ToString();
        }
        private void button6_Click(object sender, EventArgs e)
        {
            int[] levelarray = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox7.Text);


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

            tree.Root = BuildSpecialTreeFromLevelTraversalArray(levelarray);
            StringBuilder output = new StringBuilder();

            tree.LevelTraversal(tree.Root, output);


            bool result = String.Equals(this.textBox7.Text, output.ToString().Substring(0, output.ToString().LastIndexOf(',')), StringComparison.OrdinalIgnoreCase);
        }
        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);
        }
Beispiel #16
0
        private void button5_Click(object sender, EventArgs e)
        {
            string input = this.textBox1.Text;

            KarthicLinkedList       node = NodeHelper.GetLinkedListByString(input);
            KarthicBinaryTree <int> bt   = new KarthicBinaryTree <int>();

            bt.Root = new KarthicBTNode <int>(node.headnode.Data);

            //set 1st level
            bt.Root.Left  = new KarthicBTNode <int>(node.headnode.Next.Data);
            bt.Root.Right = new KarthicBTNode <int>(node.headnode.Next.Next.Data);

            //set 2nd level
            bt.Root.Left.Left   = new KarthicBTNode <int>(node.headnode.Next.Next.Next.Data);
            bt.Root.Left.Right  = new KarthicBTNode <int>(node.headnode.Next.Next.Next.Next.Data);
            bt.Root.Right.Left  = new KarthicBTNode <int>(node.headnode.Next.Next.Next.Next.Next.Data);
            bt.Root.Right.Right = new KarthicBTNode <int>(node.headnode.Next.Next.Next.Next.Next.Next.Data);
        }
        private void button9_Click(object sender, EventArgs e)
        {
            //http://www.geeksforgeeks.org/convert-an-arbitrary-binary-tree-to-a-tree-that-holds-children-sum-property/
            //Given Tree
            //            50
            //         /     \ 
            //       /         \
            //     7             2
            //   / \             /\
            // /     \          /   \
            //3        5      1      30

            //Each node is sum of the left child + right child
            //note: Only Incrementing the node is allowed..no decrement
            //Convert to

            //          50
            //        /    \ 
            //      /        \
            //    19           31
            //   / \           /  \
            // /     \       /      \
            //14      5     1       30


            KarthicBinaryTree <int> bt = new KarthicBinaryTree <int>();

            bt.Root = new KarthicBTNode <int>(50);

            //set level 1
            bt.Root.Left  = new KarthicBTNode <int>(7);
            bt.Root.Right = new KarthicBTNode <int>(2);

            //set level 2
            bt.Root.Left.Left  = new KarthicBTNode <int>(3);
            bt.Root.Left.Right = new KarthicBTNode <int>(5);

            bt.Root.Right.Left  = new KarthicBTNode <int>(1);
            bt.Root.Right.Right = new KarthicBTNode <int>(30);
        }
Beispiel #18
0
        private void button6_Click(object sender, EventArgs e)
        {
            /* Constructed binary tree is
             * 10
             * /   \
             * 8      2
             * /  \    /
             * 3     5  2
             */


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

            tree.Root            = new KarthicBTNode <int>(10);
            tree.Root.Left       = new KarthicBTNode <int>(8);
            tree.Root.Left.Left  = new KarthicBTNode <int>(3);
            tree.Root.Left.Right = new KarthicBTNode <int>(5);
            tree.Root.Right      = new KarthicBTNode <int>(2);
            tree.Root.Right.Left = new KarthicBTNode <int>(2);

            //            For example, in the above tree root to leaf paths exist with following sums.

            //21 –> 10 – 8 – 3
            //23 –> 10 – 8 – 5
            //14 –> 10 – 2 – 2

            //So the returned value should be true only for numbers 21, 23 and 14. For any other number, returned value should be false
            int    sum = 11;
            string output;

            if (HasPath(tree.Root, sum))
            {
                output = "There is a root-to-leaf path with sum " + sum;
            }
            else
            {
                output = "There is no root-to-leaf path with sum " + sum;
            }
        }
Beispiel #19
0
        private void button4_Click(object sender, EventArgs e)
        {
            KarthicBinaryTree <int> t1 = TreeHelper.SetUpBinaryTree();


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

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

            //Traverse through t1 for every match to the root check match tree
            string output = string.Empty;

            //t2 is substring for t1 via in order traversal
            if (ContainsTree(t1.Root, t2.Root))
            {
                output = "T2 is a subtree of T1";
            }
            else
            {
                output = "T2 is not a subtree of T1";
            }
        }
Beispiel #20
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";
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            //        26
            //      /   \
            //    10     3
            //  /    \     \
            //4      6      3

            KarthicBinaryTree <int> bt = new KarthicBinaryTree <int>();

            bt.Root = new KarthicBTNode <int>(26);

            //set level 1
            bt.Root.Left  = new KarthicBTNode <int>(10);
            bt.Root.Right = new KarthicBTNode <int>(3);

            //set level 2
            bt.Root.Left.Left  = new KarthicBTNode <int>(4);
            bt.Root.Left.Right = new KarthicBTNode <int>(6);

            bt.Root.Right.Right = new KarthicBTNode <int>(3);

            bool result = IsSumTree(bt.Root);
        }
Beispiel #22
0
        private void button5_Click_1(object sender, EventArgs e)
        {
            //In case of breath first search , we have queue implementaion.. we can make some changes to it
            KarthicBinaryTree <int> tree = TreeHelper.SetUpBinaryTree();
            //List<KarthicLinkedList> colllist = new List<KarthicLinkedList>();
            List <LinkedList <KarthicBTNode <int> > > colllist = new List <LinkedList <KarthicBTNode <int> > >();

            colllist = CreateLinkedList2(tree.Root);
            StringBuilder sb = new StringBuilder();

            foreach (LinkedList <KarthicBTNode <int> > list in colllist)
            {
                if (sb.ToString() != string.Empty)
                {
                    sb.AppendLine();
                }
                foreach (KarthicBTNode <int> value in list)
                {
                    sb.Append(value.Data).Append(',');
                }
            }

            string output = sb.ToString();
        }