Ejemplo n.º 1
0
        /// <summary>
        /// 递归创建创建二叉树
        /// </summary>
        /// <param name="input">"3#45###"</param>
        private static BTreeEntity BuildBTreeInternal(ref string input)
        {
            //Console.WriteLine("BuildBTree:" + input);
            if (string.IsNullOrWhiteSpace(input))
            {
                return(null);
            }

            Console.WriteLine(input);

            char c = input[0];

            if (c == '#')
            {
                return(null);
            }
            else if (c >= 0x30 && c <= 0x39)
            {
                BTreeEntity newNode = new BTreeEntity();
                newNode.Value = int.Parse(c.ToString());

                input        = input.Substring(1);
                newNode.Left = BuildBTreeInternal(ref input);

                input         = input.Substring(1);
                newNode.Right = BuildBTreeInternal(ref input);

                return(newNode);
            }
            else
            {
                // error
                return(null);
            }
        }
Ejemplo n.º 2
0
        private bool CheckMatchSubTreeRoot(BTreeEntity tree1, BTreeEntity tree2)
        {
            if (tree1 == null)
            {
                return(false);
            }

            bool result = false;

            //result = tree1.Value == tree2.Value && CheckMatchSubTreeWhole(tree1, tree2);
            //result = result || !result && CheckMatchSubTreeRoot(tree1.Left, tree2);
            //result = result || !result && CheckMatchSubTreeRoot(tree1.Right, tree2);
            if (tree1.Value == tree2.Value)
            {
                result = CheckMatchSubTreeWhole(tree1, tree2);
            }
            if (!result)
            {
                result = CheckMatchSubTreeRoot(tree1.Left, tree2);
            }
            if (!result)
            {
                result = CheckMatchSubTreeRoot(tree1.Right, tree2);
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 非递归实现后序遍历
        /// 逻辑和PostorderTraversalIteratively1,基本类似,区别是通过添加标识字段来确定是否有必要处理右孩子
        /// <see cref="http://blog.csdn.net/zhangxiangdavaid/article/details/37115355"/>
        /// </summary>
        public string PostorderTraversalIteratively2()
        {
            //借助IsRightProcessed标识。 若IsRightProcessed=false,则右孩子没有被访问过;否则则访问过
            string output = string.Empty;

            if (_tree == null)
            {
                return(output);
            }

            Stack <TagBTreeEntity> stack = new Stack <TagBTreeEntity>();
            TagBTreeEntity         tagBTree;
            BTreeEntity            p = _tree;

            while (p != null || stack.Count > 0)
            {
                if (p != null)
                {
                    //当p不为null时,循环获取左子节点

                    tagBTree                  = new TagBTreeEntity();
                    tagBTree.Tree             = p;
                    tagBTree.IsRightProcessed = false;
                    stack.Push(tagBTree);

                    p = p.Left;
                }
                else
                {
                    //当p为pull,说明需要读栈处理啦。

                    //出栈
                    tagBTree = stack.Pop();

                    if (tagBTree.Tree.Right != null && tagBTree.IsRightProcessed == false)
                    {
                        //如果该节点的右子节点不为空,且未被处理过(后序遍历的逻辑:肯定是先处理了右子树的根,然后接着处理当前节点)

                        //更新标记
                        tagBTree.IsRightProcessed = true;

                        //再次入栈
                        stack.Push(tagBTree);

                        //获取右孩子
                        p = tagBTree.Tree.Right;
                    }
                    else
                    {
                        //此时,左右孩子都已处理过了,或者是因为孩子为null,跳过了

                        output += "," + tagBTree.Tree.Value;
                        //置空,需要从栈中pop一个新的节点
                        p = null;
                    }
                }
            }

            return(output.Substring(1));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 递归查找两个节点的位置。如果在均在单侧,则在单侧查找;否则,当前节点即为最小公共父节点
        /// </summary>
        /// <param name="tree"></param>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        /// <returns></returns>
        private BTreeEntity FindCommonMinParentNodeInternal(BTreeEntity tree, int value1, int value2)
        {
            if (tree == null)
            {
                return(null);
            }

            if (_tree.Value == value1 || _tree.Value == value2)
            {
                return(tree);
            }

            bool isInLeft1 = IsFoundChildNode(tree.Left, value1);
            bool isInLeft2 = IsFoundChildNode(tree.Left, value2);

            if (isInLeft1 && isInLeft2)
            {
                return(FindCommonMinParentNodeInternal(tree.Left, value1, value2));
            }

            bool isInRight1 = IsFoundChildNode(tree.Right, value1);
            bool isInRight2 = IsFoundChildNode(tree.Right, value2);

            if (isInRight1 && isInRight2)
            {
                return(FindCommonMinParentNodeInternal(tree.Right, value1, value2));
            }

            if (isInLeft1 && isInRight2 || isInLeft2 || isInRight1)
            {
                return(tree);
            }

            return(null);
        }
Ejemplo n.º 5
0
        private void AllPathWithSumInternal(BTreeEntity tree, string path, int tempSum, int targetSum)
        {
            if (tree == null)
            {
                return;
            }

            tempSum += tree.Value;
            path     = path + "," + tree.Value;

            if (tempSum > targetSum)
            {
                return;
            }
            else if (tempSum == targetSum)
            {
                Console.WriteLine(path.Substring(1));
                return;
            }
            else
            {
                AllPathWithSumInternal(tree.Left, path, tempSum, targetSum);
                AllPathWithSumInternal(tree.Right, path, tempSum, targetSum);
            }
        }
Ejemplo n.º 6
0
        private void AddBSTree(ref BTreeEntity node, int value)
        {
            if (node == null)
            {
                BTreeEntity newNode = new BTreeEntity();
                newNode.Value = value;
                newNode.Left  = null;
                newNode.Right = null;

                node = newNode;
            }
            else if (node.Value > value)
            {
                AddBSTree(ref node.Left, value);
            }
            else if (node.Value < value)
            {
                AddBSTree(ref node.Right, value);
            }
            else
            {
                //Node repeated
                return;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 使用数组存储两条节点路径,再顺序比较,找到最后一个相同的节点,即为最小公共父节点
        /// </summary>
        /// <param name="tree"></param>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        /// <returns></returns>
        private BTreeEntity FindCommonMinParentNodeInternal2(BTreeEntity tree, int value1, int value2)
        {
            List <BTreeEntity> list1 = new List <BTreeEntity>(), list2 = new List <BTreeEntity>();

            GetNodePathInternal(tree, value1, list1);
            GetNodePathInternal(tree, value2, list2);

            var e1 = list1.GetEnumerator();
            var e2 = list2.GetEnumerator();

            BTreeEntity t1, t2;
            BTreeEntity result = null;

            while (e1.MoveNext() && e2.MoveNext())
            {
                t1 = e1.Current;
                t2 = e2.Current;
                if (t1 != t2)
                {
                    break;
                }

                result = t1;
            }

            return(result);
        }
Ejemplo n.º 8
0
        public void InitBTree(string input = null)
        {
            string sample = "124##57###3#6##";

            if (input == null)
            {
                input = sample;
            }

            _tree = BTree.BuildBTree(input);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 递归实现中序遍历
        /// </summary>
        /// <param name="tree"></param>
        private void InorderTraversalRecursively(BTreeEntity tree)
        {
            if (tree == null)
            {
                return;
            }

            InorderTraversalRecursively(tree.Left);
            Console.Write(tree.Value);
            InorderTraversalRecursively(tree.Right);
        }
Ejemplo n.º 10
0
        public void ConvertToDoublyLinkedList(BTreeEntity tree)
        {
            if (tree == null)
            {
                return;
            }

            ConvertToDoublyLinkedList(tree.Left);
            ChangeReference(tree);
            ConvertToDoublyLinkedList(tree.Right);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 递归方式获取二叉树深度
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        private int GetDepth(BTreeEntity tree)
        {
            if (tree == null)
            {
                return(0);
            }

            int leftDepth  = GetDepth(tree.Left);
            int rightDepth = GetDepth(tree.Right);

            return(Math.Max(leftDepth, rightDepth) + 1);
        }
Ejemplo n.º 12
0
        public void ChangeReference(BTreeEntity node)
        {
            if (_head == null)
            {
                _head = node;
            }
            else
            {
                _index.Right = node;
            }

            node.Left = _index;
            _index    = node;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 查找指定节点是否存在二叉树中
        /// </summary>
        /// <param name="tree"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool IsFoundChildNode(BTreeEntity tree, int value)
        {
            if (tree == null)
            {
                return(false);
            }

            if (tree.Value == value)
            {
                return(true);
            }

            return(IsFoundChildNode(tree.Left, value) || IsFoundChildNode(tree.Right, value));
        }
Ejemplo n.º 14
0
        private void MirrorRecursively(BTreeEntity node)
        {
            if (node == null)
            {
                return;
            }
            BTreeEntity temp = node.Left;

            node.Left  = node.Right;
            node.Right = temp;

            MirrorRecursively(node.Left);
            MirrorRecursively(node.Right);
        }
Ejemplo n.º 15
0
 /// <summary>
 /// 检查subTree是否为当前二叉树的子结构,即能局部吻合。如:12#3##4##中的子结构2#3##
 /// </summary>
 /// <param name="subTree"></param>
 /// <returns></returns>
 public bool CheckMatchSubTree(BTreeEntity subTree)
 {
     if (_tree == null && subTree != null)
     {
         return(false);
     }
     else if (subTree == null)
     {
         return(true);
     }
     else
     {
         return(CheckMatchSubTreeRoot(_tree, subTree));
     }
 }
Ejemplo n.º 16
0
        public void PrintDoublyLinkedList()
        {
            Console.Write("打印双向链表的结果:");

            string      ouput = string.Empty;
            BTreeEntity node  = _head;

            while (node != null)
            {
                ouput += "," + node.Value;
                node   = node.Right;
            }

            Console.WriteLine(ouput.Substring(1));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// 非递归实现中序遍历
        /// <see cref="http://blog.csdn.net/zhangxiangdavaid/article/details/37115355"/>
        /// </summary>
        public string InorderTraversalIteratively1()
        {
            string output = string.Empty;

            if (_tree == null)
            {
                return(output);
            }

            Stack <BTreeEntity> stack = new Stack <BTreeEntity>();
            BTreeEntity         p     = _tree;

            while (p != null || stack.Count > 0)
            {
                if (p != null)
                {
                    //循环获取最左下角的节点,并压栈途经的节点
                    stack.Push(p);
                    p = p.Left;
                }
                else
                {
                    p = stack.Pop();

                    //打印
                    output += "," + p.Value;

                    p = p.Right;
                }
            }

            /* 或者
             * while (p != null || stack.Count > 0)
             * {
             *  while (p != null)
             *  {
             *      stack.Push(p);
             *      p = p.Left;
             *  }
             *
             *  p = stack.Pop();
             *  output += "," + p.Value;
             *  p = p.Right;
             * }
             */

            return(output.Substring(1));
        }
Ejemplo n.º 18
0
        private bool CheckMatchSubTreeWhole(BTreeEntity tree1, BTreeEntity tree2)
        {
            if (tree2 == null)
            {
                return(true);
            }
            if (tree1 == null)
            {
                return(false);
            }

            if (tree1.Value != tree2.Value)
            {
                return(false);
            }

            return(CheckMatchSubTreeWhole(tree1.Left, tree2.Left) && CheckMatchSubTreeWhole(tree1.Right, tree2.Right));
        }
Ejemplo n.º 19
0
        /// <summary>
        /// 非递归实现后序遍历
        /// 采用一个标志位
        /// <see cref="http://blog.csdn.net/zhangxiangdavaid/article/details/37115355"/>
        /// </summary>
        public string PostorderTraversalIteratively1()
        {
            string output = string.Empty;

            if (_tree == null)
            {
                return(output);
            }

            Stack <BTreeEntity> stack = new Stack <BTreeEntity>();
            BTreeEntity         p     = _tree;
            BTreeEntity         last  = null; //指示上次打印的节点。此节点用于确认右孩子是否处理过

            //处理堆栈中的节点,因为均是左子节点,所以只考虑处理其右子节点
            while (p != null || stack.Count > 0)
            {
                //先循环找到最左下角的节点,并压栈途径的左节点
                if (p != null)
                {
                    stack.Push(p);
                    p = p.Left;
                }
                else
                {
                    p = stack.Pop();

                    //当右子节点存在且没有右孩子没有被访问过,则从处理右孩子;否则打印
                    if (p.Right != null && p.Right != last)
                    {
                        stack.Push(p);
                        p = p.Right;
                    }
                    else
                    {
                        output += "," + p.Value;
                        last    = p;
                        p       = null;
                    }
                }
            }

            return(output.Substring(1));
        }
Ejemplo n.º 20
0
        public void PrintBinarySearchTree()
        {
            Console.Write("打印二叉查找树的结果:");

            string ouput = string.Empty;
            Stack <BTreeEntity> stack  = new Stack <BTreeEntity>();
            Stack <bool>        stack2 = new Stack <bool>();

            stack.Push(Root);
            stack2.Push(true);

            while (stack.Count > 0)
            {
                BTreeEntity node    = stack.Pop();
                bool        isRight = stack2.Pop();

                if (isRight)
                {
                    //更新父节点的配置为false
                    stack.Push(node);
                    stack2.Push(false);

                    if (node.Left != null)
                    {
                        stack.Push(node.Left);
                        stack2.Push(true);
                    }

                    continue;
                }

                ouput += "," + node.Value;

                if (node.Right != null)
                {
                    stack.Push(node.Right);
                    stack2.Push(true);
                }
            }

            Console.WriteLine(ouput.Substring(1));
        }
Ejemplo n.º 21
0
        /// <summary>
        /// 递归方式判断二叉树是否为平衡树
        /// 递归算法:1: 左右子树均为平衡树; 2: 左右子树的深度差小于1
        ///
        /// 平衡树的条件是:二叉树中任意结点的左右子树的深度相差不超过1
        /// </summary>
        /// <param name="tree"></param>
        /// <param name="depth"></param>
        /// <returns></returns>
        private bool IsBalanced(BTreeEntity tree, out int depth)
        {
            depth = 0;

            if (tree == null)
            {
                return(true);
            }

            int leftDepth, rightDepth;

            if (IsBalanced(tree.Left, out leftDepth) && IsBalanced(tree.Right, out rightDepth))
            {
                if (Math.Abs(leftDepth - rightDepth) <= 1)
                {
                    depth = 1 + Math.Max(leftDepth, rightDepth);
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// 获取二叉树中指定节点的节点路径(存放数组中)
        /// </summary>
        /// <param name="tree"></param>
        /// <param name="value"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        private bool GetNodePathInternal(BTreeEntity tree, int value, List <BTreeEntity> list)
        {
            if (tree == null)
            {
                return(false);
            }

            if (tree.Value == value)
            {
                return(true);
            }

            list.Add(tree);

            bool found = GetNodePathInternal(tree.Left, value, list) || GetNodePathInternal(tree.Right, value, list);

            if (!found)
            {
                list.Remove(tree);
            }

            return(found);
        }
Ejemplo n.º 23
0
        /// <summary>
        /// 非递归实现前序遍历
        /// 根据
        /// <see cref="http://blog.csdn.net/zhangxiangdavaid/article/details/37115355"/>
        /// </summary>
        public string PreorderTraversalUnrecursively2()
        {
            string output = string.Empty;

            if (_tree == null)
            {
                return(output);
            }

            Stack <BTreeEntity> stack = new Stack <BTreeEntity>();
            BTreeEntity         p     = _tree;

            //先使树根入栈,这是循环退出的关键点
            stack.Push(_tree);

            //遇见右孩子,先入栈。 接着处理左孩子,如果不为空,移动p节点,否则,处理栈顶节点(一般为右孩子的节点,如果弹出栈的是根,则结束循环)
            while (stack.Count > 0)
            {
                //先打印
                output += "," + p.Value;

                if (p.Right != null)
                {
                    stack.Push(p.Right);
                }
                if (p.Left != null)
                {
                    p = p.Left;
                }
                else
                {
                    p = stack.Pop();
                }
            }

            return(output.Substring(1));
        }
Ejemplo n.º 24
0
        public void MirrorIteratively()
        {
            Stack <BTreeEntity> stack = new Stack <BTreeEntity>();

            stack.Push(Root);

            while (stack.Count > 0)
            {
                BTreeEntity node = stack.Pop();

                BTreeEntity temp = node.Left;
                node.Left  = node.Right;
                node.Right = temp;

                if (node.Left != null)
                {
                    stack.Push(node.Left);
                }
                if (node.Right != null)
                {
                    stack.Push(node.Right);
                }
            }
        }
Ejemplo n.º 25
0
 public BSTree()
 {
     _tree = null;
 }