public override string ToString() { string result = ""; int height = GetHeight (this); Console.WriteLine ("height = " + height); BTNode dummy = new BTNode (-1); List<BTNode> curLevel = new List<BTNode>(); curLevel.Add (this); for (int i = 0; i < height; ++i) { string indent = new string ('\t', (int)Math.Pow (2, height - i - 1)); string sep = new string ('\t', (int)Math.Pow (2, height - i)); result += indent; List<BTNode> nextLevel = new List<BTNode> (); foreach (BTNode n in curLevel) { if (n == dummy) result += sep; else result += n.data.ToString() + sep; nextLevel.Add (n.left ?? dummy); nextLevel.Add (n.right ?? dummy); } result += '\n'; curLevel = nextLevel; } return result; }
private int GetHeight(BTNode node) { if (node == null) return 0; int leftHeight = GetHeight (node.left); int rightHeight = GetHeight (node.right); return 1 + Math.Max (leftHeight, rightHeight); }
private static bool ValidateBST(BTNode n, int min, int max) { if (n == null) return true; return (n.data >= min && n.data <= max && ValidateBST (n.left, min, n.data) && ValidateBST (n.right, n.data + 1, max)); }
private static bool Match(BTNode t1, BTNode t2) { if (t1 == null) return (t2 == null); if (t2 == null || t1.data != t2.data) return false; return Match (t1.left, t2.left) && Match (t1.right, t2.right); }
// O(n) runtime public static int CountPaths(BTNode t, int sum) { Dictionary<int, int> h = new Dictionary<int, int> (); IncrementHashCount (h, 0); int total = CountPaths (t, sum, 0, h); if (sum == 0) // special case, do not include initial 0 count (don't include empty paths) --total; return total; }
private static BTNode CreateMinimalBST(int[] arr, int left, int right) { if (left > right) return null; int mid = left + (right - left) / 2; BTNode n = new BTNode (arr [mid]); n.left = CreateMinimalBST (arr, left, mid - 1); n.right = CreateMinimalBST (arr, mid + 1, right); return n; }
private static void GetDepthListsDepthFirst(BTNode t, int depth, List<LinkedList<BTNode>> list) { if (t == null) return; if (list.Count < depth + 1) list.Add(new LinkedList<BTNode> ()); list [depth].AddLast (t); GetDepthListsDepthFirst (t.left, depth + 1, list); GetDepthListsDepthFirst (t.right, depth + 1, list); }
private static int GetHeight(BTNode n) { if (n == null) return 0; int left = GetHeight (n.left); if (left == -1) return -1; int right = GetHeight (n.right); if (right == -1) return -1; if (Math.Abs(left - right) > 1) return -1; return Math.Max(left, right) + 1; }
public static bool IsSubtree(BTNode t1, BTNode t2) { if (t1 == null) return false; if (t2 == null) return true; if (t1.data == t2.data && Match (t1, t2)) return true; else return IsSubtree (t1.left, t2) || IsSubtree (t1.right, t2); }
// This only works if all nodes are distinct public static bool IsSubtreeDistinct(BTNode t1, BTNode t2) { if (t1 == null) return t2 == null; if (t2 == null) return true; if (t1.data != t2.data) { return IsSubtreeDistinct (t1.left, t2) || IsSubtreeDistinct (t1.right, t2); } else { return IsSubtreeDistinct (t1.left, t2.left) && IsSubtreeDistinct (t1.right, t2.right); } }
// O(n^2) runtime public static int CountPathsSimple(BTNode t, int sum) { if (t == null) return 0; int preCount = 0; if (t.data == sum) preCount = 1; return preCount + CountPaths (t.left, sum - t.data) + CountPaths (t.right, sum - t.data) + CountPaths (t.left, sum) + CountPaths (t.right, sum); }
public static void RunTests() { BTNode t = Q4_2.CreateMinimalBST (new int[] { 1, 3, 4, 5, 6, 7, 10, 12, 15, 16, 17, 20, 50, 75, 100 }); Console.WriteLine (t); Console.WriteLine (IsBalanced(t)); t = new BTNode (1); t.left = new BTNode (2); Console.WriteLine (IsBalanced(t)); t.left.right = new BTNode (3); Console.WriteLine (IsBalanced(t)); t.right = new BTNode (4); Console.WriteLine (IsBalanced(t)); }
public static void RunTests() { BTNode t = Q4_2.CreateMinimalBST (new int[] { 1, 3, 4, 5, 6, 7, 10, 12, 15, 16, 17, 20, 50, 75, 100 }); Console.WriteLine (t); Console.WriteLine (IsBinarySearchTree(t)); t.left.data = 16; Console.WriteLine (IsBinarySearchTree(t)); t = new BTNode (0); t.left = new BTNode (-1); Console.WriteLine (IsBinarySearchTree(t)); t.left.data = 0; Console.WriteLine (IsBinarySearchTree(t)); t.right = new BTNode (-1); Console.WriteLine (IsBinarySearchTree(t)); }
public static BTNode GetInorderSuccessor(BTNode n) { if (n == null) return null; if (n.right != null) { n = n.right; while (n.left != null) n = n.left; return n; } else { while (n.parent != null) { if (n.parent.left == n) return n.parent; n = n.parent; } return null; } }
public static List<LinkedList<BTNode>> GetDepthListsBreadthFirst(BTNode t) { List<LinkedList<BTNode>> result = new List<LinkedList<BTNode>> (); LinkedList<BTNode> current = new LinkedList<BTNode> (); current.AddLast (t); while (current.Count > 0) { result.Add (current); LinkedList<BTNode> parents = current; current = new LinkedList<BTNode> (); var e = parents.GetEnumerator (); while (e.MoveNext ()) { if (e.Current.left != null) current.AddLast (e.Current.left); if (e.Current.right != null) current.AddLast (e.Current.right); } } return result; }
private static int CountPaths(BTNode t, int target, int running, Dictionary<int, int> h) { if (t == null) return 0; running += t.data; IncrementHashCount (h, running); int keySubPaths = running - target; int totalPaths = h.ContainsKey (keySubPaths) ? h[keySubPaths] : 0; if (target == 0) // special case, do not include increment from this iteration --totalPaths; totalPaths += CountPaths (t.left, target, running, h); totalPaths += CountPaths (t.right, target, running, h); // revert changes to traverse different paths --h[running]; return totalPaths; }
private static BTNode Count(BTNode n, BTNode a, BTNode b, out int c) { if (n == null) { c = 0; return null; } int left = 0; BTNode leftFCA = Count (n.left, a, b, out left); int right = 0; BTNode rightFCA = Count (n.right, a, b, out right); c = left + right; if (n == a || n == b) { c += 1; return n; } if (left == 1 && right == 1) return n; return leftFCA ?? rightFCA; }
public static List<List<int>> GetArrays(BTNode t) { List<List<int>> all = new List<List<int>> (); if (t == null) { all.Add (new List<int> ()); return all; } List<int> prefix = new List<int> (); prefix.Add (t.data); List<List<int>> leftLists = GetArrays (t.left); List<List<int>> rightLists = GetArrays (t.right); foreach (var leftList in leftLists) { foreach (var rightList in rightLists) { List<List<int>> weavedLists = new List<List<int>> (); Weave (leftList, rightList, weavedLists, prefix); all.AddRange (weavedLists); } } return all; }
public BTNode(int data) { this.data = data; this.parent = null; }
public static bool IsBinarySearchTree(BTNode t) { return ValidateBST (t, int.MinValue, int.MaxValue); }
public static List<LinkedList<BTNode>> GetDepthListsDepthFirst(BTNode t) { List<LinkedList<BTNode>> list = new List<LinkedList<BTNode>> (); GetDepthListsDepthFirst (t, 0, list); return list; }
public static BTNode FindFCA(BTNode n, BTNode a, BTNode b) { int c = 0; BTNode fca = Count (n, a, b, out c); return (c == 2 ? fca : null); }
public static bool IsBalanced(BTNode t) { return GetHeight (t) > 0; }