Esempio n. 1
0
 public static void mirrorTree(TreeAndGraph.BinaryTreeNode root)
 {
     if (root == null)
     {
         return;
     }
     if (root.LeftNode != null && root.RightNode !=null)
     {
         TreeAndGraph.BinaryTreeNode temp = root.LeftNode;
         root.LeftNode = root.RightNode;
         root.RightNode = temp;
         mirrorTree(root.LeftNode);
         mirrorTree(root.RightNode);
     }
     else if(root.LeftNode!=null)
     {
         root.LeftNode = root.RightNode;
         mirrorTree(root.LeftNode);
     }
     else if (root.RightNode!=null)
     {
         root.RightNode = root.LeftNode;
         mirrorTree(root.RightNode);
     }
     else
     {
         return;
     }
 }
 public int minDepth2(TreeAndGraph.BinaryTreeNode root)
 {
     Queue<TreeAndGraph.BinaryTreeNode> q = new Queue<BinaryTreeNode>();
     q.Enqueue(root);
     root.state = 1;
     while (q.Count != 0)
     {
         BinaryTreeNode node = q.Dequeue();
         if (node.LeftNode == null && node.RightNode == null)
         {
             return node.state;
         }
         else
         {
             if (node.LeftNode != null)
             {
                 node.LeftNode.state = node.state + 1;
                 q.Enqueue(node.LeftNode);
             }
             if (node.RightNode != null)
             {
                 node.RightNode.state = node.state + 1;
                 q.Enqueue(node.RightNode);
             }
         }
     }
     return -1;
 }
        public List<double> inorder(TreeAndGraph.BinaryTreeNode root)
        {
            List<double> list = new List<double>();
            Stack<TreeAndGraph.BinaryTreeNode> s = new Stack<BinaryTreeNode>();
            s.Push(root);
            while (s.Count != 0)
            {
                while (s.Peek().LeftNode != null)
                {
                    s.Push(s.Peek().LeftNode);
                }

                do
                {
                    TreeAndGraph.BinaryTreeNode node = s.Pop();
                    list.Add(node.Data);
                    if (node.RightNode != null)
                    {
                        s.Push(node.RightNode);
                        break;
                    }
                } while (s.Count != 0);

            }
            return list;
        }
Esempio n. 4
0
        //also reference to Algorithm.TreeAndGraph.Floyd and Algorithm.TreeAndGraph.Dijkstra
        public static int getShortestDistance(TreeAndGraph.GraphNode start, TreeAndGraph.GraphNode end)
        {
            System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<TreeAndGraph.GraphNode>> tab
                = new System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<TreeAndGraph.GraphNode>>();
            System.Collections.Generic.List<TreeAndGraph.GraphNode> list = new System.Collections.Generic.List<TreeAndGraph.GraphNode>();
            int count = 1;
            list.Add(start);
            tab.Add(count, list);
            while (true)
            {
                System.Collections.Generic.List<TreeAndGraph.GraphNode> gn_list = tab[count];
                ++count;
                if (!tab.ContainsKey(count))
                {
                    list = new System.Collections.Generic.List<TreeAndGraph.GraphNode>();
                    tab.Add(count, list);
                }
                foreach (TreeAndGraph.GraphNode gn in gn_list)
                {

                    foreach (TreeAndGraph.GraphNode node in gn.Nodes)
                    {
                        if (node == end)
                        {
                            return count;
                        }

                        tab[count].Add(node);
                    }
                }
            }
        }
Esempio n. 5
0
        public static void interateBinaryTree(TreeAndGraph.BinaryTreeNode head, Order o)
        {
            if (head == null)
            {
                return;
            }

            if (o == Order.Pre)
            {
                System.Console.WriteLine("Pre:" + head.Data);
            }

            interateBinaryTree(head.LeftNode, o);

            if (o == Order.Mid)
            {
                System.Console.WriteLine("Mid:" + head.Data);
            }

            interateBinaryTree(head.RightNode, o);

            if (o == Order.Post)
            {
                System.Console.WriteLine("Post:" + head.Data);
            }
        }
 public int maxDepth2(TreeAndGraph.BinaryTreeNode root)
 {
     if (root == null)
     {
         return 0;
     }
     return Math.Max(maxDepth2(root.LeftNode), maxDepth2(root.RightNode)) + (int)root.Data;
 }
Esempio n. 7
0
 public List<List<double>> pathSum(TreeAndGraph.BinaryTreeNode root, int sum)
 {
     List<List<double>> solution = new List<List<double>>();
     int height = root.getHeight();
     count = new double[height];
     PreDPS(root, solution, sum, count, 0);
     return solution;
 }
 public double minDepth(TreeAndGraph.BinaryTreeNode root)
 {
     if (root == null)
     {
         return 0;
     }
     return Math.Min(minDepth(root.LeftNode), minDepth(root.RightNode)) + root.Data;
 }
