コード例 #1
0
        protected void Add(ref LinkedListBinaryTree <T> child, TreeNode <T> treeNode)
        {
            if (child != null)
            {
                throw new Exception("Child is existed");
            }

            LinkedListBinaryTree <T> tempTree = treeNode as LinkedListBinaryTree <T>;

            if (tempTree == null)
            {
                tempTree = new LinkedListBinaryTree <T>(treeNode.Value);
                TreeNode <T> .Copy(treeNode, tempTree);
            }

            child        = tempTree;
            child.level  = level + 1;
            child.parent = this;

            if (depth == 1)
            {
                depth = 1;
                BubbleDepth();
            }

            ++count;
            BubbleCount(1);
        }
コード例 #2
0
        public override void Remove()
        {
            LinkedListBinaryTree <T> sibling;

            if (parent == null)
            {
                return;
            }
            else if (parent.left == this)
            {
                parent.left = null;
                sibling     = parent.right;
            }
            else if (parent.right == this)
            {
                parent.right = null;
                sibling      = parent.left;
            }
            else
            {
                return;
            }

            if (depth + 1 == parent.depth)
            {
                if (sibling == null || sibling.depth < depth)
                {
                    parent.UpdateDepth();
                }
            }

            parent.count -= count;
            parent.BubbleCount(-count);
            parent = null;
        }
コード例 #3
0
        public override TreeNode <T> Clone()
        {
            LinkedListBinaryTree <T> cloneTree = new LinkedListBinaryTree <T>(this.Value);

            if (this.parent == null)
            {
                cloneTree.count        = this.count;
                cloneTree.depth        = this.depth;
                cloneTree.left         = this.left;
                cloneTree.left         = (LinkedListBinaryTree <T>) this.left.Clone();
                cloneTree.right        = (LinkedListBinaryTree <T>) this.right.Clone();
                cloneTree.left.parent  = cloneTree;
                cloneTree.right.parent = cloneTree;
            }
            else
            {
                TreeNode <T> .Copy(this, cloneTree);
            }

            return(cloneTree);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: zamhsu/DataStructure
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            LinkedListBinaryTree <string> binaryTree = new LinkedListBinaryTree <string>("A");

            binaryTree.AddLeft("B");
            binaryTree.AddRight("C");
            binaryTree.Left.AddLeft("D");
            binaryTree.Left.AddRight("E");
            binaryTree.Right.AddLeft("F");
            binaryTree.Right.AddRight("G");

            List <string> preOrderList     = BinaryTreeRecursionTraversal.PreOrder(binaryTree);
            List <string> inOrderList      = BinaryTreeRecursionTraversal.InOrder(binaryTree);
            List <string> postOrderList    = BinaryTreeRecursionTraversal.PostOrder(binaryTree);
            List <string> breadthFirstList = BinaryTreeRecursionTraversal.BreadthFirst(binaryTree);

            StringBuilder preOrder     = new StringBuilder();
            StringBuilder inOrder      = new StringBuilder();
            StringBuilder postOrder    = new StringBuilder();
            StringBuilder breadthFirst = new StringBuilder();

            for (int i = 0; i < preOrderList.Count; i++)
            {
                preOrder.Append(preOrderList[i]);

                if (i != preOrderList.Count - 1)
                {
                    preOrder.Append(", ");
                }
            }

            for (int i = 0; i < inOrderList.Count; i++)
            {
                inOrder.Append(inOrderList[i]);

                if (i != inOrderList.Count - 1)
                {
                    inOrder.Append(", ");
                }
            }

            for (int i = 0; i < postOrderList.Count; i++)
            {
                postOrder.Append(postOrderList[i]);

                if (i != postOrderList.Count - 1)
                {
                    postOrder.Append(", ");
                }
            }

            for (int i = 0; i < breadthFirstList.Count; i++)
            {
                breadthFirst.Append(breadthFirstList[i]);

                if (i != breadthFirstList.Count - 1)
                {
                    breadthFirst.Append(", ");
                }
            }

            Console.WriteLine($"前序走訪:{preOrder}");
            Console.WriteLine($"中序走訪:{inOrder}");
            Console.WriteLine($"後序走訪:{postOrder}");
            Console.WriteLine($"廣度優先:{breadthFirst}");

            Console.ReadLine();
        }
コード例 #5
0
 protected void Add(ref LinkedListBinaryTree <T> child, T value)
 {
     Add(ref child, new LinkedListBinaryTree <T>(value));
 }