-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLNode.py
More file actions
74 lines (59 loc) · 1.93 KB
/
AVLNode.py
File metadata and controls
74 lines (59 loc) · 1.93 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 4 20:39:40 2018
@author: JesusMHernandez
"""
class AVLNode():
def __init__(self, key):
self.key = key
self.parent = None
self.left = None
self.right = None
self.height = 0
def get_balance(self):
# Get current height of left subtree, or -1 if None
left_height = -1
if self.left is not None:
left_height = self.left.height
# Get current height of right subtree, or -1 if None
right_height = -1
if self.right is not None:
right_height = self.right.height
# Calculate the balance factor.
return left_height - right_height
def update_height(self):
# Get current height of left subtree, or -1 if None
left_height = -1
if self.left is not None:
left_height = self.left.height
# Get current height of right subtree, or -1 if None
right_height = -1
if self.right is not None:
right_height = self.right.height
# Assign self.height with calculated node height.
self.height = max(left_height, right_height) + 1
def set_child(self, which_child, child):
if which_child != "left" and which_child != "right":
return False
# Assign the left or right data member.
if which_child == "left":
self.left = child
else:
self.right = child
# Assign the parent data member of the new child,
# if the child is not None.
if child is not None:
child.parent = self
# Update the node's height, since the subtree's structure
# may have changed.
self.update_height()
return True
def replace_child(self, current_child, new_child):
if self.left is current_child:
return self.set_child("left", new_child)
elif self.right is current_child:
return self.set_child("right", new_child)
# If neither of the above cases applied, then the new child
# could not be attached to this node.
return False