Esempio n. 9
0
        public static int countMaxDistanceHelper(TreeAndGraph.BinaryTreeNode root)
        {
            if (root == null)
            {
                return 0;
            }

            return 1 + System.Math.Abs(countMaxDistanceHelper(root.LeftNode) - countMaxDistanceHelper(root.RightNode));
        }
Esempio n. 10
0
 public bool verify(TreeAndGraph.BinaryTreeNode t1, TreeAndGraph.BinaryTreeNode t2)
 {
     if (t1 == null && t2 == null)
     {
         return true;
     }else if (t1 == null || t2 == null ||t1.Data != t2.Data)
     {
         return false;
     }
     return verify(t1.LeftNode, t2.LeftNode) && verify(t1.RightNode, t2.RightNode);
 }
Esempio n. 11
0
 bool DFS(TreeAndGraph.BinaryTreeNode left, TreeAndGraph.BinaryTreeNode right)
 {
     if (left == null && right == null)
     {
         return true;
     }
     else if (left == null || right == null || left.Data != right.Data)
     {
         return false;
     }
     return DFS(left.LeftNode, right.RightNode) && DFS(left.RightNode, right.LeftNode);
 }
 public bool validate(TreeAndGraph.BinaryTreeNode root, double min = double.MinValue , double max = double.MaxValue)
 {
     if (root == null)
     {
         return true;
     }
     if (root.Data < min || root.Data > max)
     {
         return false;
     }
     return validate(root.LeftNode, min, root.Data) && validate(root.RightNode, root.Data, max);
 }
Esempio n. 13
0
        private static void PutArr(TreeAndGraph.BinaryTreeNode root, double[] arr)
        {
            if (root == null)
            {
                return;
            }

            PutArr(root.LeftNode, arr);

            arr[index++] = root.Data;

            PutArr(root.RightNode, arr);
        }
 private Wrapper DFS(TreeAndGraph.BinaryTreeNode head)
 {
     Wrapper wr = new Wrapper();
     if (head == null)
     {
         return wr;
     }
     Wrapper l = DFS(head.LeftNode);
     Wrapper r = DFS(head.RightNode);
     wr.BranchLen = Math.Max(l.BranchLen, r.BranchLen) + (int)head.Data;
     wr.LoopLen = Math.Max(Math.Max(l.LoopLen, r.LoopLen), l.BranchLen + r.BranchLen + (int)head.Data);
     return wr;
 }
 public void recover(TreeAndGraph.BinaryTreeNode root)
 {
     last = null;
     forward(root);
     last = null;
     backward(root);
     if (f != b)
     {
         double temp = f.Data;
         f.Data = b.Data;
         b.Data = temp;
     }
 }
 void DFS(List<List<double>> solution, TreeAndGraph.BinaryTreeNode root, int level)
 {
     if (root == null)
     {
         return;
     }
     if (solution.Count <= level)
     {
         solution.Add(new List<double>());
     }
     solution[level].Add(root.Data);
     DFS(solution, root.LeftNode, level + 1);
     DFS(solution, root.RightNode, level + 1);
 }
Esempio n. 17
0
        //revision: clear 12/1/2012
        /*
                     TreeAndGraph.BinaryTreeNode btn10 = new TreeAndGraph.BinaryTreeNode(10);
            TreeAndGraph.BinaryTreeNode btn6 = new TreeAndGraph.BinaryTreeNode(6);
            TreeAndGraph.BinaryTreeNode btn14 = new TreeAndGraph.BinaryTreeNode(14);
            TreeAndGraph.BinaryTreeNode btn4 = new TreeAndGraph.BinaryTreeNode(4);
            TreeAndGraph.BinaryTreeNode btn8 = new TreeAndGraph.BinaryTreeNode(8);
            TreeAndGraph.BinaryTreeNode btn12 = new TreeAndGraph.BinaryTreeNode(12);
            TreeAndGraph.BinaryTreeNode btn16 = new TreeAndGraph.BinaryTreeNode(16);
            btn10.LeftNode = btn6; btn10.RightNode = btn14; btn6.LeftNode = btn4; btn6.RightNode = btn8; btn14.LeftNode = btn12; btn14.RightNode = btn16;
            swapTree(btn10);
         */
        public static void swapTree(TreeAndGraph.BinaryTreeNode node)
        {
            if (node == null)
            {
                return;
            }

            TreeAndGraph.BinaryTreeNode temp = node.LeftNode;
            node.LeftNode = node.RightNode;
            node.RightNode = temp;

            swapTree(node.LeftNode);
            swapTree(node.RightNode);
        }
