Skip to content

fix some style errors in Binary Tree #95

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

Merged
merged 4 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
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
131 changes: 131 additions & 0 deletions data_structures/AVL/AVL.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
'''
A AVL tree
'''


class Node:

def __init__(self, label):
self.label = label
self.left = None
self.rigt = None
self.parent = None
self.height = 0

def getLabel(self):
return self.label

def setLabel(self, label):
self.label = label

def getLeft(self):
return self.left

def setLeft(self, left):
self.left = left

def getRight(self):
return self.rigt

def setRight(self, right):
self.rigt = right

def getParent(self):
return self.parent

def setParent(self, parent):
self.parent = parent

def setHeight(self, height):
self.height = height

def getHeight(self, height):
return self.height


class AVL:

def __init__(self):
self.root = None
self.size = 0

def insert(self, value):
node = Node(value)
if self.root is None:
self.root = node
self.size = 1
else:
# Same as Binary Tree
dad_node = None
curr_node = self.root

while True:
if curr_node is not None:

dad_node = curr_node

if node.getLabel() < curr_node.getLabel():
curr_node = curr_node.getLeft()
else:
curr_node = curr_node.getRight()
else:
if node.getLabel() < dad_node.getLabel():
dad_node.setLeft(node)
dad_node.setHeight(dad_node.getHeight() + 1)

if (dad_node.getRight().getHeight() -
dad_node.getLeft.getHeight() > 1):
self.rebalance(dad_node)

else:
dad_node.setRight(node)
dad_node.setHeight(dad_node.getHeight() + 1)

if (dad_node.getRight().getHeight() -
dad_node.getLeft.getHeight() > 1):
self.rebalance(dad_node)
break

def rebalance(self, node):
if (node.getRight().getHeight() -
node.getLeft.getHeight() > 1):
if (node.getRight().getHeight() >
node.getLeft.getHeight()):
pass
else:
pass
pass
elif (node.getRight().getHeight() -
node.getLeft.getHeight() > 2):
if (node.getRight().getHeight() >
node.getLeft.getHeight()):
pass
else:
pass
pass
pass

def rotate_left(self, node):
# TODO: is this pythonic enought?
aux = node.getLabel()
node = aux.getRight()
node.setHeight(node.getHeight() - 1)
node.setLeft(Node(aux))
node.getLeft().setHeight(node.getHeight() + 1)
node.getRight().setHeight(node.getRight().getHeight() - 1)

def rotate_right(self, node):
aux = node.getLabel()
node = aux.getLeft()
node.setHeight(node.getHeight() - 1)
node.setRight(Node(aux))
node.getLeft().setHeight(node.getHeight() + 1)
node.getLeft().setHeight(node.getLeft().getHeight() - 1)

def double_rotate_left(self, node):
self.rotate_right(node.getRight().getRight())
self.rotate_left(node)

def double_rotate_right(self, node):
self.rotate_left(node.getLeft().getLeft())
self.rotate_right(node)
10 changes: 6 additions & 4 deletions data_structures/Binary Tree/binary_seach_tree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'''
A binary search Tree
'''


class Node:

def __init__(self, label):
Expand Down Expand Up @@ -34,7 +36,7 @@ def __init__(self):

def insert(self, label):

#Create a new Node
# Create a new Node

node = Node(label)

Expand All @@ -45,7 +47,7 @@ def insert(self, label):
curr_node = self.root

while True:
if curr_node != None:
if curr_node is not None:

dad_node = curr_node

Expand All @@ -61,12 +63,12 @@ def insert(self, label):
break

def empty(self):
if self.root == None:
if self.root is None:
return True
return False

def preShow(self, curr_node):
if curr_node != None:
if curr_node is None:
print(curr_node.getLabel(), end=" ")

self.preShow(curr_node.getLeft())
Expand Down