public bool Insert(int iKey, Data data) { if (data == null) { return false; } Node n = new Node(iKey,data); return Insert(n); }
public void InOrderTraversal(Node n) { if (n == null) { return; } InOrderTraversal(n.left); _visit.VisitNode(n,_que); InOrderTraversal(n.right); }
public void VisitNode(Node n, Queue<int> queue) { if ((n == null)||(queue == null)) { throw new ArgumentNullException(); } queue.Enqueue(n.iKey); return; }
public void VisitNode_Enqueues_Int() { // Arrange Node node = new Node(); Queue<int> queue = new Queue<int>(); Visit v = new Visit(); // Class being tested... // Act Assert.IsTrue(queue.Count == 0); v.VisitNode(node, queue); //Assert Assert.IsTrue(queue.Count == 1); }
public void VisitNode_Throws_ArgumentNullException_On_Null_Queue() { // Arrange Node n = new Node(); Visit v = new Visit(); // Class being tested... // Act/Assert try { v.VisitNode(n, null); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentNullException)); return; } Assert.Fail(); }
public bool Insert(Node n) { if (_tree == null) { _tree = n; // tree was empty (null), so n is first element. return true; } Node current = _tree; Node parent = null; bool inserted = false; while (true) { parent = current; if (current.iKey == n.iKey) { return true; // Element (node) is already in tree. } if (current.iKey > n.iKey) { current = current.left; if (current == null) { parent.left = n; // Less-than insert left. inserted = true; break; } } else { current = current.right; if (current == null) { parent.right = n; inserted = true; break; } } } return inserted; }
public void InOrderTraversal(Node n) { Stack<Node> s = new Stack<Node>(); while ((s.Count > 0)||(n != null)) { if (n != null) { s.Push(n); n = n.left; } else // Get here if (n==null), so stack is not empty. { n = s.Pop(); _visit.VisitNode(n, _que); n = n.right; } } }
public void VisitNode_Enqueues_Ints_InOrder() { // Arrange Node node1 = new Node(); Node node2 = new Node(); Queue<int> queue = new Queue<int>(); Visit v = new Visit(); // Class being tested... node1.iKey = 1; node2.iKey = 2; // Act v.VisitNode(node1, queue); v.VisitNode(node2, queue); //Assert int i1 = queue.Dequeue(); int i2 = queue.Dequeue(); Assert.IsTrue(i1 == 1); Assert.IsTrue(i2 == 2); }
public bool FindNodeAndParent(Node tree, Node n, ref Node parent) { if (tree == null) // Check for empty tree. { parent = null; return false; } if (tree == n) { return true; } parent = tree; bool found; if (tree.iKey > n.iKey) { if (tree.left != null) { found = FindNodeAndParent(tree.left, n, ref parent); if (found == true) { return true; } } } if (tree.right != null) { found = FindNodeAndParent(tree.right, n, ref parent); if (found == true) { return true; } } return false; }
// FindNodeyValue() is a pre-order recursive search. In a well balanced tree this is O(log number-of-nodes). private Node FindNodeByValue(Node tree, int iKey) { if (tree == null) { return null; } if (iKey == tree.iKey) // Check for match. { return tree; } if (iKey < tree.iKey) // Check to see if we need to go look left. { return FindNodeByValue(tree.left, iKey); } // Only case left is to check the right branch of the tree. return FindNodeByValue(tree.right, iKey); }
public void Test_Remove_On_Node_With_Only_A_Right_Subtree() { // Arrange BinaryTree bt = new BinaryTree(); Node n1 = new Node(17); Node n2 = new Node(23); // This is the node we will delete. Node n3 = new Node(29); // This order of insertion should create a right-only subtree. bt.Insert(n1); bt.Insert(n2); bt.Insert(n3); // Act bool removed = bt.Remove(23); // Assert Assert.IsTrue(removed); Assert.IsTrue(bt.TreeDepth() == 2); }
public void Test_Remove_On_Root_Node_With_Left_and_right_Subtrees() { // Arrange BinaryTree bt = new BinaryTree(); // The test tree looks like: // 11 // / \ // 5 17 // / \ // 2 7 // / \ // 1 4 // / // 3 Node[] nArray = new Node[8]; nArray[0] = new Node(11); // This is the node we will delete below (root node). nArray[1] = new Node(5); nArray[2] = new Node(17); nArray[3] = new Node(2); nArray[4] = new Node(7); nArray[5] = new Node(1); nArray[6] = new Node(4); nArray[7] = new Node(3); // This order of insertion should create a our "complex" tree. for (int i = 0; i < 8; i++) { bt.Insert(nArray[i]); } // Act bool removed = bt.Remove(11); // Assert Assert.IsTrue(removed); Queue<Node> queue = bt.TreeToQueue(); // Get the remaining elements to validate them... Assert.IsTrue(queue.Count == 7); Assert.IsTrue(1 == queue.Dequeue().iKey); Assert.IsTrue(2 == queue.Dequeue().iKey); Assert.IsTrue(3 == queue.Dequeue().iKey); Assert.IsTrue(4 == queue.Dequeue().iKey); Assert.IsTrue(5 == queue.Dequeue().iKey); Assert.IsTrue(7 == queue.Dequeue().iKey); Assert.IsTrue(17 == queue.Dequeue().iKey); }
public void Test_TreeIsBalanced1_For_Single_Node_Tree() { // Arrange BinaryTree b = new BinaryTree(); Node tree = new Node(); // Act bool balanced = b.TreeIsBalanced1(); // Assert Assert.IsTrue(balanced); }
private bool TreeIsBalancedInternal2(Node t, ref int depth) { if (t == null) { depth = 0; return true; } int depthL = 0; int depthR = 0; if (TreeIsBalancedInternal2(t.left,ref depthL)&&(TreeIsBalancedInternal2(t.right,ref depthR))) { int diff = Math.Abs(depthL - depthR); if (diff <= 1) { depth = 1 + (Math.Max(depthL, depthR)); return true; } } return false; }
private int TreeDepthInternal(Node t) { if (t == null) { return 0; } int dLeft = TreeDepthInternal(t.left); int dRight = TreeDepthInternal(t.right); return Math.Max(dLeft, dRight) + 1; // Plus one for this node... }
public void Test_Remove_On_Node_With_Only_A_Left_Subtree() { // Arrange BinaryTree bt = new BinaryTree(); Node n1 = new Node(17); Node n2 = new Node(14); // This is the one we will remove. Node n3 = new Node(9); // This order of insertion should create a left-only subtree. bt.Insert(n1); bt.Insert(n2); bt.Insert(n3); // Act bool removed = bt.Remove(14); // Assert Assert.IsTrue(removed); Assert.IsTrue(bt.TreeDepth() == 2); Queue<Node> queue = bt.TreeToQueue(); Assert.IsTrue(queue.Dequeue() == n3); Assert.IsTrue(queue.Dequeue() == n1); }
public bool Remove(int iKey) { Node nRemove = FindNodeByValue(_tree, iKey); // XXX: Dorky code. Combine this and next method call. Node parent = null; bool found = FindNodeAndParent(_tree, nRemove, ref parent); if (found == false) { // Either the tree is empty or the node doesn't exist in the tree. return false; } // Ok, node was found. if (parent == null) { // Tree is a single node tree. _tree = null; return true; } // Case #1: Node n is a leaf (no children) if ( (nRemove.left == null) && (nRemove.right == null)) { if (parent.left == nRemove) parent.left = null; else parent.right = null; return true; } // Case #2: Node n has either a left or right child but not both. if((nRemove.left != null) && (nRemove.right == null)) { if (parent.left == nRemove) { parent.left = nRemove.left; } else { parent.right = nRemove.left; } return true; } if ((nRemove.left == null) && (nRemove.right != null)) { if (parent.left == nRemove) { parent.left = nRemove.right; } else { parent.right = nRemove.right; } return true; } // Case #3: The node has both left and right children. // Find the largest value in the left subtree, copy its data into the node being "removed" then // remove that largest node from the tree (note: it won't have a right subtree). Node nLargest = nRemove.left; Node nLargestParent = nRemove; while (nLargest.right != null) { nLargestParent = nLargest; nLargest = nLargest.right; } int tempValue = nLargest.iKey; bool removed = Remove(nLargest.iKey); nRemove.iKey = tempValue; return true; }
public bool TreeIsBalancedInternal1(Node t) { if (t == null) { return true; } int dLeft = TreeDepthInternal(t.left); int dRight = TreeDepthInternal(t.right); if (Math.Abs(dLeft - dRight) > 1) { return false; } else { return (TreeIsBalancedInternal1(t.left) && (TreeIsBalancedInternal1(t.right))); } }
private void InOrderTreeToQueue(Node t, Queue<Node> queue) { if (t != null) { InOrderTreeToQueue(t.left, queue); queue.Enqueue(t); InOrderTreeToQueue(t.right, queue); } }
public void Test_Remove_On_Node_With_Left_and_right_Leaves() { // Arrange BinaryTree bt = new BinaryTree(); Node n1 = new Node(17); Node n2 = new Node(23); // This is the node we will delete. Node n3 = new Node(21); Node n4 = new Node(29); bt.Insert(n1); bt.Insert(n2); // Node to delete. bt.Insert(n3); bt.Insert(n4); // Act bool removed = bt.Remove(23); // Assert Assert.IsTrue(removed); Queue<Node> queue = bt.TreeToQueue(); // To validate the remaining tree nodes. Assert.IsTrue(queue.Count == 3); Assert.IsTrue(17 == queue.Dequeue().iKey); Assert.IsTrue(21 == queue.Dequeue().iKey); Assert.IsTrue(29 == queue.Dequeue().iKey); }