Esempio n. 18
0
        //reference to Q39(Similar)
        public static int getLongestDistance(TreeAndGraph.BinaryTreeNode root, TreeAndGraph.BinaryTreeNode left, TreeAndGraph.BinaryTreeNode right)
        {
            ReturnWrapper wr = getLongestDistanceHelper(root, left, right);

            //left or right is root will done separately

            if (wr.findleft && wr.findright)
            {
                return wr.count;
            }
            else
            {
                return -1;
            }
        }
 private void BFS(TreeAndGraph.BinaryTreeNode node, int cur, ref int count)
 {
     if (node == null)
     {
         return;
     }
     cur = cur * 10 + (int)node.Data;
     BFS(node.LeftNode, cur, ref count);
     BFS(node.RightNode, cur, ref count);
     //cur = (cur - (int)node.Data) / 10;
     if (node.LeftNode == null && node.RightNode == null)
     {
         count += cur;
     }
 }
        public void connect(TreeAndGraph.BinaryTreeNode root)
        {
            TreeAndGraph.BinaryTreeNode cur_loop = root, down_node = root;

            while (down_node != null)
            {
                TreeAndGraph.BinaryTreeNode last_node = null;
                cur_loop = down_node;
                down_node = null;
                while (cur_loop != null)
                {
                    if (cur_loop.LeftNode != null)
                    {
                        if (down_node == null)
                        {
                            down_node = cur_loop.LeftNode;
                        }
                        if (last_node == null)
                        {
                            last_node = cur_loop.LeftNode;
                        }
                        else
                        {
                            last_node.NextNode = cur_loop.LeftNode;
                            last_node = cur_loop.LeftNode;
                        }
                    }
                    if (cur_loop.RightNode != null)
                    {
                        if (down_node == null)
                        {
                            down_node = cur_loop.RightNode;
                        }
                        if (last_node == null)
                        {
                            last_node = cur_loop.RightNode;
                        }
                        else
                        {
                            last_node.NextNode = cur_loop.RightNode;
                            last_node = cur_loop.RightNode;
                        }
                    }

                    cur_loop = cur_loop.NextNode;
                }
            }
        }
Esempio n. 21
0
 public static void BreathFirstSearch(TreeAndGraph.BinaryTreeNode root)
 {
     System.Collections.Queue q = new System.Collections.Queue();
     q.Enqueue(root);
     while (q.Count!=0)
     {
         TreeAndGraph.BinaryTreeNode btn = (TreeAndGraph.BinaryTreeNode)q.Dequeue();
         System.Console.WriteLine(btn.Data);
         if (btn.LeftNode != null)
         {
             q.Enqueue(btn.LeftNode);
         }
         if (btn.RightNode != null)
         {
             q.Enqueue(btn.RightNode);
         }
     }
 }
Esempio n. 22
0
        public static void getDoubleLinkedListFromBinarySearchTreeInPlace(TreeAndGraph.BinaryTreeNode head)
        {
            if (head == null)
            {
                return;
            }

            getDoubleLinkedListFromBinarySearchTreeInPlace(head.LeftNode);

            if (last!=null)
            {
                last.RightNode = head;
            }
            head.ParentNode = last;
            last = head;

            getDoubleLinkedListFromBinarySearchTreeInPlace(head.RightNode);
        }
Esempio n. 23
0
        public static Result findBiggestDistance(TreeAndGraph.BinaryTreeNode root)
        {
            if (root == null)
            {
                Result n = new Result();
                n.MaxDepth = 0; n.MaxDistance = 0;
                return n;
            }

            Result r_left = findBiggestDistance(root.LeftNode);
            Result r_right = findBiggestDistance(root.RightNode);

            Result r = new Result();
            r.MaxDepth = System.Math.Max(r_left.MaxDepth, r_right.MaxDepth) + 1;
            r.MaxDistance = System.Math.Max(System.Math.Max(r_left.MaxDistance, r_right.MaxDistance), r_left.MaxDepth + r_right.MaxDepth + 1);
            //r_left.MaxDepth + r_right.MaxDepth + 1;
            //System.Math.Max(System.Math.Max(r_left.MaxDistance, r_right.MaxDistance), r_left.MaxDepth + r_right.MaxDepth +1);
            return r;
        }
 private void PreDFS(TreeAndGraph.BinaryTreeNode node)
 {
     if (node == null)
     {
         return;
     }
     TreeAndGraph.BinaryTreeNode l = node.LeftNode, r = node.RightNode;
     node.LeftNode = null;
     if (_last_node == null)
     {
         _last_node = node;
     }
     else
     {
         _last_node.RightNode = node;
         _last_node = node;
     }
     PreDFS(l);
     PreDFS(r);
 }
