public BinaryTree(int value) { this.value = value; this.left = null; this.right = null; }
static void Main(string[] args) { Console.WriteLine('Z' > 'B'); Console.ReadLine(); //Console.WriteLine("Test Deletion method\n"); BinaryTree bt; //bt = new BinaryTree(); //bt.insert("50", 50); //bt.insert("60", 60); //bt.insert("40", 40); //bt.insert("30", 30); //bt.insert("20", 20); //bt.insert("35", 35); //bt.insert("45", 45); //bt.insert("44", 44); //bt.insert("46", 46); //Console.WriteLine("Number of nodes in the tree = {0}\n", bt.count()); //Console.WriteLine("Original: " + bt.drawTree()); //bt.delete("40"); //Console.WriteLine("Delete node 40: " + bt.drawTree()); //bt.delete("45"); //Console.WriteLine("Delete node 45: " + bt.drawTree()); //Console.WriteLine("\nSimple one layered tree"); //bt = new BinaryTree(); //bt.insert("50", 50); //bt.insert("20", 20); //bt.insert("90", 90); //Console.WriteLine("\nOriginal: " + bt.drawTree()); //bt.delete("50"); //Console.WriteLine("Delete node 50: " + bt.drawTree()); //bt = new BinaryTree(); //bt.insert("50", 50); //bt.insert("20", 20); //bt.insert("90", 90); //Console.WriteLine("\nOriginal: " + bt.drawTree()); //bt.delete("20"); //Console.WriteLine("Delete node 20: " + bt.drawTree()); //bt = new BinaryTree(); //bt.insert("50", 50); //bt.insert("20", 20); //bt.insert("90", 90); //Console.WriteLine("\nOriginal: " + bt.drawTree()); //bt.delete("90"); //Console.WriteLine("Delete node 90: " + bt.drawTree()); //bt.delete("20"); //Console.WriteLine("Delete node 20: " + bt.drawTree()); //bt.delete("50"); //Console.WriteLine("Delete node 50: " + bt.drawTree()); Console.WriteLine("\n"); bt = new BinaryTree(); bt.insert("L", 1); bt.insert("D", 2); bt.insert("C", 3); bt.insert("A", 4); bt.insert("H", 5); bt.insert("F", 6); bt.insert("J", 7); bt.insert("P", 8); Console.WriteLine("Original: " + bt.drawTree()); bt.delete("J"); Console.WriteLine("Delete J: " + bt.drawTree()); bt.delete("C"); Console.WriteLine("Delete C: " + bt.drawTree()); bt.delete("L"); Console.WriteLine("Delete L: " + bt.drawTree()); bt.delete("D"); Console.WriteLine("Delete D: " + bt.drawTree()); bt.delete("A"); Console.WriteLine("Delete A: " + bt.drawTree()); Console.ReadLine(); }
public bool Equals(BinaryTree Tree) { return(CheckEquals(Tree._root, _root)); }
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(); }
public BinaryTree(int value, BinaryTree parent) { this.value = value; this.parent = parent; }
static void Main(string[] args) { Console.WriteLine('Z' > 'B'); Console.ReadLine(); //Console.WriteLine("Test Deletion method\n"); BinaryTree bt; //bt = new BinaryTree(); //bt.insert("50", 50); //bt.insert("60", 60); //bt.insert("40", 40); //bt.insert("30", 30); //bt.insert("20", 20); //bt.insert("35", 35); //bt.insert("45", 45); //bt.insert("44", 44); //bt.insert("46", 46); //Console.WriteLine("Number of nodes in the tree = {0}\n", bt.count()); //Console.WriteLine("Original: " + bt.drawTree()); //bt.delete("40"); //Console.WriteLine("Delete node 40: " + bt.drawTree()); //bt.delete("45"); //Console.WriteLine("Delete node 45: " + bt.drawTree()); //Console.WriteLine("\nSimple one layered tree"); //bt = new BinaryTree(); //bt.insert("50", 50); //bt.insert("20", 20); //bt.insert("90", 90); //Console.WriteLine("\nOriginal: " + bt.drawTree()); //bt.delete("50"); //Console.WriteLine("Delete node 50: " + bt.drawTree()); //bt = new BinaryTree(); //bt.insert("50", 50); //bt.insert("20", 20); //bt.insert("90", 90); //Console.WriteLine("\nOriginal: " + bt.drawTree()); //bt.delete("20"); //Console.WriteLine("Delete node 20: " + bt.drawTree()); //bt = new BinaryTree(); //bt.insert("50", 50); //bt.insert("20", 20); //bt.insert("90", 90); //Console.WriteLine("\nOriginal: " + bt.drawTree()); //bt.delete("90"); //Console.WriteLine("Delete node 90: " + bt.drawTree()); //bt.delete("20"); //Console.WriteLine("Delete node 20: " + bt.drawTree()); //bt.delete("50"); //Console.WriteLine("Delete node 50: " + bt.drawTree()); Console.WriteLine("\n"); bt = new BinaryTree(); bt.insert("L", 1); bt.insert("D", 2); bt.insert("C", 3); bt.insert("A", 4); bt.insert("H", 5); bt.insert("F", 6); bt.insert("J", 7); bt.insert("P", 8); Console.WriteLine("Original: " + bt.drawTree()); bt.delete("J"); Console.WriteLine("Delete J: " + bt.drawTree()); bt.delete("C"); Console.WriteLine("Delete C: " + bt.drawTree()); bt.delete("L"); Console.WriteLine("Delete L: " + bt.drawTree()); bt.delete("D"); Console.WriteLine("Delete D: " + bt.drawTree()); bt.delete("A"); Console.WriteLine("Delete A: " + bt.drawTree()); Console.ReadLine(); }