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 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)); }
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 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 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"; }