Correction during revising session with Kim
This commit is contained in:
parent
ddea8c08f8
commit
5e64039f27
1 changed files with 70 additions and 19 deletions
89
tree.py
Normal file → Executable file
89
tree.py
Normal file → Executable file
|
@ -3,7 +3,6 @@
|
|||
|
||||
import sys
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, k):
|
||||
self.key = k
|
||||
|
@ -48,7 +47,14 @@ def reverse_walk(t):
|
|||
|
||||
# Complexity (worst): Theta(n)
|
||||
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)
|
||||
|
@ -92,13 +98,21 @@ def predecessor(t):
|
|||
|
||||
|
||||
def insert(t, k):
|
||||
if t is None:
|
||||
return Node(k)
|
||||
if t.key < t.key:
|
||||
t.set_left(insert(t.left, k))
|
||||
insert_node(t, Node(k))
|
||||
|
||||
|
||||
def insert_node(t, node):
|
||||
if node.key < t.key:
|
||||
if t.left is None:
|
||||
t.set_left(node)
|
||||
else:
|
||||
insert_node(t.left, node)
|
||||
else:
|
||||
tree.set_right(insert(t.right, k))
|
||||
return t
|
||||
if t.right is None:
|
||||
t.set_right(node)
|
||||
else:
|
||||
insert_node(t.right, node)
|
||||
|
||||
|
||||
def right_rotate(x):
|
||||
# assume x is not None
|
||||
|
@ -108,6 +122,7 @@ def right_rotate(x):
|
|||
t.right = x
|
||||
return t
|
||||
|
||||
|
||||
def left_rotate(x):
|
||||
# assume x is not None
|
||||
# assume x.right is not None
|
||||
|
@ -116,6 +131,7 @@ def left_rotate(x):
|
|||
t.left = x
|
||||
return t
|
||||
|
||||
|
||||
def root_insert(t, k):
|
||||
if t is None:
|
||||
return Node(k)
|
||||
|
@ -126,6 +142,36 @@ def root_insert(t, k):
|
|||
t.set_left(root_insert(t.left, k))
|
||||
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
|
||||
|
||||
|
@ -196,15 +242,20 @@ def print_w(t, width):
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print_tree(tree)
|
||||
|
||||
in_order_walk(tree)
|
||||
print()
|
||||
|
||||
reverse_walk(tree)
|
||||
print()
|
||||
|
||||
print(tree.left.right.right.key)
|
||||
print(successor(tree.left.right.right).key)
|
||||
print(predecessor(tree.left.right.right).key)
|
||||
args = [x for x in sys.argv[1:]]
|
||||
T = Node(int(args[0]))
|
||||
for i in range(1, len(args)):
|
||||
if args[i] == '|':
|
||||
break
|
||||
print_tree(T)
|
||||
print("\nInsert " + str(args[i]) + ":")
|
||||
insert(T, int(args[i]))
|
||||
for i in range(i+1, len(args)):
|
||||
print_tree(T)
|
||||
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)
|
||||
|
||||
|
|
Reference in a new issue