Esempio n. 25
0
        private static void traverse(TreeAndGraph.BinaryTreeNode btn)
        {
            if (btn == null)
            {
                return;
            }
            traverse(btn.LeftNode);

            if (dln == null)
            {
                dln = new LinkedList.DoubleLinkedNode(btn.Data);
            }
            else
            {
                LinkedList.DoubleLinkedNode new_dln = new LinkedList.DoubleLinkedNode(btn.Data);
                dln.Next = new_dln;
                new_dln.Last = dln;
                dln = new_dln;
            }
            traverse(btn.RightNode);
        }
Esempio n. 26
0
 int DFS(TreeAndGraph.BinaryTreeNode node)
 {
     if (node == null)
     {
         return 0;
     }
     int l =DFS(node.LeftNode);
     if (l == -1)
     {
         return -1;
     }
     int r =DFS(node.RightNode);
     if (r == -1)
     {
         return -1;
     }
     if (Math.Abs(l-r) >= 1)
     {
         return -1;
     }
     return Math.Max(l, r) + 1;
 }
Esempio n. 27
0
 public static void getShortestDistancePoor(TreeAndGraph.GraphNode start, TreeAndGraph.GraphNode end, int count = 1)
 {
     ++count;
     foreach (TreeAndGraph.GraphNode node in start.Nodes)
     {
         if (node == end)
         {
             if (ShortestCount == -1)
             {
                 ShortestCount = count;
             }
             else if (ShortestCount > count)
             {
                 ShortestCount = count;
             }
         }
         else
         {
             getShortestDistancePoor(node, end, count);
         }
     }
 }
 void backward(TreeAndGraph.BinaryTreeNode node)
 {
     if (node == null)
     {
         return;
     }
     backward(node.RightNode);
     if (last == null)
     {
         last = node;
     }
     else
     {
         if (node.Data > last.Data)
         {
             b = last;
             return;
         }
         last = node;
     }
     backward(node.LeftNode);
 }
 void forward(TreeAndGraph.BinaryTreeNode node)
 {
     if (node == null)
     {
         return;
     }
     forward(node.LeftNode);
     if (last == null)
     {
         last = node;
     }
     else
     {
         if (node.Data < last.Data)
         {
             f = last;
             return;
         }
         last = node;
     }
     forward(node.RightNode);
 }
Esempio n. 30
0
        //reference to CC4_6
        //practice: my implementation
        /*
                    TreeAndGraph.BinaryTreeNode btn1 = new TreeAndGraph.BinaryTreeNode(1);
            TreeAndGraph.BinaryTreeNode btn2 = new TreeAndGraph.BinaryTreeNode(2);
            TreeAndGraph.BinaryTreeNode btn3 = new TreeAndGraph.BinaryTreeNode(3);
            TreeAndGraph.BinaryTreeNode btn4 = new TreeAndGraph.BinaryTreeNode(4);
            TreeAndGraph.BinaryTreeNode btn5 = new TreeAndGraph.BinaryTreeNode(5);
            TreeAndGraph.BinaryTreeNode btn6 = new TreeAndGraph.BinaryTreeNode(6);
            TreeAndGraph.BinaryTreeNode btn7 = new TreeAndGraph.BinaryTreeNode(7);
            btn1.LeftNode = btn2; btn1.RightNode = btn3;
            btn2.LeftNode = btn4; btn2.RightNode = btn5;
            btn3.LeftNode = btn6; btn3.RightNode = btn7;

            Wrapper wr = findCommonAncesterWrap(btn1, btn4, btn5);
            if (wr.find)
            {
                Console.WriteLine(wr.btn.Data);
            }
         */
        static Wrapper findCommonAncesterWrap(TreeAndGraph.BinaryTreeNode root, TreeAndGraph.BinaryTreeNode p, TreeAndGraph.BinaryTreeNode q)
        {
            if (root == null)
            {
                Wrapper w = new Wrapper();
                return w;
            }

            Wrapper lw = findCommonAncesterWrap(root.LeftNode, p, q);
            if (lw.find)
            {
                return lw;
            }
            Wrapper rw = findCommonAncesterWrap(root.RightNode, p, q);
            if (rw.find)
            {
                return rw;
            }

            Wrapper wr = new Wrapper();
            if (root == p || root == q)
            {
                wr.find = lw.btn != null || rw.btn != null;
                wr.btn = root;
            }
            else if (lw.btn != null && rw.btn != null)
            {
                wr.find = true;
                wr.btn = root;
            }
            else
            {
                wr.find = false;
                wr.btn = lw.btn != null ? lw.btn : rw.btn;
            }

            return wr;
        }