public static void PreOrderTraversal(BinaryTreeNode node) { if (node == null) return; Debug.Print(node.Value.ToString(CultureInfo.InvariantCulture)); PreOrderTraversal(node.Left); PreOrderTraversal(node.Right); }
public void VerifyNodeParent() { BinaryTreeNode parent = new BinaryTreeNode(0); BinaryTreeNode node = new BinaryTreeNode(1, parent); Assert.Equal(parent, node.Parent); }
public void VerifyNodeData() { BinaryTreeNode node = new BinaryTreeNode(0); Assert.Equal(0, node.Data); }
public bool RemoveNode(int data) { BinaryTreeNode targetNode = SearchNode(data); if (targetNode == null) { return(false); } else { BinaryTreeNode tempNode; if ((targetNode.LeftNode == null) && (targetNode.RightNode == null)) { if (targetNode.Parent.LeftNode == targetNode) { targetNode.Parent.LeftNode = null; } else if (targetNode.Parent.RightNode == targetNode) { targetNode.Parent.RightNode = null; } } else if ((targetNode.LeftNode != null) || (targetNode.RightNode != null)) { if ((targetNode.LeftNode != null) && (targetNode.RightNode != null)) { if (targetNode.Parent.LeftNode == targetNode) { tempNode = GetSmallNode(targetNode.RightNode); RemoveNode(tempNode.Data); targetNode.Data = tempNode.Data; } else if (targetNode.Parent.RightNode == targetNode) { tempNode = GetSmallNode(targetNode.RightNode); RemoveNode(tempNode.Data); targetNode.Data = tempNode.Data; } return(true); } if (targetNode.Parent.LeftNode == targetNode) { if (targetNode.LeftNode != null) { targetNode.Parent.LeftNode = targetNode.LeftNode; } else { targetNode.Parent.LeftNode = targetNode.RightNode; } } else if (targetNode.Parent.RightNode == targetNode) { if (targetNode.LeftNode != null) { targetNode.Parent.RightNode = targetNode.LeftNode; } else { targetNode.Parent.RightNode = targetNode.RightNode; } } } } return(true); }
public BinaryTree(int rootData) { _rootNode = new BinaryTreeNode(rootData); }
public void Delete(int _val) { BinaryTreeNode curNode = root; if (curNode == null) { Console.WriteLine("BST is empty!!!"); } else { while (true) { if (curNode.val == _val) { if (curNode.left != null) { if (curNode.right != null) { BinaryTreeNode leftMaxNode = curNode.left; while (leftMaxNode.right != null) { leftMaxNode = leftMaxNode.right; } curNode.val = leftMaxNode.val; if (leftMaxNode.left != null) { leftMaxNode.left.parent = leftMaxNode.parent; leftMaxNode.parent.right = leftMaxNode.left; } else { leftMaxNode.parent.right = null; } } else { curNode.left.parent = curNode.parent; if (curNode.parent.left == curNode) { curNode.parent.left = curNode.left; } else { curNode.parent.right = curNode.left; } } } else { if (curNode.right != null) { curNode.right.parent = curNode.parent; if (curNode.parent.left == curNode) { curNode.parent.left = curNode.right; } else { curNode.parent.right = curNode.right; } } else { if (curNode.parent.left == curNode) { curNode.parent.left = null; } else { curNode.parent.right = null; } } } break; } else if (curNode.val > _val) { if (curNode.left == null) { Console.WriteLine(_val + " is not BST!!!"); break; } else { curNode = curNode.left; } } else { if (curNode.right == null) { Console.WriteLine(_val + " is not BST!!!"); break; } else { curNode = curNode.right; } } } } }
public void SetRoot(BinaryTreeNode rootNode) { root = rootNode; }
static void Main(string[] args) { // travels binary tree BinaryTreeNode tmpNode1 = new BinaryTreeNode(1); BinaryTreeNode tmpNode2 = new BinaryTreeNode(2); BinaryTreeNode tmpNode3 = new BinaryTreeNode(3); BinaryTreeNode tmpNode4 = new BinaryTreeNode(4); BinaryTreeNode tmpNode5 = new BinaryTreeNode(5); BinaryTreeNode tmpNode6 = new BinaryTreeNode(6); BinaryTreeNode tmpNode7 = new BinaryTreeNode(7); BinaryTree bTree = new BinaryTree(); bTree.SetRoot(tmpNode1); bTree.Insert(tmpNode1, tmpNode2, tmpNode3); bTree.Insert(tmpNode2, tmpNode4, tmpNode5); bTree.Insert(tmpNode3, tmpNode6, tmpNode7); bTree.TravelsByPre(); Console.WriteLine(); bTree.TravelsByIn(); Console.WriteLine(); bTree.TravelsByPost(); Console.WriteLine(); // test heap by link HeapByLinked myHeapByLinked = new HeapByLinked(); myHeapByLinked.Insert(0); myHeapByLinked.Insert(1); myHeapByLinked.Insert(2); myHeapByLinked.Insert(3); myHeapByLinked.Insert(4); myHeapByLinked.Insert(5); myHeapByLinked.Insert(6); myHeapByLinked.TravelsByPre(); Console.WriteLine(); Console.WriteLine(myHeapByLinked.Delete()); myHeapByLinked.TravelsByPre(); Console.WriteLine(); // test heap by array HeapByArray myHeapByArray = new HeapByArray(); myHeapByArray.Insert(0); myHeapByArray.Insert(1); myHeapByArray.Insert(2); myHeapByArray.Insert(3); myHeapByArray.Insert(4); myHeapByArray.Insert(5); myHeapByArray.Insert(6); myHeapByArray.Print(); Console.WriteLine(myHeapByArray.Delete()); myHeapByArray.Print(); //Binary Search Tree Console.WriteLine("-----------------"); Console.WriteLine("It's BST Test!!!"); BST myBST = new BST(); myBST.Insert(50); myBST.Insert(20); myBST.Insert(70); myBST.Insert(30); myBST.Insert(10); myBST.Insert(60); myBST.Insert(80); myBST.Insert(5); myBST.Insert(15); myBST.Insert(25); myBST.Insert(35); myBST.Insert(55); myBST.Insert(65); myBST.Insert(75); myBST.Insert(85); myBST.Insert(90); myBST.Insert(3); myBST.Insert(8); myBST.TravelsByIn(); Console.WriteLine(myBST.Search(4)); Console.WriteLine(myBST.Search(5)); myBST.Delete(8); myBST.TravelsByIn(); myBST.Delete(20); myBST.TravelsByIn(); myBST.Delete(4); myBST.TravelsByIn(); //Binary Search Tree derived by Binary Tree Console.WriteLine("-----------------"); Console.WriteLine("It's BST Test!!!"); BSTDerivedBT myBSTDerived = new BSTDerivedBT(); myBSTDerived.Insert(50); myBSTDerived.Insert(20); myBSTDerived.Insert(70); myBSTDerived.Insert(30); myBSTDerived.Insert(10); myBSTDerived.Insert(60); myBSTDerived.Insert(80); myBSTDerived.Insert(5); myBSTDerived.Insert(15); myBSTDerived.Insert(25); myBSTDerived.Insert(35); myBSTDerived.Insert(55); myBSTDerived.Insert(65); myBSTDerived.Insert(75); myBSTDerived.Insert(85); myBSTDerived.Insert(90); myBSTDerived.Insert(3); myBSTDerived.Insert(8); myBSTDerived.TravelsByIn(); Console.WriteLine(myBSTDerived.Search(4)); Console.WriteLine(myBSTDerived.Search(5)); myBSTDerived.Delete(8); myBSTDerived.TravelsByIn(); myBSTDerived.Delete(20); myBSTDerived.TravelsByIn(); myBSTDerived.Delete(4); myBSTDerived.TravelsByIn(); }
/// <summary> /// Clears the binary search tree. /// </summary> public void Clear() { _root = null; _count = 0; }
public BinaryTreeNode(int data, BinaryTreeNode parent) : this(data) { Parent = parent; }