示例#1
0
        public int CompareTo(Node other)
        {
            if (other.Data < Data)
            {
                return 1;
            }
            else if (other.Data > Data)
            {
                return -1;
            }

            return 0;
        }
示例#2
0
 protected void PrintTree(Node root)
 {
     var iter = new InOrderTraversalIterator(root);
     foreach (Node node in iter)
     {
         Console.Write("{0} ", node.Data);
     }
     Console.WriteLine();
 }
示例#3
0
        protected Node MakeCompleteTree(int[] seq)
        {
            if (seq.Length < 1)
            {
                return null;
            }

            var root = new Node(seq[0]);
            var adder = new CompleteTreeChildNodeAdder(root);
            for (var i = 1; i < seq.Length; i++)
            {
                adder.AddChildNode(seq[i]);
            }

            return root;
        }
示例#4
0
        public void TestCompleteTreeChildNodeAdder()
        {
            var root = new Node(1);
            var adder = new CompleteTreeChildNodeAdder(root);

            adder.AddChildNode(2);
            Assert.Test(root.Left.Data == 2);

            adder.AddChildNode(3);
            Assert.Test(root.Right.Data == 3);

            adder.AddChildNode(4);
            Assert.Test(root.Left.Left.Data == 4);

            adder.AddChildNode(5);
            Assert.Test(root.Left.Right.Data == 5);

            adder.AddChildNode(6);
            Assert.Test(root.Right.Left.Data == 6);

            adder.AddChildNode(7);
            Assert.Test(root.Right.Right.Data == 7);
        }
示例#5
0
        public void TestLeafs()
        {
            var root = new Node(3);
            var adder = new SortTreeChildNodeAdder(root);
            Assert.Test(root.Leafs == 1);

            adder.AddChildNode(2);
            Assert.Test(root.Leafs == 1);

            adder.AddChildNode(4);
            Assert.Test(root.Leafs == 2);

            adder.AddChildNode(5);
            Assert.Test(root.Leafs == 2);
        }
示例#6
0
        public void TestHeight()
        {
            var root = new Node(3);
            var adder = new SortTreeChildNodeAdder(root);
            Assert.Test(root.Height == 1);

            adder.AddChildNode(2);
            Assert.Test(root.Height == 2);

            adder.AddChildNode(4);
            Assert.Test(root.Height == 2);

            adder.AddChildNode(5);
            Assert.Test(root.Height == 3);
        }
示例#7
0
        public override AbstractChildNodeAdder AddChildNode(Node child)
        {
            Add(Root, child);

            return this;
        }
示例#8
0
 protected void Setup(int data, Node left, Node right)
 {
     Data = data;
     Left = left;
     Right = right;
 }
示例#9
0
        public IEnumerable Traversal(Node node)
        {
            if (node == null)
            {
                yield break;
            }

            if (node.Left != null)
            {
                foreach (var n in Traversal(node.Left))
                {
                    yield return n;
                }
            }
            yield return node;
            if (node.Right != null)
            {
                foreach (var n in Traversal(node.Right))
                {
                    yield return n;
                }
            }
        }
示例#10
0
 public InOrderTraversalIterator(Node _root)
 {
     root = _root;
 }
示例#11
0
        protected Node FindNodeAt(Node n, int targetNo, int currentNo)
        {
            if (n == null)
            {
                return null;
            }

            if (targetNo == currentNo)
            {
                return n;
            }

            Node target;

            target = FindNodeAt(n.Left, targetNo, currentNo * 2);
            if (target == null)
            {
                target = FindNodeAt(n.Right, targetNo, currentNo * 2 + 1);
            }
            return target;
        }
示例#12
0
 protected Node FindNodeAt(Node n, int targetNo)
 {
     return FindNodeAt(n, targetNo, 1);
 }
示例#13
0
        public override AbstractChildNodeAdder AddChildNode(Node child)
        {
            Nodes += 1;
            var parent = ParentNode(Nodes);
            if (parent.Left == null)
            {
                parent.Left = child;
            }
            else if (parent.Right == null)
            {
                parent.Right = child;
            }

            return this;
        }
示例#14
0
 public CompleteTreeChildNodeAdder(Node root)
 {
     Root = root;
     Nodes = 1;
 }
示例#15
0
        protected void Add(Node root, Node child)
        {
            var cmp = root.CompareTo(child);

            // 放到左子树
            if (cmp >= 0)
            {
                if (root.Left == null)
                {
                    root.Left = child;
                }
                else
                {
                    Add(root.Left, child);
                }
            }
            // 放到右子树
            else
            {
                if (root.Right == null)
                {
                    root.Right = child;
                }
                else
                {
                    Add(root.Right, child);
                }
            }
        }
示例#16
0
 public Node(int data, Node left, Node right)
 {
     Setup(data, left, right);
 }
示例#17
0
        public void TestInstaceNode()
        {
            int data = 5;
            var node = new Node(data);

            Assert.Test(data == node.Data);
            Assert.Test(null == node.Left);
            Assert.Test(null == node.Right);

            var left = new Node(data - 1);
            var right = new Node(data + 1);
            node = new Node(data, left, right);

            Assert.Test(data == node.Data);
            Assert.Test(left == node.Left);
            Assert.Test(right == node.Right);
        }
示例#18
0
        public void TestInOrderTraversalIterator()
        {
            var root = new Node(5);
            var adder = new SortTreeChildNodeAdder(root);
            adder.AddChildNode(4)
                .AddChildNode(6)
                .AddChildNode(3)
                .AddChildNode(7);

            Node previous = null;
            foreach (Node n in new InOrderTraversalIterator(root))
            {
                if (previous != null)
                {
                    Assert.Test(previous.Data <= n.Data);
                }
                previous = n;
            }
        }
示例#19
0
 // 添加一个子节点
 abstract public AbstractChildNodeAdder AddChildNode(Node child);
示例#20
0
 public SortTreeChildNodeAdder(Node root)
 {
     Root = root;
 }