Skip to content

Instantly share code, notes, and snippets.

@BarnabasMarkus
Created September 19, 2016 11:30
Show Gist options
  • Save BarnabasMarkus/3305c6a42af4da2169d30dac95f4f6d9 to your computer and use it in GitHub Desktop.
Save BarnabasMarkus/3305c6a42af4da2169d30dac95f4f6d9 to your computer and use it in GitHub Desktop.
binary search tree
#!/usr/bin/env python3
# B I N A R Y S E A R C H T R E E
# Project Binary Search Tree
# Author Barnabas Markus
# Email [email protected]
# Date 19.09.2016
# Python 3.5.1
# License MIT
class Tree:
def __init__(self, key, value=None):
self.key = key
self.value = value
self.left = None
self.right = None
def insert(self, key, value=None):
""" insert value if key does not exist, else update value """
if isinstance(key, list):
for k in key:
self.insert(k)
else:
if key == self.key:
# update key: value
self.value = value
elif key < self.key:
if self.left == None:
self.left = Tree(key, value)
else:
self.left.insert(key, value)
else:
if self.right == None:
self.right = Tree(key, value)
else:
self.right.insert(key, value)
def delete(self):
# TODO
pass
def search(self, key):
try:
if key == self.key:
return self.key, self.value
elif key < self.key:
return self.left.search(key)
else:
return self.right.search(key)
except AttributeError:
return None
def display(self, indent=' '):
print(indent, self.key)
if self.left:
self.left.display(indent+' ')
if self.right:
self.right.display(indent+' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment