Esempio n. 1
0
        public static BiTNode pEnd  = null; //双向链表尾结点

        /// <summary>
        /// 方法功能:把二叉树转换为双向链表
        /// 输入参数:root-二叉树根结点
        /// </summary>
        /// <param name="root"></param>
        public static void InOrderBSTree(BiTNode root)
        {
            if (root == null)
            {
                return;
            }

            //转换root的左子树
            InOrderBSTree(root.Lchild);

            root.Lchild = pEnd; //使当前结点的左指针指向双向链表中最后一个结点
            if (pEnd == null)
            {
                //双向链表中最后一个结点的右指针指向当前结点
                pHead = root;
            }
            else
            {
                //使双向链表中最后一个结点的右指针指向当前结点
                pEnd.Rchild = root;
            }

            pEnd = root;    //将当前结点设为双向链表中最后一个结点

            //转换root的右子树
            InOrderBSTree(root.Rchild);
        }
Esempio n. 2
0
        /// <summary>
        /// 方法功能:判断两个二叉树是否相等
        /// 参数:root1与root2分别为两个二叉树的根结点
        /// </summary>
        /// <param name="root1"></param>
        /// <param name="root2"></param>
        /// <returns></returns>
        public static bool IsEqual(BiTNode root1, BiTNode root2)
        {
            if (root1 == null && root2 == null)
            {
                return(true);
            }

            if (root1 == null && root2 != null)
            {
                return(false);
            }

            if (root1 != null && root2 == null)
            {
                return(false);
            }

            if (root1.Data == root2.Data)
            {
                return(IsEqual(root1.Lchild, root2.Lchild) && IsEqual(root1.Rchild, root2.Rchild));
            }
            else
            {
                return(false);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// 方法功能:打印出满足所有结点的和等于num的所有路径
 /// 参数:root-0二叉树根结点;num:给定的整数;sum:当前路径上所有结点的和
 /// v:用来存储从根结点到当前遍历到结点的路径
 /// </summary>
 /// <param name="root"></param>
 /// <param name="num"></param>
 /// <param name="sum"></param>
 /// <param name="v"></param>
 public static void FindRoad(BiTNode root, int num, int sum, List <int> v)
 {
     //记录当前遍历的root结点
     sum += root.Data;
     v.Add(root.Data);
     //当前结点是叶子结点且遍历的路径上所有结点的和等于sum
     if (root.Lchild == null && root.Rchild == null && sum == num)
     {
         for (int i = 0; i < v.Count; i++)
         {
             Console.Write(v[i] + " ");
         }
         Console.WriteLine();
     }
     //遍历root的左子树
     if (root.Lchild != null)
     {
         FindRoad(root.Lchild, num, sum, v);
     }
     //遍历root的右子树
     if (root.Rchild != null)
     {
         FindRoad(root.Rchild, num, sum, v);
     }
     //清除遍历的路径
     sum -= v[v.Count - 1];
     v.Remove(v.Count - 1);
 }
Esempio n. 4
0
        public static void PrintTreeLayer(BiTNode root)
        {
            if (root == null)
            {
                return;
            }

            BiTNode         p;
            Queue <BiTNode> queue = new Queue <BiTNode>();

            //树根结点进队列
            queue.Enqueue(root);
            while (queue.Count > 0)
            {
                p = queue.Dequeue();
                //访问当前结点
                Console.Write(p.Data + " ");
                //如果这个结点的左孩子不为空则入队列
                if (p.Lchild != null)
                {
                    queue.Enqueue(p.Lchild);
                }
                //如果这个结点的右孩子不为空则入队列
                if (p.Rchild != null)
                {
                    queue.Enqueue(p.Rchild);
                }
            }
        }
Esempio n. 5
0
 /// <summary>
 /// 方法功能:查找值最大的结点
 /// 输入参数:root-根结点
 /// </summary>
 /// <param name="root"></param>
 /// <returns></returns>
 public static BiTNode GetMaxNode(BiTNode root)
 {
     if (root == null)
     {
         return(root);
     }
     while (root.Rchild != null)
     {
         root = root.Rchild;
     }
     return(root);
 }
Esempio n. 6
0
        public static void ReverseTree(BiTNode root)
        {
            if (root == null)
            {
                return;
            }

            ReverseTree(root.Lchild);
            ReverseTree(root.Rchild);
            BiTNode tmp = root.Lchild;

            root.Lchild = root.Rchild;
            root.Rchild = tmp;
        }
Esempio n. 7
0
        public static BiTNode createDupTree(BiTNode root)
        {
            if (root == null)
            {
                return(null);
            }
            //二叉树根结点
            BiTNode dupTreee = new BiTNode();

            dupTreee.Data = root.Data;
            //复制左子树
            dupTreee.Lchild = createDupTree(root.Lchild);
            //复制右子树
            dupTreee.Rchild = createDupTree(root.Rchild);
            return(dupTreee);
        }
Esempio n. 8
0
 public static void printTreeMidOrder(BiTNode root)
 {
     if (root == null)
     {
         return;
     }
     //遍历root结点的左子树
     if (root.Lchild != null)
     {
         printTreeMidOrder(root.Lchild);
     }
     //遍历root结点
     Console.Write(root.Data + " ");
     //遍历root结点的右子树
     if (root.Rchild != null)
     {
         printTreeMidOrder(root.Rchild);
     }
 }
Esempio n. 9
0
        /// <summary>
        /// 方法功能:构造二叉树
        /// 返回值:返回新构造的二叉树的根结点
        /// </summary>
        /// <returns></returns>
        public static BiTNode constructTree(int i, int j, int x, int y)
        {
            BiTNode root  = new BiTNode();
            BiTNode node1 = new BiTNode();
            BiTNode node2 = new BiTNode();
            BiTNode node3 = new BiTNode();
            BiTNode node4 = new BiTNode();

            root.Data    = i;
            node1.Data   = j;
            node2.Data   = x;
            node3.Data   = y;
            node4.Data   = 9;
            root.Lchild  = node1;
            root.Rchild  = node2;
            node1.Lchild = node3;
            node1.Rchild = node4;
            node2.Lchild = node2.Rchild = node3.Lchild = node3.Rchild = node4.Lchild = node4.Rchild = null;
            return(root);
        }
Esempio n. 10
0
        public static BiTNode constructTree()
        {
            BiTNode root  = new BiTNode();
            BiTNode node1 = new BiTNode();
            BiTNode node2 = new BiTNode();
            BiTNode node3 = new BiTNode();
            BiTNode node4 = new BiTNode();

            root.Data    = 6;
            node1.Data   = 3;
            node2.Data   = -7;
            node3.Data   = -1;
            node4.Data   = 9;
            root.Lchild  = node1;
            root.Rchild  = node2;
            node1.Lchild = node3;
            node1.Rchild = node4;
            node2.Lchild = node2.Rchild = node3.Lchild = node3.Rchild = node4.Lchild = node4.Rchild = null;
            return(root);
        }
Esempio n. 11
0
        /// <summary>
        /// 方法功能;查找二叉树中两个结点最近的共同父结点
        /// 输入参数:root-根结点;node1和node2为二叉树中两个结点
        /// 返回值:node1和node2最近的共同父结点
        /// </summary>
        /// <param name="root"></param>
        /// <param name="root1"></param>
        /// <param name="root2"></param>
        /// <returns></returns>
        public static BiTNode FindParentNode(BiTNode root, BiTNode node1, BiTNode node2)
        {
            Stack <BiTNode> stack1 = new Stack <BiTNode>();   //保存从root到node1的路径
            Stack <BiTNode> stack2 = new Stack <BiTNode>();   //保存从root到node2的路径

            //获取从root到node1的路径
            getPathFromRoot(root, node1, stack1);
            //获取从root到node2的路径
            getPathFromRoot(root, node2, stack2);
            BiTNode commomParent = null;

            //获取最靠近node1和node2的父结点
            while (stack1.Peek() == stack2.Peek())
            {
                commomParent = stack1.Peek();
                stack1.Pop();
                stack2.Pop();
            }
            return(commomParent);
        }
Esempio n. 12
0
        public static BiTNode arraytotree(int[] arr, int start, int end)
        {
            BiTNode root = null;

            if (end >= start)
            {
                root = new BiTNode();
                int mid = (start + end + 1) / 2;
                //树的根结点为数组中间的元素
                root.Data = arr[mid];
                //递归的用左半部分数组构造root的左子树
                root.Lchild = arraytotree(arr, start, mid - 1);
                //递归的用右半部分数组构造root的右子树
                root.Rchild = arraytotree(arr, mid + 1, end);
            }
            else
            {
                root = null;
            }
            return(root);
        }
Esempio n. 13
0
        /// <summary>
        /// 方法功能:获取二叉树从根结点root到node结点的路径
        /// 输入参数:root-根结点 node-二叉树中的某个结点;s-用来存储路径的栈
        /// </summary>
        /// <param name="root"></param>
        /// <param name="node"></param>
        /// <param name=""></param>
        /// <returns></returns>
        public static bool getPathFromRoot(BiTNode root, BiTNode node, Stack <BiTNode> s)
        {
            if (root == null)
            {
                return(false);
            }

            if (root == node)
            {
                s.Push(root);
                return(true);
            }

            //如果node结点在root结点的左子树或右子树上
            //那么root就是node的祖先结点,把它加到栈里
            if (getPathFromRoot(root.Lchild, node, s) || getPathFromRoot(root.Rchild, node, s))
            {
                s.Push(root);
                return(true);
            }
            return(false);
        }
Esempio n. 14
0
        public static BiTNode GetNode(BiTNode root)
        {
            BiTNode maxNode = GetMaxNode(root);
            BiTNode minNode = GetMinNode(root);

            int     mid    = (maxNode.Data + minNode.Data) / 2;
            BiTNode result = null;

            while (root != null)
            {
                //当前结点的值不大于f,则在右子树上找
                if (root.Data <= mid)
                {
                    root = root.Rchild;
                }
                //否在左子树上找
                else
                {
                    result = root;
                    root   = root.Lchild;
                }
            }
            return(result);
        }