Skip to content

Commit 5379638

Browse files
committed
Began Lesson 5: Trees
1 parent 3c150a2 commit 5379638

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

5-trees/BinaryTree-mine.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Node(object):
2+
def __init__(self, value):
3+
self.value = value
4+
self.left = None
5+
self.right = None
6+
7+
class BinaryTree(object):
8+
def __init__(self, root):
9+
self.root = Node(root)
10+
11+
def search(self, find_val):
12+
"""Return True if the value
13+
is in the tree, return
14+
False otherwise."""
15+
if self.preorder_search(self.root, find_val):
16+
return True
17+
return False
18+
19+
def print_tree(self):
20+
"""Print out all tree nodes
21+
as they are visited in
22+
a pre-order traversal."""
23+
myList = []
24+
self.preorder_print(self.root, myList)
25+
print('-'.join(myList))
26+
return ""
27+
28+
def preorder_search(self, start, find_val):
29+
"""Helper method - use this to create a
30+
recursive search solution."""
31+
if start:
32+
if start.value == find_val:
33+
return True
34+
return self.preorder_search(start.left, find_val) or self.preorder_search(start.right, find_val)
35+
return False
36+
37+
def preorder_print(self, start, traversal):
38+
"""Helper method - use this to create a
39+
recursive print solution."""
40+
if start:
41+
traversal.append(str(start.value))
42+
self.preorder_print(start.left, traversal)
43+
self.preorder_print(start.right, traversal)
44+
return traversal
45+
46+
47+
# Set up tree
48+
tree = BinaryTree(1)
49+
tree.root.left = Node(2)
50+
tree.root.right = Node(3)
51+
tree.root.left.left = Node(4)
52+
tree.root.left.right = Node(5)
53+
54+
# Test search
55+
# Should be True
56+
print tree.search(4)
57+
# Should be False
58+
print tree.search(6)
59+
60+
# Test print_tree
61+
# Should be 1-2-4-5-3
62+
print tree.print_tree()

5-trees/BinaryTree.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class BinaryTree(object):
2+
def __init__(self, root):
3+
self.root = Node(root)
4+
5+
def search(self, find_val):
6+
return self.preorder_search(tree.root, find_val)
7+
8+
def print_tree(self):
9+
return self.preorder_print(tree.root, "")[:-1]
10+
11+
def preorder_search(self, start, find_val):
12+
if start:
13+
if start.value == find_val:
14+
return True
15+
else:
16+
return self.preorder_search(start.left, find_val) or self.preorder_search(start.right, find_val)
17+
return False
18+
19+
def preorder_print(self, start, traversal):
20+
if start:
21+
traversal += (str(start.value) + "-")
22+
traversal = self.preorder_print(start.left, traversal)
23+
traversal = self.preorder_print(start.right, traversal)
24+
return traversal

5-trees/TreeNotes.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
### Trees
2+
3+
Trees are like linked lists except they can have more than one next.
4+
5+
- Trees must be completely connected
6+
- Trees must not have cycles
7+
8+
The level of a tree starts counting at 1 from the root node.
9+
The height of a tree starts counting from the leaf nodes as 0, upwards.
10+
The depth of a tree is the number of edges (connections), which starts at 0 from the root node.
11+
12+
Identical terms:
13+
- Parent/Child
14+
- Ancestor/Descendent
15+
- Internal/Leaf
16+
17+
### Quiz
18+
19+
a
20+
/ \
21+
b c
22+
/ /
23+
d e
24+
\
25+
f
26+
27+
Level of D? 3
28+
Parent of C? A
29+
Height of B? 2
30+
Depth of F? 3
31+
32+
### Tree Traversal
33+
34+
DFS - depth first search, can be
35+
- preorder: root, L, R
36+
- inorder: L, root, R
37+
- postorder: L, R, root (commonly used for deleting)
38+
39+
BFS - breadth first search, popularly
40+
- search by level, left to right each level
41+
42+
### Quiz
43+
44+
a
45+
/ \
46+
b c
47+
/ \
48+
d e
49+
\
50+
f
51+
52+
Write out the post order traversal.
53+
D, F, E, B, C, A
54+
55+
### Binary Trees
56+
57+
Each node can have at most 2 children.
58+
59+
**Searching:**
60+
Any of the DFS or BFS traversals work here. O(n), having to look through til found.
61+
62+
**Deleting:**
63+
Often starts off with a search to find the thing to delete.
64+
When deleting, have to consider the children if you delete an internal node (parent).
65+
If it's got one kid, you can just promote the kid up.
66+
If it's got two, you will have to decide how to shift around the elements. You can search til you hit a leaf and promote the leaf up if you don't care about order.
67+
68+
**Inserting:**
69+
If there's no order, just stick it in as a child somewhere. Move down tree from root looking for an open spot.
70+
Worse case is O(height). What's the height of the binary tree?
71+
72+
A perfect tree has all children for every parent til the even leaf nodes at the end. Perfect trees have nodes equal to 2^(level-1).
73+
74+
Powers of 2 remind us of log(n).

0 commit comments

Comments
 (0)