-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl_temp.py
41 lines (33 loc) · 1.23 KB
/
avl_temp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Node:
def __init__(self , data , parent):
self.data = data
self.parent = parent
self.leftChild = None
self.rightChild = None
self.height = 0
class AVL:
def __init__(self):
self.root = None
def insert(self , data):
if self.root is None:
self.root = Node(data , None)
else:
self.insert_node(self , data , self.root)
def insert_node(self, data , node):
#checking the left subtree
if node.data < data:
if node.leftChild:
self.insert_node(node.leftChild)
else:
node.leftChild = Node(data , node)
node.height = max(self.calculate_height(node.leftChild) ,
self.calculate_height(node.rightChild)) +1
if node.data < data:
if node.rightChild:
self.insert_node(data , node.rightChild)
else:
node.rightChild = Node(data , node)
node.height = max(self.calculate_height(node.leftChild) ,
self.calculate_height(node.rightChild)) +1
self.handle_violation(node)
def calculate_height(self , node):