Beispiel #1
0
        public void Posorder()
        {
            Stack <TwoXTree> stack  = new Stack <TwoXTree>();
            Stack <bool>     stack2 = new Stack <bool>();
            TwoXTree         node   = this;
            bool             flag;

            do
            {
                while (node != null)
                {
                    stack.Push(node);
                    stack2.Push(false);

                    node = node.LeftChild;
                }

                node = stack.Pop();
                flag = stack2.Pop();
                if (flag)
                {
                    Visit(node);
                    node = null;
                }
                else
                {
                    stack.Push(node);
                    stack2.Push(true);

                    node = node.RightChild;
                }
            } while (node != null || stack.Count > 0);
        }
Beispiel #2
0
        public void Preorder()
        {
            Stack <TwoXTree> stack  = new Stack <TwoXTree>();
            Stack <bool>     stack2 = new Stack <bool>();
            TwoXTree         node   = this;
            bool             flag; // used to check if already visit both the left child and the right child of current node

            do
            {
                while (node != null)
                {
                    Visit(node);
                    stack.Push(node);
                    stack2.Push(false);

                    node = node.LeftChild;
                }

                node = stack.Pop();
                flag = stack2.Pop();
                if (flag)
                {
                    node = null;
                }
                else
                {
                    stack.Push(node);
                    stack2.Push(true);

                    node = node.RightChild;
                }
            } while (node != null || stack.Count > 0);
        }
Beispiel #3
0
        public int GetMaxLength()
        {
            Queue <TwoXTree> treeQueue   = new Queue <TwoXTree>();
            Queue <int>      lengthQueue = new Queue <int>();

            TwoXTree node   = this;
            int      length = 0;

            treeQueue.Enqueue(node);
            lengthQueue.Enqueue(1);
            while (treeQueue.Count > 0)
            {
                node   = treeQueue.Dequeue();
                length = lengthQueue.Dequeue();

                if (node.LeftChild != null)
                {
                    treeQueue.Enqueue(node.LeftChild);
                    lengthQueue.Enqueue(length + 1);
                }
                if (node.RightChild != null)
                {
                    treeQueue.Enqueue(node.RightChild);
                    lengthQueue.Enqueue(length + 1);
                }
            }

            return(length);
        }
Beispiel #4
0
        /// <summary>
        /// Get total count
        /// </summary>
        /// <returns></returns>
        public int GetTotalCount()
        {
            Queue <TwoXTree> queue = new Queue <TwoXTree>();
            TwoXTree         node  = this;
            int totalCount         = 0;

            while (node != null)
            {
                ++totalCount;

                if (node.LeftChild != null)
                {
                    queue.Enqueue(node.LeftChild);
                }
                if (node.RightChild != null)
                {
                    queue.Enqueue(node.RightChild);
                }

                if (queue.Count > 0)
                {
                    node = queue.Dequeue();
                }
                else
                {
                    node = null;
                }
            }

            return(totalCount);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            string   tree     = "A(B(C(E(H,),F),D(,I)),J(K,L(M(,N),)))";
            TwoXTree twoXTree = TwoXTree.CreateTwoXTree(tree);

            int totalCount          = twoXTree.GetTotalCount();
            int totalCountRecursion = twoXTree.GetTotalCount_Recursion();

            int maxLength          = twoXTree.GetMaxLength();
            int maxLengthRecursion = twoXTree.GetMaxLength_Recursion();

            Console.WriteLine("Preorder: ");
            twoXTree.Preorder_Recursion();
            Console.WriteLine();
            twoXTree.Preorder();
            Console.WriteLine();

            Console.WriteLine("Inorder: ");
            twoXTree.Inorder_Recursion();
            Console.WriteLine();
            twoXTree.Inorder();
            Console.WriteLine();

            Console.WriteLine("Posorder: ");
            twoXTree.Posorder_Recursion();
            Console.WriteLine();
            twoXTree.Posorder();
            Console.WriteLine();

            Console.WriteLine("Layer Order: ");
            twoXTree.LayerOrder();
            Console.WriteLine();

            Console.ReadKey();
        }
Beispiel #6
0
        private void Visit(TwoXTree treeNode)
        {
            if (treeNode == null)
            {
                return;
            }

            Console.Write(" ---> {0} ", treeNode.Data);
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            //从Tree.txt文件读取
            string tree = string.Empty;

            tree = File.ReadAllText(Environment.CurrentDirectory + "\\Tree.txt");

            //string tree = "A(B(C(E(H,),F),D(,I)),J(K,L(M(,N),)))";
            TwoXTree twoXTree = TwoXTree.CreateTwoXTree(tree);

            int totalCount          = twoXTree.GetTotalCount();
            int totalCountRecursion = twoXTree.GetTotalCount_Recursion();

            int maxLength          = twoXTree.GetMaxLength();
            int maxLengthRecursion = twoXTree.GetMaxLength_Recursion();

            Console.WriteLine("Preorder: ");
            twoXTree.Preorder_Recursion();
            Console.WriteLine();
            twoXTree.Preorder();
            Console.WriteLine();

            Console.WriteLine("Inorder: ");
            twoXTree.Inorder_Recursion();
            Console.WriteLine();
            twoXTree.Inorder();
            Console.WriteLine();

            Console.WriteLine("Posorder: ");
            twoXTree.Posorder_Recursion();
            Console.WriteLine();
            twoXTree.Posorder();
            Console.WriteLine();

            Console.WriteLine("Layer Order: ");
            twoXTree.LayerOrder();
            Console.WriteLine();

            Console.ReadKey();
        }
Beispiel #8
0
        /// <summary>
        /// Create a 2XTree
        /// </summary>
        /// <param name="treeString">eg: A(B(C,D),E(,F(G,)))$</param>
        /// <returns></returns>
        public static TwoXTree CreateTwoXTree(string treeString)
        {
            if (String.IsNullOrEmpty(treeString) || treeString.Equals("$"))
            {
                return(null);
            }

            var charArray = treeString.ToCharArray();

            if (charArray.Count(item => item == '(') != charArray.Count(item => item == ')'))
            {
                throw new ArgumentException("treeString");
            }

            Stack <TwoXTree> stack = new Stack <TwoXTree>();
            TwoXTree         root  = new TwoXTree
            {
                Data = treeString[0].ToString()
            };

            stack.Push(root);

            TwoXTree parent = root, node = root;
            bool     isLeftNode = true;

            for (int i = 1; i < treeString.Length; i++)
            {
                switch (treeString[i])
                {
                case '$':
                    break;

                case '(':
                    stack.Push(node);
                    parent     = node;
                    isLeftNode = true;
                    break;

                case ')':
                    stack.Pop();
                    parent = stack.Peek();
                    break;

                case ',':
                    isLeftNode = false;
                    break;

                default:
                    node = new TwoXTree
                    {
                        Data = treeString[i].ToString()
                    };

                    if (parent != null)
                    {
                        if (isLeftNode)
                        {
                            parent.LeftChild = node;
                        }
                        else
                        {
                            parent.RightChild = node;
                        }
                    }
                    break;
                }
            }

            return(root);
        }