Skip to content

Commit 393906f

Browse files
committed
Binary search tree lesson
1 parent 5379638 commit 393906f

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

5-trees/BST-mine.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Node(object):
2+
def __init__(self, value):
3+
self.value = value
4+
self.left = None
5+
self.right = None
6+
7+
class BST(object):
8+
def __init__(self, root):
9+
self.root = Node(root)
10+
11+
def insert(self, new_val):
12+
def insertNode(node, new_value):
13+
if new_value < node.value and node.left == None:
14+
node.left = Node(new_val)
15+
elif new_value > node.value and node.right == None:
16+
node.right = Node(new_val)
17+
elif node.value > new_val and node.left:
18+
insertNode(node.left, new_value)
19+
elif node.value < new_val and node.right:
20+
insertNode(node.right, new_value)
21+
insertNode(self.root, new_val)
22+
23+
def printSelf(self):
24+
def printNode(node):
25+
if node:
26+
print(node.value)
27+
printNode(node.left)
28+
printNode(node.right)
29+
printNode(self.root)
30+
31+
def search(self, find_val):
32+
def preorder(node, find_value):
33+
if node:
34+
if node.value == find_value:
35+
return True
36+
return preorder(node.left, find_value) or preorder(node.right, find_value)
37+
return False
38+
return preorder(self.root, find_val)
39+
40+
# Kind of wondering how insertion works if you have to rearrange order in the tree.
41+
# This problem didn't check for any of that.
42+
43+
# Set up tree
44+
tree = BST(4)
45+
46+
# Insert elements
47+
tree.insert(2)
48+
tree.insert(1)
49+
tree.insert(3)
50+
tree.insert(5)
51+
52+
#tree.printSelf()
53+
54+
# Check search
55+
# Should be True
56+
print tree.search(4)
57+
# Should be False
58+
print tree.search(6)

5-trees/BST.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class BST(object):
2+
def __init__(self, root):
3+
self.root = Node(root)
4+
5+
def insert(self, new_val):
6+
self.insert_helper(self.root, new_val)
7+
8+
def insert_helper(self, current, new_val):
9+
if current.value < new_val:
10+
if current.right:
11+
self.insert_helper(current.right, new_val)
12+
else:
13+
current.right = Node(new_val)
14+
else:
15+
if current.left:
16+
self.insert_helper(current.left, new_val)
17+
else:
18+
current.left = Node(new_val)
19+
20+
def search(self, find_val):
21+
return self.search_helper(self.root, find_val)
22+
23+
def search_helper(self, current, find_val):
24+
if current:
25+
if current.value == find_val:
26+
return True
27+
elif current.value < find_val:
28+
return self.search_helper(current.right, find_val)
29+
else:
30+
return self.search_helper(current.left, find_val)
31+
return False

5-trees/BSTNotes.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
### Binary Search Trees
2+
3+
The difference is that everything to the left of the root node is smaller than it and everything to its right is bigger.
4+
5+
Searching this tree if it is pretty balanced then is O(log(n)), because it depends on the height of the tree (which in a previous calculation was shown to be log(n)).
6+
7+
In the worst case, where it is completely unbalanced, it is basically like a line and it would be linear O(n) time.
8+
9+
10+
11+
I'm kind of curious on how shuffling the tree around works if I make inserts that require that reshuffling. The quiz where the implementation was done did not address this.

0 commit comments

Comments
 (0)