|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import math |
| 3 | +class my_queue: |
| 4 | + def __init__(self): |
| 5 | + self.data = [] |
| 6 | + self.head = 0 |
| 7 | + self.tail = 0 |
| 8 | + def isEmpty(self): |
| 9 | + return self.head == self.tail |
| 10 | + def push(self,data): |
| 11 | + self.data.append(data) |
| 12 | + self.tail = self.tail + 1 |
| 13 | + def pop(self): |
| 14 | + ret = self.data[self.head] |
| 15 | + self.head = self.head + 1 |
| 16 | + return ret |
| 17 | + def count(self): |
| 18 | + return self.tail - self.head |
| 19 | + def print(self): |
| 20 | + print(self.data) |
| 21 | + print("**************") |
| 22 | + print(self.data[self.head:self.tail]) |
| 23 | + |
| 24 | +class my_node: |
| 25 | + def __init__(self,data): |
| 26 | + self.data = data |
| 27 | + self.left = None |
| 28 | + self.right = None |
| 29 | + self.height = 1 |
| 30 | + def getdata(self): |
| 31 | + return self.data |
| 32 | + def getleft(self): |
| 33 | + return self.left |
| 34 | + def getright(self): |
| 35 | + return self.right |
| 36 | + def getheight(self): |
| 37 | + return self.height |
| 38 | + def setdata(self,data): |
| 39 | + self.data = data |
| 40 | + return |
| 41 | + def setleft(self,node): |
| 42 | + self.left = node |
| 43 | + return |
| 44 | + def setright(self,node): |
| 45 | + self.right = node |
| 46 | + return |
| 47 | + def setheight(self,height): |
| 48 | + self.height = height |
| 49 | + return |
| 50 | + |
| 51 | +def getheight(node): |
| 52 | +# print("xxx") |
| 53 | + if node is None: |
| 54 | + return 0 |
| 55 | + return node.getheight() |
| 56 | + |
| 57 | +def my_max(a,b): |
| 58 | + if a > b: |
| 59 | + return a |
| 60 | + return b |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +def leftrotation(node): |
| 65 | + ret = node.getleft() |
| 66 | + node.setleft(ret.getright()) |
| 67 | + ret.setright(node) |
| 68 | + h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1 |
| 69 | + node.setheight(h1) |
| 70 | + h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1 |
| 71 | + ret.setheight(h2) |
| 72 | + return ret |
| 73 | + |
| 74 | +def rightrotation(node): |
| 75 | + ret = node.getright() |
| 76 | + node.setright(ret.getleft()) |
| 77 | + ret.setleft(node) |
| 78 | + h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1 |
| 79 | + node.setheight(h1) |
| 80 | + h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1 |
| 81 | + ret.setheight(h2) |
| 82 | + return ret |
| 83 | + |
| 84 | +def lrrotation(node): |
| 85 | + node.setright(leftrotation(node.getright())) |
| 86 | + return rightrotation(node) |
| 87 | + |
| 88 | +def rlrotation(node): |
| 89 | + node.setleft(rightrotation(node.getleft())) |
| 90 | + return leftrotation(node) |
| 91 | + |
| 92 | +def insert_node(node,data): |
| 93 | + if node is None: |
| 94 | + return my_node(data) |
| 95 | + if data < node.getdata(): |
| 96 | + node.setleft(insert_node(node.getleft(),data)) |
| 97 | + if getheight(node.getleft()) - getheight(node.getright()) == 2: |
| 98 | + if data < node.getleft().getdata(): |
| 99 | + node = leftrotation(node) |
| 100 | + else: |
| 101 | + node = rlrotation(node) |
| 102 | + else: |
| 103 | + node.setright(insert_node(node.getright(),data)) |
| 104 | + if getheight(node.getright()) - getheight(node.getleft()) == 2: |
| 105 | + if data < node.getright().getdata(): |
| 106 | + node = lrrotation(node) |
| 107 | + else: |
| 108 | + node = rightrotation(node) |
| 109 | + h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1 |
| 110 | + node.setheight(h1) |
| 111 | + return node |
| 112 | + |
| 113 | +class AVLtree: |
| 114 | + def __init__(self): |
| 115 | + self.root = None |
| 116 | + def getheight(self): |
| 117 | +# print("yyy") |
| 118 | + return getheight(self.root) |
| 119 | + def insert(self,data): |
| 120 | + self.root = insert_node(self.root,data) |
| 121 | + def traversale(self): |
| 122 | + q = my_queue() |
| 123 | + q.push(self.root) |
| 124 | + layer = self.getheight() |
| 125 | + cnt = 0 |
| 126 | + while not q.isEmpty(): |
| 127 | + node = q.pop() |
| 128 | + space = " "*int(math.pow(2,layer) - 1) |
| 129 | + print(space,end = " ") |
| 130 | + if node is None: |
| 131 | + print("*",end = " ") |
| 132 | + else: |
| 133 | + print(node.getdata(),end = " ") |
| 134 | + q.push(node.getleft()) |
| 135 | + q.push(node.getright()) |
| 136 | + print(space,end = " ") |
| 137 | + cnt = cnt + 1 |
| 138 | + for i in range(100): |
| 139 | + if cnt == math.pow(2,i) - 1: |
| 140 | + layer = layer -1 |
| 141 | + print() |
| 142 | + break |
| 143 | + print() |
| 144 | + print("*************************************") |
| 145 | + return |
| 146 | + |
| 147 | + def test(self): |
| 148 | + getheight(None) |
| 149 | + print("****") |
| 150 | + self.getheight() |
| 151 | +if __name__ == "__main__": |
| 152 | +# t = AVLtree() |
| 153 | +# t.test() |
| 154 | +# q = my_queue() |
| 155 | +## q.push(1) |
| 156 | +## q.push(2) |
| 157 | +# for i in range(10): |
| 158 | +# q.push(i) |
| 159 | +# q.print() |
| 160 | +# q.push(90) |
| 161 | +# q.print() |
| 162 | +# |
| 163 | +# q.pop() |
| 164 | +# q.print() |
| 165 | + t = AVLtree() |
| 166 | + t.traversale() |
| 167 | +# t.insert(7) |
| 168 | +# t.traversale() |
| 169 | + |
| 170 | + t.insert(8) |
| 171 | + t.traversale() |
| 172 | + t.insert(3) |
| 173 | + t.traversale() |
| 174 | + t.insert(6) |
| 175 | + t.traversale() |
| 176 | + t.insert(1) |
| 177 | + t.traversale() |
| 178 | + t.insert(10) |
| 179 | + t.traversale() |
| 180 | + t.insert(14) |
| 181 | + t.traversale() |
| 182 | + t.insert(13) |
| 183 | + t.traversale() |
| 184 | +# t.traversale() |
| 185 | + t.insert(4) |
| 186 | + t.traversale() |
| 187 | + t.insert(7) |
| 188 | + t.traversale() |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + |
0 commit comments