Ejemplo n.º 1
0
 public void printTreeByLevel(Node root)
 {
     if (root==null) return;
     Queue cur = new Queue();
     Queue next = new Queue();
     cur.Enqueue(root);
     while ((cur.Count > 0) || (next.Count > 0))
     {
         if (cur.Count > 0)
         {
             Node temp = (Node)cur.Dequeue();
             Console.Write("{0}->", temp.value);
             if (temp.right != null)
                 next.Enqueue(temp.right);
             if (temp.left != null)
                 next.Enqueue(temp.left);
         }
         else
         {
             Console.WriteLine();
             Queue tq = next;
             next = cur;
             cur = tq;
         }
     }
 }
Ejemplo n.º 2
0
 public void printAllpath(Node root, int sum)
 {
     if (root == null) return;
     ArrayList al = new ArrayList();
     ArrayList working = new ArrayList();
     printSumUtil(al, root, sum, working);
 }
Ejemplo n.º 3
0
 public static void printLevelOrder(Node root)
 {
     int height = maxHeight(root);
     for (int level = 1; level <= height; level++)
     {
         printLevelMethod(root, level);
         Console.WriteLine();
     }
 }
Ejemplo n.º 4
0
 public static int maxHeight(Node root)
 {
     if (null == root) return 0;
     int lHeight = maxHeight(root.left);
     int rHeight = maxHeight(root.right);
     if (lHeight > rHeight)
         return lHeight + 1;
     else
         return rHeight + 1;
 }
Ejemplo n.º 5
0
 public Node CommonAncestor(Node root, Node X, Node Y)
 {
     if (null == root) return null;
     if (root == X) return X;
     if (root == Y) return Y;
     Node left = CommonAncestor(root.left, X, Y);
     Node right = CommonAncestor(root.right, X, Y);
     if ((left != null) && (right != null))
         return root;
     else if ((left == null) && (right == null))
         return null;
     else return (null==left) ? right : left;
 }
Ejemplo n.º 6
0
        public void getnThNode(Node root,ref int i, int N)
        {
            if (root == null) return;
            getnThNode(root.right, ref i, N);
            i++;

            if (i == N)
            {
                Console.WriteLine("the value of {0}th is {1}", N, root.value);
                return;
            }
            getnThNode(root.left, ref i, N);
        }
