Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVL_Tree implement in python #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions Trees/AVLTree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
class Node(object):
def __init__(self, data, left = None, right = None):
self.data = data
self.left = left
self.right = right
self.BF = 0 #Balance Factor


class AVL_Tree(object):
def __init__(self):
self.root = None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// initialize the self.root


def insert(self, data):
pNew = Node(data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// set the Node(data) as a pNew


if self.root == None:
Copy link

@kwanukwanu kwanukwanu Jun 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// if self.root == None, pNew becomes self.root

self.root = pNew
return True

a = self.root
pa = None
Cur = self.root
pCur = None
subroot = None

while Cur != None:
if Cur.data == data:
return False
if Cur.BF != 0:
a = Cur
pa = pCur
if data < Cur.data:
pCur = Cur
Cur = Cur.left
elif data > Cur.data:
pCur = Cur;
Cur = Cur.right

if data < pCur.data:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// if data is smaller than pCur.data, pNew becomes pCur.left

pCur.left = pNew
else:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// if not, pNew becomes pCur.right

pCur.right = pNew

balance = None
Ca = None
CCa = None

if data < a.data:
Cur = a.left
Ca = Cur
balance = 1
else:
Cur = a.right
Ca = Cur
balance = -1

while Cur != pNew:
if data < Cur.data:
Cur.BF = 1
Cur = Cur.left
else:
Cur.BF = -1
Cur = Cur.right

if a.BF == 0 or ((a.BF + balance) == 0):
a.BF = a.BF + balance
return True

if balance == 1:
if Ca.BF == 1:
a.left = Ca.right
Ca.right = a
a.BF = 0
Ca.BF = 0
subroot = Ca
else:
CCa = Ca.right
Ca.right = CCa.left
a.left = CCa.right
CCa.left = Ca
CCa.right = a

if CCa.BF == 0:
a.BF = 0
Ca.BF = 0
elif CCa.BF == 1:
a.BF = -1
Ca.BF = 0
elif CCa.BF == -1:
a.BF = 0
Ca.BF = 1

CCa.BF = 0
subroot = CCa
else:
if Ca.BF == -1:
a.right = Ca.left
Ca.left = a
a.BF = 0
Ca.BF = 0
subroot = Ca
else:
CCa = Ca.left
Ca.left = CCa.right
a.right = CCa.left
CCa.right = Ca
CCa.left = a

if CCa.BF == 0:
a.BF = 0
Ca.BF = 0
elif CCa.BF == 1:
a.BF = 0
Ca.BF = -1
elif CCa.BF == -1:
a.BF = 1
Ca.BF = 0
CCa.BF = 0
subroot = CCa

if pa == None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if pa == None, subroot becomes self.root

self.root = subroot
elif a == pa.left:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if a == pa.left, subroot becomes pa.left

pa.left = subroot
else:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else, subroot becomes pa.right

pa.right = subroot

return True

def in_order(self):
def _in_order(root):
if root:
_in_order(root.left)
print(root.data)
_in_order(root.right)
_in_order(self.root)

def pre_order(self):
def _pre_order(root):
if root:
print(root.data)
_pre_order(root.left)
_pre_order(root.right)
_pre_order(self.root)

def post_order(self):
def _post_order(root):
if root:
_post_order(root.left)
_post_order(root.right)
print(root.data)
_post_order(self.root)

if __name__ == '__main__':
avl_tree = AVL_Tree()
avl_tree.insert(40)
avl_tree.insert(4)
avl_tree.insert(34)
avl_tree.insert(45)
avl_tree.insert(14)
avl_tree.insert(55)
avl_tree.insert(48)
avl_tree.insert(13)
avl_tree.insert(15)
avl_tree.insert(49)
avl_tree.insert(47)

print("pre order")
avl_tree.pre_order()
print()
print("in order")
avl_tree.in_order()
print()
print("post order")
avl_tree.post_order()