public static KarthicBST <int> SetUpNonBinarySearchTree() { KarthicBST <int> bt = new KarthicBST <int>(); bt.Root = new KarthicBTNode <int>(8); //set level 1 bt.Root.Left = new KarthicBTNode <int>(3); bt.Root.Right = new KarthicBTNode <int>(10); //set level 2 bt.Root.Left.Left = new KarthicBTNode <int>(1); bt.Root.Left.Right = new KarthicBTNode <int>(6); bt.Root.Left.Right.Left = new KarthicBTNode <int>(4); bt.Root.Left.Right.Right = new KarthicBTNode <int>(7); bt.Root.Right.Right = new KarthicBTNode <int>(14); bt.Root.Right.Right.Left = new KarthicBTNode <int>(13); //making non binary tree bt.Root.Right.Right.Left.Right = new KarthicBTNode <int>(11); ////making the tree unbalaced //bt.Root.Left.Left.Left.Left = new KarthicBTNode<int>(9); return(bt); }
private void button9_Click(object sender, EventArgs e) { KarthicBST <int> tree = TreeHelper.SetUpBinarySearchTree(); StringBuilder sb = new StringBuilder(); tree.InOrderTraversal(tree.Root, ref sb); string check = sb.ToString(); //Given the node of a bst, find the next node of the given node via in-order traversal //In-order traversal left, current and right //pseudocode // public Node Inordersuccessor(node) /// if(node has right subtree) /// return (left most node of right subtree) /// else /// while (n is right of n.parent) /// n = n.parent /// return n.parent /// KarthicBTNode <int> next = tree.NextInOrderSuccessor(tree.Find(10, tree.Root)); }
public static KarthicBST <int> SetUpBinarySearchTree() { KarthicBST <int> btree = new KarthicBST <int>(); btree.Root = new KarthicBTNode <int>(8); btree.Insert(3, btree.Root); btree.Insert(10, btree.Root); btree.Insert(1, btree.Root); btree.Insert(6, btree.Root); btree.Insert(4, btree.Root); btree.Insert(7, btree.Root); btree.Insert(14, btree.Root); btree.Insert(13, btree.Root); //bt.Root = new KarthicBTNode<int>(8); ////set level 1 //bt.Root.Left = new KarthicBTNode<int>(3); //bt.Root.Right = new KarthicBTNode<int>(10); ////set level 2 //bt.Root.Left.Left = new KarthicBTNode<int>(1); //bt.Root.Left.Right = new KarthicBTNode<int>(6); //bt.Root.Left.Right.Left = new KarthicBTNode<int>(4); //bt.Root.Left.Right.Right = new KarthicBTNode<int>(7); //bt.Root.Right.Right = new KarthicBTNode<int>(14); //bt.Root.Right.Right.Left = new KarthicBTNode<int>(13); ////making the tree unbalaced //bt.Root.Left.Left.Left.Left = new KarthicBTNode<int>(9); return(btree); }
private void button8_Click(object sender, EventArgs e) { //Things to ask interviewer //1) Is it BST OR BINARY tree?. If it is bst we can use the value and compare //bst code KarthicBST <int> tree = TreeHelper.SetUpBinarySearchTree(); StringBuilder sb = new StringBuilder(); tree.InOrderTraversal(tree.Root, ref sb); string check = sb.ToString(); KarthicBTNode <int> node1 = tree.Find(4, tree.Root); KarthicBTNode <int> node2 = tree.Find(7, tree.Root); KarthicBTNode <int> root = tree.Root; KarthicBTNode <int> result2 = tree.FindFirstCommonAncestorForOnlyBST(tree.Root, tree.Find(4, tree.Root), tree.Find(103, tree.Root)); //2) Is the parent node is given... //If the parent node is given and addition datastructure is allowed //Take one node and mark all the path visited as true from root..either modify tree node struc or use ht //Take another node and keep track of last visited..when we reach first non-visited path then last visited will be ancestor //if the parent node is goven and addition datasturc not allowed }
private void button2_Click(object sender, EventArgs e) { int[] array = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox1.Text); KarthicBST <int> tree = new KarthicBST <int>(); tree.Root = ConstructTreeusingStacks(array); string output = tree.PreOrderTraversal(tree.Root); bool result = String.Equals(this.textBox1.Text, output.Substring(0, output.LastIndexOf(',')), StringComparison.OrdinalIgnoreCase); //test KarthicBST <int> tree1 = TreeHelper.SetUpBinarySearchTree(); string test1 = tree1.PreOrderTraversal(tree1.Root); int[] input2 = AlgorithmHelper.ConvertCommaSeparetedStringToInt(test1); KarthicBST <int> tree3 = new KarthicBST <int>(); tree3.Root = ConstructTreeusingStacks(input2); string test2 = tree3.PreOrderTraversal(tree3.Root); bool result2 = String.Equals(test1, test2, StringComparison.OrdinalIgnoreCase); }
private void button8_Click(object sender, EventArgs e) { //This is an optimized way to check a tree is bst or not //logic during iteration make sure every node satisfies the bst definition and also check this case //**********20********* //********10***30****** //**********25*********** //Though the 25 is on the right of the node 10..it is still larger than 20 (root) //so during every iteration we need to maintain the min and max of every node KarthicBST <int> tree = TreeHelper.SetUpNonBinarySearchTree(); StringBuilder sb = new StringBuilder(); tree.InOrderTraversal(tree.Root, ref sb); string check = sb.ToString(); //int[] array = new int[9]; //InOrderTraversalResult(tree.Root, array, 0); //If it is BST, the resulting array should in sorted order string output = tree.IsBSTOptimalSolution(tree.Root) ? "Tree is BST" : "Tree is not BST"; }
private void button7_Click(object sender, EventArgs e) { KarthicBST <int> tree = TreeHelper.SetUpBinarySearchTree(); StringBuilder sb = new StringBuilder(); tree.MorrisInOrderTraversal(tree.Root, sb); string output = sb.ToString(); }
private void button1_Click(object sender, EventArgs e) { int k = Convert.ToInt16(this.textBox2.Text); KarthicBST <int> tree = TreeHelper.SetUpBinarySearchTree(); this.textBox3.Text = FindkthSmallestNodeByChildNo(tree.Root, k).Data.ToString(); StringBuilder sb = new StringBuilder(); tree.InOrderTraversal(tree.Root, ref sb); }
private void button3_Click(object sender, EventArgs e) { int[] array = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox1.Text); KarthicBST <int> tree = new KarthicBST <int>(); tree.Root = ConstructTreeWithMinAndMax(array, 0, Int32.MinValue, Int32.MaxValue, array[0]).treenode; string output = tree.PreOrderTraversal(tree.Root); bool result = String.Equals(this.textBox1.Text, output.Substring(0, output.LastIndexOf(',')), StringComparison.OrdinalIgnoreCase); }
private void button1_Click(object sender, EventArgs e) { int[] array = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox1.Text); KarthicBST <int> tree = new KarthicBST <int>(); tree.Root = BuildTreeWithPreOrderArray(array, 0, 0, array.Length - 1).treenode; string output = tree.PreOrderTraversal(tree.Root); bool result = String.Equals(this.textBox1.Text, output.Substring(0, output.LastIndexOf(',')), StringComparison.OrdinalIgnoreCase); }
private void button10_Click(object sender, EventArgs e) { KarthicBST <int> tree = TreeHelper.SetUpBinarySearchTree(); int input = Convert.ToInt32(this.textBox4.Text); //int input = KarthicBTNode <int> result = tree.Find(input, tree.Root); //KarthicBTNode<int> result = tree.Find2(input, tree.Root); this.textBox6.Text = (result != null) ? "true" : "false"; }
private void button2_Click(object sender, EventArgs e) { int k = Convert.ToInt16(this.textBox2.Text); KarthicBST <int> tree = TreeHelper.SetUpBinarySearchTree(); Stack <KarthicBTNode <int> > mystack = new Stack <KarthicBTNode <int> >(); this.textBox3.Text = FindkthsmallestnodebyInordertraversal(tree.Root, k, mystack).node.Data.ToString(); StringBuilder sb = new StringBuilder(); tree.InOrderTraversal(tree.Root, ref sb); }
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 }
private void button9_Click(object sender, EventArgs e) { //KarthicBinaryTree<int> tree = TreeHelper.SetUpBinaryTree(); KarthicBST <int> tree = TreeHelper.SetUpBinarySearchTree(); StringBuilder sb = new StringBuilder(); tree.InOrderTraversal(tree.Root, ref sb); string check = sb.ToString(); KarthicBTNode <int> node = tree.FindFirstCommonAncestor(tree.Root, tree.Find(1, tree.Root), tree.Find(6, tree.Root)); KarthicBTNode <int> node1 = tree.FindFirstCommonAncestor(tree.Root, tree.Find(4, tree.Root), tree.Find(7, tree.Root)); KarthicBTNode <int> node2 = tree.FindFirstCommonAncestor(tree.Root, tree.Find(4, tree.Root), tree.Find(14, tree.Root)); }
private void button7_Click(object sender, EventArgs e) { KarthicBST <int> tree = TreeHelper.SetUpBinarySearchTree(); StringBuilder sb = new StringBuilder(); tree.InOrderTraversal(tree.Root, ref sb); string check = sb.ToString(); KarthicBTNode <int> node1 = tree.Find(4, tree.Root); KarthicBTNode <int> node2 = tree.Find(7, tree.Root); KarthicBTNode <int> root = tree.Root; KarthicBTNode <int> result1 = tree.FindFirstCommonAncestorUsingParent(root, null, null); KarthicBTNode <int> result2 = tree.FindFirstCommonAncestorUsingParent(tree.Root, tree.Find(4, tree.Root), tree.Find(103, tree.Root)); }
private void button2_Click(object sender, EventArgs e) { //Given an sorted array..we need to create a binary tree with minimal height.. //we can't add node to binary tree by iterating through array bcoz the height will grow on left or right side int[] array = { 11, 22, 33, 44, 55, 66, 77 }; int length = array.Length; KarthicBST <int> tree = new KarthicBST <int>(); tree.Root = CreateBinarySearchTreeWithMinimalHeight(array); int height = tree.GetHeight(tree.Root); StringBuilder sb = new StringBuilder(); tree.InOrderTraversal(tree.Root, ref sb); }
private void button6_Click(object sender, EventArgs e) { KarthicBST <int> tree = TreeHelper.SetUpBinarySearchTree(); //Test In-Order Traversal //Traverse the binary tree in the follwing leftnode, current, right node StringBuilder sb = new StringBuilder(); tree.InOrderTraversal(tree.Root, ref sb); string test = sb.ToString(); //Expected output: 1,3,4,6,7,8,10,13,14 int value = Convert.ToInt16(this.textBox5.Text); //Insert a node value of 20 KarthicBST <int> newtree = new KarthicBST <int>(); newtree.Root = tree.Insert(value, tree.Root); StringBuilder sb2 = new StringBuilder(); newtree.InOrderTraversal(tree.Root, ref sb2); this.textBox7.Text = sb2.ToString(); //Expected output: 1,3,4,6,7,8,10,13,14,20 }
private void button7_Click(object sender, EventArgs e) { //To check the given tree is bst, If we use in-order sort we will get all the data in ascending order.. //put the result in an array and check whether the array is sorted.. //limitation: This won't work for duplicate records example below //************25**** //***********20***** //*********20********** //*************25 //***********20 //*************20 //The resulting array for both case will be 25 20 20..but the second case is invalid //so array sort will work only for non duplicate values KarthicBST <int> tree = TreeHelper.SetUpNonBinarySearchTree(); StringBuilder sb = new StringBuilder(); tree.InOrderTraversal(tree.Root, ref sb); string check = sb.ToString(); //int[] array = new int[9]; //InOrderTraversalResult(tree.Root, array, 0); //If it is BST, the resulting array should in sorted order string output = tree.IsBinarySearchTree() ? "Tree is BST" : "Tree is not BST"; }