|
18 | 18 | } |
19 | 19 | ], |
20 | 20 | "source": [ |
21 | | - "class Heap(object):\n", |
22 | 21 | "\n", |
23 | | - "\tHEAP_SIZE = 10\n", |
24 | | - "\t\n", |
| 22 | + "class Node(object):\n", |
| 23 | + "\n", |
| 24 | + "\tdef __init__(self, data):\n", |
| 25 | + "\t\tself.data = data;\n", |
| 26 | + "\t\tself.leftChild = None;\n", |
| 27 | + "\t\tself.rightChild = None;\n", |
| 28 | + "\t\t\n", |
| 29 | + "class BinarySearchTree(object):\n", |
| 30 | + "\n", |
25 | 31 | "\tdef __init__(self):\n", |
26 | | - "\t\tself.heap = [0]*Heap.HEAP_SIZE;\n", |
27 | | - "\t\tself.currentPosition = -1;\n", |
| 32 | + "\t\tself.root = None;\n", |
28 | 33 | "\t\t\n", |
29 | | - "\tdef insert(self, item):\n", |
| 34 | + "\tdef insert(self, data):\n", |
| 35 | + "\t\tif not self.root:\n", |
| 36 | + "\t\t\tself.root = Node(data);\n", |
| 37 | + "\t\telse:\n", |
| 38 | + "\t\t\tself.insertNode(data, self.root);\n", |
| 39 | + "\t\t\t\n", |
| 40 | + "\t# O(logN) if the tree is balanced !!!!!!!!!!!!! --> it can reduced to O(N) --> AVL RBT are needed !!!!!\n", |
| 41 | + "\tdef insertNode(self, data, node):\n", |
| 42 | + "\t\n", |
| 43 | + "\t\tif data < node.data:\n", |
| 44 | + "\t\t\tif node.leftChild:\n", |
| 45 | + "\t\t\t\tself.insertNode(data, node.leftChild);\n", |
| 46 | + "\t\t\telse:\n", |
| 47 | + "\t\t\t\tnode.leftChild = Node(data);\n", |
| 48 | + "\t\telse:\n", |
| 49 | + "\t\t\tif node.rightChild:\n", |
| 50 | + "\t\t\t\tself.insertNode(data, node.rightChild);\n", |
| 51 | + "\t\t\telse:\n", |
| 52 | + "\t\t\t\tnode.rightChild = Node(data);\n", |
30 | 53 | "\t\n", |
31 | | - "\t\tif self.isFull():\n", |
32 | | - "\t\t\tprint(\"Heap is full..\");\n", |
33 | | - "\t\t\treturn \n", |
| 54 | + "\t# O(logN)\t\n", |
| 55 | + "\tdef removeNode(self, data, node):\n", |
| 56 | + "\t\n", |
| 57 | + "\t\tif not node:\n", |
| 58 | + "\t\t\treturn node;\n", |
| 59 | + "\t\t\t\n", |
| 60 | + "\t\tif data < node.data:\n", |
| 61 | + "\t\t\tnode.leftChild = self.removeNode(data, node.leftChild);\n", |
| 62 | + "\t\telif data > node.data:\n", |
| 63 | + "\t\t\tnode.rightChild = self.removeNode(data, node.rightChild);\n", |
| 64 | + "\t\telse:\n", |
| 65 | + "\t\t\t\n", |
| 66 | + "\t\t\tif not node.leftChild and not node.rightChild:\n", |
| 67 | + "\t\t\t\tprint(\"Removing a leaf node...\");\n", |
| 68 | + "\t\t\t\tdel node;\n", |
| 69 | + "\t\t\t\treturn None;\n", |
| 70 | + "\t\t\t\t\n", |
| 71 | + "\t\t\tif not node.leftChild: # node !!!\n", |
| 72 | + "\t\t\t\tprint(\"Removing a node with single right child...\");\n", |
| 73 | + "\t\t\t\ttempNode = node.rightChild;\n", |
| 74 | + "\t\t\t\tdel node;\n", |
| 75 | + "\t\t\t\treturn tempNode;\n", |
| 76 | + "\t\t\telif not node.rightChild: # node instead of self\n", |
| 77 | + "\t\t\t\tprint(\"Removing a node with single left child...\");\n", |
| 78 | + "\t\t\t\ttempNode = node.leftChild;\n", |
| 79 | + "\t\t\t\tdel node;\n", |
| 80 | + "\t\t\t\treturn tempNode;\n", |
34 | 81 | "\t\t\t\n", |
35 | | - "\t\tself.currentPosition = self.currentPosition + 1\n", |
36 | | - "\t\tself.heap[self.currentPosition] = item\n", |
37 | | - "\t\tself.fixUp(self.currentPosition)\n", |
| 82 | + "\t\t\tprint(\"Removing node with two children....\");\n", |
| 83 | + "\t\t\ttempNode = self.getPredecessor(node.leftChild); # self instead of elf + get predecessor \n", |
| 84 | + "\t\t\tnode.data = tempNode.data;\n", |
| 85 | + "\t\t\tnode.leftChild = self.removeNode(tempNode.data, node.leftChild);\n", |
38 | 86 | "\t\t\n", |
39 | | - "\tdef fixUp(self, index):\n", |
| 87 | + "\t\treturn node; # !!!!!!!!!!!!\n", |
| 88 | + "\n", |
| 89 | + "\tdef getPredecessor(self, node):\n", |
40 | 90 | "\t\n", |
41 | | - "\t\tparentIndex = int((index-1)/2)\n", |
| 91 | + "\t\tif node.rightChild:\n", |
| 92 | + "\t\t\treturn self.getPredeccor(node.rightChild);\n", |
| 93 | + "\t\t\t\n", |
| 94 | + "\t\treturn node;\n", |
42 | 95 | "\t\t\n", |
43 | | - "\t\twhile parentIndex >= 0 and self.heap[parentIndex] < self.heap[index]:\n", |
44 | | - "\t\t\ttemp = self.heap[index]\n", |
45 | | - "\t\t\tself.heap[index] = self.heap[parentIndex]\n", |
46 | | - "\t\t\tself.heap[parentIndex] = temp\n", |
47 | | - "\t\t\tindex=parentIndex\n", |
48 | | - "\t\t\tparentIndex = (int)((index-1)/2)\n", |
| 96 | + "\tdef remove(self, data):\n", |
| 97 | + "\t\tif self.root:\n", |
| 98 | + "\t\t\tself.root = self.removeNode(data, self.root);\n", |
| 99 | + "\t\t\t\n", |
| 100 | + "\t\t# O(logN)\n", |
| 101 | + "\tdef getMinValue(self):\n", |
| 102 | + "\t\tif self.root:\n", |
| 103 | + "\t\t\treturn self.getMin(self.root);\n", |
49 | 104 | "\t\t\t\n", |
50 | | - "\tdef heapsort(self):\n", |
| 105 | + "\tdef getMin(self, node):\n", |
51 | 106 | "\t\n", |
52 | | - "\t\tfor i in range(0,self.currentPosition+1):\n", |
53 | | - "\t\t\ttemp = self.heap[0]\n", |
54 | | - "\t\t\tprint(\"%d \" % temp)\n", |
55 | | - "\t\t\tself.heap[0] = self.heap[self.currentPosition-i]\n", |
56 | | - "\t\t\tself.heap[self.currentPosition-i] = temp\n", |
57 | | - "\t\t\tself.fixDown(0,self.currentPosition-i-1)\n", |
| 107 | + "\t\tif node.leftChild:\n", |
| 108 | + "\t\t\treturn self.getMin(node.leftChild);\n", |
58 | 109 | "\t\t\t\n", |
59 | | - "\tdef fixDown(self, index, upto):\n", |
60 | | - "\n", |
61 | | - "\t\twhile index <= upto:\n", |
| 110 | + "\t\treturn node.data;\n", |
62 | 111 | "\t\t\n", |
63 | | - "\t\t\tleftChild = 2*index+1\n", |
64 | | - "\t\t\trightChild = 2*index+2\n", |
| 112 | + "\t\t# O(logN)\n", |
| 113 | + "\tdef getMaxValue(self):\n", |
| 114 | + "\t\tif self.root:\n", |
| 115 | + "\t\t\treturn self.getMax(self.root);\n", |
| 116 | + "\t\t\t\n", |
| 117 | + "\tdef getMax(self, node):\n", |
65 | 118 | "\t\n", |
66 | | - "\t\t\tif leftChild < upto:\n", |
67 | | - "\t\t\t\tchildToSwap = None\n", |
68 | | - "\t\t\t\t\n", |
69 | | - "\t\t\t\tif rightChild > upto:\n", |
70 | | - "\t\t\t\t\tchildToSwap = leftChild\n", |
71 | | - "\t\t\t\telse:\n", |
72 | | - "\t\t\t\t\tif self.heap[leftChild] > self.heap[rightChild]:\n", |
73 | | - "\t\t\t\t\t\tchildToSwap = leftChild\n", |
74 | | - "\t\t\t\t\telse:\n", |
75 | | - "\t\t\t\t\t\tchildToSwap = rightChild\n", |
76 | | - "\t\t\t\t\n", |
77 | | - "\t\t\t\tif self.heap[index] < self.heap[childToSwap]:\n", |
78 | | - "\t\t\t\t\ttemp = self.heap[index]\n", |
79 | | - "\t\t\t\t\tself.heap[index] = self.heap[childToSwap]\n", |
80 | | - "\t\t\t\t\tself.heap[childToSwap] = temp\n", |
81 | | - "\t\t\t\telse:\n", |
82 | | - "\t\t\t\t\tbreak\n", |
83 | | - "\t\t\t\t\n", |
84 | | - "\t\t\t\tindex = childToSwap\n", |
85 | | - "\t\t\telse:\n", |
86 | | - "\t\t\t\tbreak;\t\t\t\t\t\t\t\n", |
| 119 | + "\t\tif node.rightChild:\n", |
| 120 | + "\t\t\treturn self.getMax(node.rightChild);\n", |
87 | 121 | "\t\t\t\n", |
88 | | - "\tdef isFull(self):\n", |
89 | | - "\t\tif self.currentPosition == Heap.HEAP_SIZE:\n", |
90 | | - "\t\t\treturn True\n", |
91 | | - "\t\telse:\n", |
92 | | - "\t\t\treturn False\n", |
| 122 | + "\t\treturn node.data;\n", |
| 123 | + "\t\t\n", |
| 124 | + "\tdef traverse(self):\n", |
| 125 | + "\t\tif self.root:\n", |
| 126 | + "\t\t\tself.traverseInOrder(self.root);\n", |
93 | 127 | "\t\t\t\n", |
| 128 | + "\t\t\t# O(N)\n", |
| 129 | + "\tdef traverseInOrder(self, node):\n", |
| 130 | + "\t\n", |
| 131 | + "\t\tif node.leftChild:\n", |
| 132 | + "\t\t\tself.traverseInOrder(node.leftChild);\n", |
94 | 133 | "\t\t\t\n", |
95 | | - "if __name__ == \"__main__\":\n", |
| 134 | + "\t\tprint(\"%s \" % node.data);\n", |
| 135 | + "\t\t\n", |
| 136 | + "\t\tif node.rightChild:\n", |
| 137 | + "\t\t\tself.traverseInOrder(node.rightChild);\n", |
| 138 | + "\t\t\t\n", |
| 139 | + "\t\t\t\n", |
| 140 | + "bst = BinarySearchTree();\n", |
| 141 | + "bst.insert(10);\n", |
| 142 | + "bst.insert(13);\n", |
| 143 | + "bst.insert(5);\n", |
| 144 | + "bst.insert(14);\n", |
| 145 | + "bst.remove(10);\n", |
96 | 146 | "\n", |
97 | | - "\theap = Heap()\n", |
98 | | - "\theap.insert(10)\n", |
99 | | - "\theap.insert(-20)\n", |
100 | | - "\theap.insert(0)\n", |
101 | | - "\theap.insert(2)\n", |
102 | | - "\theap.insert(4)\n", |
103 | | - "\theap.insert(5)\n", |
104 | | - "\theap.insert(6)\n", |
105 | | - "\theap.insert(7)\n", |
106 | | - "\theap.insert(20)\n", |
107 | | - "\theap.insert(15)\n", |
108 | | - "\t\n", |
109 | | - "\theap.heapsort()" |
| 147 | + "bst.traverse();" |
110 | 148 | ] |
111 | 149 | } |
112 | 150 | ], |
|
0 commit comments