예제 #1
0
파일: BTNode.cs 프로젝트: slao-learn/ctci
 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;
 }
예제 #2
0
파일: BTNode.cs 프로젝트: slao-learn/ctci
 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);
 }
예제 #3
0
파일: Q4_5.cs 프로젝트: slao-learn/ctci
 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));
 }
예제 #4
0
파일: Q4_10.cs 프로젝트: slao-learn/ctci
 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);
 }
예제 #5
0
파일: Q4_12.cs 프로젝트: slao-learn/ctci
 // 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;
 }
예제 #6
0
파일: Q4_2.cs 프로젝트: slao-learn/ctci
 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;
 }
예제 #7
0
파일: Q4_3.cs 프로젝트: slao-learn/ctci
 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);
 }
예제 #8
0
파일: Q4_4.cs 프로젝트: slao-learn/ctci
 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;
 }
예제 #9
0
파일: Q4_10.cs 프로젝트: slao-learn/ctci
        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);
        }
예제 #10
0
파일: Q4_10.cs 프로젝트: slao-learn/ctci
        // 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);
            }
        }
예제 #11
0
파일: Q4_12.cs 프로젝트: slao-learn/ctci
 // 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);
 }
예제 #12
0
파일: Q4_4.cs 프로젝트: slao-learn/ctci
        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));
        }
예제 #13
0
파일: Q4_5.cs 프로젝트: slao-learn/ctci
 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));
 }
예제 #14
0
파일: Q4_6.cs 프로젝트: slao-learn/ctci
 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;
     }
 }
예제 #15
0
파일: Q4_3.cs 프로젝트: slao-learn/ctci
 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;
 }
예제 #16
0
파일: Q4_12.cs 프로젝트: slao-learn/ctci
        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;
        }
예제 #17
0
파일: Q4_8.cs 프로젝트: slao-learn/ctci
        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;
        }
예제 #18
0
파일: Q4_9.cs 프로젝트: slao-learn/ctci
        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;
        }
예제 #19
0
파일: BTNode.cs 프로젝트: slao-learn/ctci
 public BTNode(int data)
 {
     this.data = data;
     this.parent = null;
 }
예제 #20
0
파일: Q4_5.cs 프로젝트: slao-learn/ctci
 public static bool IsBinarySearchTree(BTNode t)
 {
     return ValidateBST (t, int.MinValue, int.MaxValue);
 }
예제 #21
0
파일: Q4_3.cs 프로젝트: slao-learn/ctci
 public static List<LinkedList<BTNode>> GetDepthListsDepthFirst(BTNode t)
 {
     List<LinkedList<BTNode>> list = new List<LinkedList<BTNode>> ();
     GetDepthListsDepthFirst (t, 0, list);
     return list;
 }
예제 #22
0
파일: Q4_8.cs 프로젝트: slao-learn/ctci
 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);
 }
예제 #23
0
파일: Q4_4.cs 프로젝트: slao-learn/ctci
 public static bool IsBalanced(BTNode t)
 {
     return GetHeight (t) > 0;
 }