Ejemplo n.º 7
0
        public static void printLevelMethod(Node p, int level)
        {
            if (p == null) return;
            if (level == 1)
            {
                Console.Write("{0}", p.value);

            }
            else
            {
                printLevelMethod(p.left, level - 1);
                printLevelMethod(p.right, level - 1);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// this created based on BFS
        /// a full balanced tree, 
        /// starting node[1]
        /// </summary>
        /// <param name="total"></param>
        /// <returns></returns>
        public Node createTree(int total)
        {
            Node[] all = new Node[total + 15];
            for (int i = 0; i < total + 15; i++)
                all[i] = new Node(i);
            all[1].left = all[2];
            all[2].left = all[4];
            all[3].left = all[6];
            all[6].left = all[7];

            all[1].right = all[3];

            all[2].right = all[5];

            return all[1];
        }
Ejemplo n.º 9
0
        private void Link_DFS_Util(Node root, int level)
        {
            if (root == null) return;
            ArrayList cur = null;
            if (result.Count <= level)
            {
                cur = new ArrayList();
                result.Add(cur);
            }

            ArrayList levelth = (ArrayList)result[level];
            levelth.Add(root);

            Link_DFS_Util(root.left, level + 1);
            Link_DFS_Util(root.right, level + 1);
        }
Ejemplo n.º 10
0
        public Node createBigTree(int total)
        {
            Node[] all = new Node[total];
            for (int i = 0; i < total; i++)
            {
                all[i] = new Node(i % 5);
                all[i].Name = "node-" + Convert.ToString(i);

            }
            for (int i = 0; i < total / 2 - 5; i++)
            {
                all[i].value = i % 5;
                all[i].left = all[i * 2 + 1];
                all[i].right = all[i * 2 + 2];
            }
            return all[0];
        }
Ejemplo n.º 11
0
 private void printSumUtil(ArrayList al, Node root, int sum, ArrayList working)
 {
     if (root == null)
         return;
     if (root.value == sum)
     {
         working.Add(root.value);
         ArrayList temp = (ArrayList)working.Clone();
         al.Add(temp);
         return;
     }
     working.Add(root.value);
     if (root.left != null)
     {
         printSumUtil(al, root.left, sum - root.value, working);
     }
     if (root.right != null)
     {
         printSumUtil(al, root.right, sum - root.value, working);
     }
     working.RemoveAt(working.Count - 1);
 }
Ejemplo n.º 12
0
 public Node(int val)
 {
     left = null;
     right = null;
     value = val;
     next = null;
 }
Ejemplo n.º 13
0
 private void serialUti(Node root, ArrayList res)
 {
     if (root == null)
     {
         res.Add("#");
         return;
     }
     res.Add(Convert.ToString(root.value));
     serialUti(root.left, res);
     serialUti(root.right, res);
 }
Ejemplo n.º 14
0
 private void DeserialUtil(ref Node cur, ArrayList al, ref int index)
 {
     if (index > al.Count - 1) return;
     object top = al[index++];
     string temp = (string)top;
     int value = 0;
     if (temp != "#")
     {
         value= Convert.ToInt32(temp);
         cur = new Node(value);
         ///cur.left and cur.right are both null
         ///in the initialization
         DeserialUtil(ref cur.left, al, ref index);
         DeserialUtil(ref cur.right, al, ref index);
     }
 }
Ejemplo n.º 15
0
        public ArrayList serializeIt(Node root)
        {
            ArrayList al = new ArrayList();
            serialUti(root, al);
            for (int i = 0; i < al.Count; i++)

                Console.Write("{0}", al[i]);

            return al;
        }
Ejemplo n.º 16
0
 public void ReverseInOrder(Node root)
 {
     if (root == null) return;
     ReverseInOrder(root.right);
     Console.WriteLine("{0}->", root.value);
     ReverseInOrder(root.left);
 }
Ejemplo n.º 17
0
 public void preOrder(Node root)
 {
     if (null == root) return;
     Stack pre = new Stack();
     pre.Push(root);
     while (pre.Count > 0)
     {
         Node cur = (Node)pre.Pop();
         Console.Write("{0}", cur.value);
         if (cur.right != null)
             pre.Push(cur.right);
         if (cur.left != null)
             pre.Push(cur.left);
     }
 }
Ejemplo n.º 18
0
 public Node LinkThem_BFS(Node root)
 {
     return null;
 }
Ejemplo n.º 19
0
 public void LinkThem_DFS(Node root)
 {
     Link_DFS_Util(root, 0);
 }
Ejemplo n.º 20
0
 /// <summary>
 /// this is inorder traversal
 /// </summary>
 /// <param name="root"></param>
 public void inOrder(Node root)
 {
     Stack inStack = new Stack();
     Node r = root;
     while (true)
     {
         if (r != null)
         {
             inStack.Push(r);
             r = r.left;
         }
         else if (inStack.Count > 0)
         {
             r = (Node)inStack.Pop();
             Console.WriteLine("{0}->", r.value);
             r = r.right;
         }
         else
         {
             break;
         }
     }
     return;
 }
Ejemplo n.º 21
0
        /// <summary>
        /// this is post order traversal
        /// </summary>
        /// <param name="root"></param>
        public void PostOrder(Node root)
        {
            Stack myPost = new Stack();
            if (null == root) return;
            Node r = root;
            myPost.Push(root);
            Node pre = null;
            while (myPost.Count>0)
            {
                Node cur = (Node)myPost.Peek();
                /// going down
                if ((pre == null) || (pre.left == cur) || (pre.right == cur))
                {
                    if (cur.left != null)
                        myPost.Push(cur.left);
                    else if (cur.right != null)
                        myPost.Push(cur.right);
                    else
                    {
                        cur = (Node)myPost.Pop();
                        Console.Write("{0}, ", cur.value);
                    }
                    pre = cur;
                }
                /// going up from left child
                if (cur.left == pre)
                {
                    if (cur.right != null)
                    {
                        myPost.Push(cur.right);
                    }
                    else
                    {
                        cur = (Node)myPost.Pop();

                        Console.Write("{0}, ", cur.value);
                    }
                }
                /// going up from right child
                if (cur.right == pre)
                {
                    cur = (Node)myPost.Pop();
                    Console.Write("{0}, ", cur.value);
                }

                pre = cur;
            }
        }