/// <summary> /// successor : Minimum value in a right sub tree from a given node x e.g X here ///if right tree is not there then, /// successor is furtherst parent of x that you get following right branches /// / <------Successor /// / /// / /// / /// X /// \ No right Sub Tree /// </summary> /// <param name="root">Tree</param> /// <param name="x">X node for which we find Successor</param> /// <returns></returns> public bnode<int> InOrderSuccesor(bnode<int> root, bnode<int> x) { //If right sub tree exist from node x node, //then find minimum value in right sub tree which is succ if (x.right != null) return Min(x.right); bnode<int> suc = null; //If right tree doens't exist, do the following while (root != null) { if (x.data < root.data) { suc = root; root = root.left; } else if (x.data > root.data) { root = root.right; } else break; } return suc; }
public void InorderTraversalUsingStack( bnode<int> node) { Stack<bnode<int>> s = new Stack<bnode<int>>(); while (node !=null) { while (node != null) { if (node.right != null) s.Push(node.right); s.Push(node); node = node.left; } bnode<int> p = s.Pop();// visit left child First while(p.right == null && s.Count!=0) { Console.WriteLine(p.data); //visit middle (root) node p = s.Pop(); } // you are visiting middle node any case // weather it has right child or not Console.WriteLine(p.data); //visit middle (root) node //if right child exist go for iteration if (s.Count!= 0) node = s.Pop(); // pop right child and now iterat again else node = null; } }
//traverse right only public void printrightEdge(bnode<int> root) { if (root == null) return; //this codition doesn't print the leaf node if (root.right != null) { Console.WriteLine(root.data); } printrightEdge(root.right); }
//To print leaf node is simply a in-order traversal //but print the visit node which has left and right node=null public void printleafnodes(bnode<int> root) { if (root == null) return; printleafnodes(root.left); if (root.right == null && root.left == null) { Console.WriteLine(root.data); } printleafnodes(root.right); }
//Do in order traversal and print kth element by checking condition public void inorder(bnode<int> root) { bnode<int> cur = root; if (cur == null) return; inorder(cur.left); i++; if (i == k) Console.WriteLine(cur.data); inorder(cur.right); }
public void fixprevPointer(bnode<int> root) { //Fix the left pointer first //root is current node if (root == null) return; fixprevPointer(root.left); root.left = prev; prev = root; fixprevPointer(root.right); }
public void PathRootToleafRec(bnode<int> node, int length) { if (node == null) return; Path[length] = node; length++; if (node.left == null && node.right == null) print(length); PathRootToleafRec(node.left, length); PathRootToleafRec(node.right, length); }
//Tree1 is a big tree and Tree2 is a Sub Tree //Steps //1. For each Tree1 node // you check Tree2 is subTree ( identical ) //And Identical Tree means every node of Tree1 must match with every node of Tree2 // public Boolean FindSubTree(bnode<int> Tree1, bnode<int> Tree2) { //When traversing through Tree1 you never return with if (Tree1 == null) return false; //if Tree2 is itself null means it is sub tree if (Tree2 == null) return true; if(IsIdentical(Tree1, Tree2)) return true; //if you don't find sub tree with Tree1's root, try left and Try right subTrees // one by one. return (FindSubTree(Tree1.left, Tree2) || FindSubTree(Tree1.right, Tree2)); }
// public void fixNextPointer(bnode<int> root) { //reach the right most node, which is last node in ddl while (root != null && root.right != null) root = root.right; //iterate through left pointer that we set previosly //in that aslo while (root != null && root.left!=null) { prev = root; root = root.left;//next root.right = prev; //next points to prev } }
private static bstring add_bstring(bstring a, bstring b) { int n = (a.length > b.length) ? a.length : b.length; bstring result = new bstring(n + 1); char carry = '0'; for (int i = 0; i < n; i++) { bnode badd_result = badd(a[i], b[i], carry); result[i] = badd_result.r; carry = badd_result.carry; } result[n] = carry; return(result); }
public Boolean FindSumPath(bnode<int> curNode, int sum) { //every time we substract current node with sum value // at the end sum == 0 then we found the sum path if (curNode==null && sum == 0) return true; if (curNode == null) return false; int subsum = sum - curNode.data; if( curNode.left == null && curNode.right==null && subsum ==0) return true; return FindSumPath(curNode.left, subsum) || FindSumPath (curNode.right, subsum); }
public Boolean IsIdentical(bnode<int> Tree1, bnode<int> Tree2) { //base case both null or terminate condition if (Tree1 == null && Tree2 == null) return true; //Either of Two Tree reached null that means root2 is not sub tree // becuase both should end with null togather, otherwise return false if (Tree1 ==null || Tree2 == null ) return false; //In Traverse, you find that if data doesn't match, then //Simply return false, because every node of Tree1 must match with every node of Tree2 if (Tree1.data != Tree2.data) return false; // Traverse In-order return (IsIdentical(Tree1.left, Tree2.left) && IsIdentical(Tree1.right, Tree2.right)); }
public Boolean IsBinarySearchTree(bnode<int> root) { if (root == null) return false; if (!IsBinarySearchTree(root.left)) return false; if (prev != null && prev.data > root.data) return false; //save the previous node to comare with current prev = root; if (!IsBinarySearchTree(root.right)) return false; return true; }
public int MinDepthBinaryTree(bnode<int> root) { //base if (root == null) return depth; if (root.left == null || root.right == null) return depth; depth++; int lefth = MinDepthBinaryTree(root.left); int righth = MinDepthBinaryTree(root.right); if (lefth > righth) { return righth; } else { return lefth; } }
/// <summary> /// opposite of successor /// find node with maximum value in a left sub tree from x /// if left sub tree doens't exist then do the opposite thing but /// same way that i did for successor /// \ <---Predecessor /// \ /// \ /// \ /// X //no left tree / /// </summary> /// <param name="root"></param> /// <param name="x">X node for which we find Predecessor</param> /// <returns></returns> public bnode<int> InOrderPredecessor(bnode<int> root, bnode<int> x) { if (x.left != null) return Max(x.left); bnode<int> pred = null; while (root != null) { if (x.data < root.data) { root = root.left; } else if (x.data > root.data) { pred = root; root = root.right; } else { break; } } return pred; }
//Minimum value always deep right side of the tree // you can do this prog using while loop public bnode<int> Max(bnode<int> root) { if (root.right != null) Max(root.right); return root; }
//private int PathLength = 0; public void PrintPathRootToLeaf(bnode<int> root) { Path = new bnode<int>[1000]; PathRootToleafRec(root, 0); }
//Minimum value always deep left side of the tree // you can do this prog using while loop public bnode<int> Min(bnode<int> root) { if (root.left != null) Min(root.left); return root; }
public void ConvertB2DDL(bnode<int> root) { fixprevPointer(root); fixNextPointer(root); }
//print outermost edge of a Binary Tree //root is printing 2 times for left edge and for right edge public void printouterMostEdge(bnode<int> root) { printleftEdge(root); printleafnodes(root); printrightEdge(root); }