public int CompareTo(Node other) { if (other.Data < Data) { return 1; } else if (other.Data > Data) { return -1; } return 0; }
protected void PrintTree(Node root) { var iter = new InOrderTraversalIterator(root); foreach (Node node in iter) { Console.Write("{0} ", node.Data); } Console.WriteLine(); }
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; }
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); }
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); }
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); }
public override AbstractChildNodeAdder AddChildNode(Node child) { Add(Root, child); return this; }
protected void Setup(int data, Node left, Node right) { Data = data; Left = left; Right = right; }
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; } } }
public InOrderTraversalIterator(Node _root) { root = _root; }
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; }
protected Node FindNodeAt(Node n, int targetNo) { return FindNodeAt(n, targetNo, 1); }
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; }
public CompleteTreeChildNodeAdder(Node root) { Root = root; Nodes = 1; }
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); } } }
public Node(int data, Node left, Node right) { Setup(data, left, right); }
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); }
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; } }
// 添加一个子节点 abstract public AbstractChildNodeAdder AddChildNode(Node child);
public SortTreeChildNodeAdder(Node root) { Root = root; }