Correction during revising session with Kim

This commit is contained in:
Claudio Maggioni 2019-05-08 17:45:03 +02:00
parent ddea8c08f8
commit 5e64039f27

89
tree.py Normal file → Executable file
View file

@ -3,7 +3,6 @@
import sys import sys
class Node: class Node:
def __init__(self, k): def __init__(self, k):
self.key = k self.key = k
@ -48,7 +47,14 @@ def reverse_walk(t):
# Complexity (worst): Theta(n) # Complexity (worst): Theta(n)
def search(tree, k): def search(tree, k):
pass if tree is None:
return None
elif tree.key == k:
return tree
elif k < tree.key:
return search(tree.left, k)
else:
return search(tree.right, k)
# Complexity (worst): Theta(n) # Complexity (worst): Theta(n)
@ -92,13 +98,21 @@ def predecessor(t):
def insert(t, k): def insert(t, k):
if t is None: insert_node(t, Node(k))
return Node(k)
if t.key < t.key:
t.set_left(insert(t.left, k)) def insert_node(t, node):
if node.key < t.key:
if t.left is None:
t.set_left(node)
else: else:
tree.set_right(insert(t.right, k)) insert_node(t.left, node)
return t else:
if t.right is None:
t.set_right(node)
else:
insert_node(t.right, node)
def right_rotate(x): def right_rotate(x):
# assume x is not None # assume x is not None
@ -108,6 +122,7 @@ def right_rotate(x):
t.right = x t.right = x
return t return t
def left_rotate(x): def left_rotate(x):
# assume x is not None # assume x is not None
# assume x.right is not None # assume x.right is not None
@ -116,6 +131,7 @@ def left_rotate(x):
t.left = x t.left = x
return t return t
def root_insert(t, k): def root_insert(t, k):
if t is None: if t is None:
return Node(k) return Node(k)
@ -126,6 +142,36 @@ def root_insert(t, k):
t.set_left(root_insert(t.left, k)) t.set_left(root_insert(t.left, k))
return right_rotate(t) return right_rotate(t)
def unlink_me(node, to_link):
if node.parent == None:
node.key = to_link.key
node.left = to_link.left
node.right = to_link.right
elif node.parent.left == node:
node.parent.left = to_link
else:
node.parent.right = to_link
def delete(t, k):
to_delete = search(t, k)
if to_delete is None:
return
elif to_delete.left is None:
unlink_me(to_delete, to_delete.right)
elif to_delete.right is None:
unlink_me(to_delete, to_delete.left)
else:
if abs(to_delete.left.key - to_delete.key) < abs(to_delete.right.key -
to_delete.key):
unlink_me(to_delete, to_delete.left)
insert_node(to_delete.left, to_delete.right)
else:
unlink_me(to_delete, to_delete.right)
insert_node(to_delete.right, to_delete.left)
############################################################################### ###############################################################################
# Code for printing trees, ignore this # Code for printing trees, ignore this
@ -196,15 +242,20 @@ def print_w(t, width):
if __name__ == "__main__": if __name__ == "__main__":
print_tree(tree) args = [x for x in sys.argv[1:]]
T = Node(int(args[0]))
in_order_walk(tree) for i in range(1, len(args)):
print() if args[i] == '|':
break
reverse_walk(tree) print_tree(T)
print() print("\nInsert " + str(args[i]) + ":")
insert(T, int(args[i]))
print(tree.left.right.right.key) for i in range(i+1, len(args)):
print(successor(tree.left.right.right).key) print_tree(T)
print(predecessor(tree.left.right.right).key) print("\nDelete " + str(args[i]) + ":")
delete(T, int(args[i]))
print_tree(T)
#print(tree.left.right.right.key)
#print(successor(tree.left.right.right).key)
#print(predecessor(tree.left.right.right).key)