11b Binary Tree &
Binary Search Tree
1
What is a Tree?
• A non-linear data structure whose entries follow a hierarchical
organization.
• Contains a set of nodes connected by edges.
Nodes
Hierarchical
organisation
Edges
Binary Tree data structure
• Every node has one parent, except for the root.
• Every node can have zero, one or at most two children
Parent Root
Left Child
Parent
Right Child
Left Child Right Child
3
Binary Tree data structure
• Children from the same parent are called siblings.
• Nodes with no children are called leaves. Root
Parent Leaves
Siblings
Parent
Siblings
4
Binary Tree data structure
• Every node contains two subtrees which are also binary trees.
Right subtree
Left subtree
5
Binary Tree data structure
• The number of edges of the longest path from the root to a leaf is
called the height of the tree.
• The number of edges of the path from the root to a node is called the
depth of the node.
Height of tree = 3 Depth of node = 1
Depth of node = 2
6
Binary Tree data structure
The size of a tree is equivalent to the number of nodes.
Size = 9 1
2 3
4 5 6 7
8 9
7
Binary Tree data structure
Full tree– A tree in which every node, except the leaves, has two children.
Complete tree– A tree in which every level, except possibly the last, is
completely filled. Nodes are as far left as possible.
8
Binary Search Tree
• Binary search tree is a binary tree with
relative ordering in how the nodes are 6
organized
• Each node stores a distinct key. 4 8
• All the nodes to the left of a node have keys 2 5 7 9
less than the key of the node
• All the nodes to the right of a node have keys 1 3
greater than the key of the node.
9
Uses of Binary Search Tree
Store keys of HashTable/Dictionary:
• Allows for efficient search (O(log(n)) in separate chaining
Divide & Conquer approach to programming
10
Binary Tree traversals
• The process of checking/visiting each node once.
• Applications include:
• Printing all values in a binary tree
• Determining if there is one or more nodes with some property
• Making a copy
• The order of traversal depends on the algorithm used.
• Three such algorithms are:
• Pre-order
• In-order
• Post-order
https://www.educative.io/collection/page/10370001/160001/220001
11
Pre-order traversal
Starting at the root, 1
2 7
We traverse in the following order:
3 6 8 9
1. First visit the node itself.
2. Then visit the left subtree of the node. 4 5
3. Then visit the right subtree of the node.
12
In-order traversal
Starting at the root, 6
4 8
We traverse in the following order:
2 5 7 9
1. First visit the left subtree of the node.
2. Then visit the node itself. 1 3
3. Then visit the right subtree of the node.
13
Post-order traversal
Starting at the root, 9
5 8
We traverse in the following order:
3 4 6 7
1.First visit the left subtree of the node.
2.Then visit the right subtree of the node. 1 2
3.Then visit the node itself.
14
Binary Search Tree ADT
entry
• Accessors:
left right def entry(tree):
return tree[0]
def left_branch(tree):
• Constructors: return tree[1]
def make_tree(entry, left, right):
return [entry, left, right] def right_branch(tree):
return tree[2]
Binary Search Tree ADT
• Predicates: • Modifiers:
def is_empty_tree(tree): def insert(x, tree):
return tree==[] (left as an exercise in
tutorial)
def contains(x, tree):
(left as an exercise in def remove(x, tree):
tutorial) (left as an extra
practice)
• Time complexity: O(log n)
Balanced trees 1
7 5
• Operation is O(log n) assuming that
tree is balanced.
7
3 9
• Unbalanced trees break the log n
complexity.
9
1 5 11
11
Balanced trees
• For a complete tree with height h,
where all levels are filled up, the
total number of nodes,
n = 1 + 2 + 4 + …. + 2h-1 + 2h
= (2h+1 -1 ) [sum of a GP]
• Since the time needed for
search/insertion depends directly on
h, making h the subject, we have:
• h = log2(n+1)-1
• Using big-O,
• Time complexity: O(log n)
Binary Search Tree vs Binary Search
7
• Searching using a binary search tree
works the same way as the binary
search in a linear sorted array. 3 9
• The algorithm for binary search also 1 5 11
has time complexity of O(log n) since
the search scope is always reduced by
half for every iteration, making it one of
the most time efficient algorithm. {1,3,5,7,9,11}
Insertion entry to binary search tree
Three trees representing the set of numbers 1,3,5,7,9,11
7 3 5
1 7
3 9 3 9
5 9
1 7 11
1 5 11
11
Binary Search Tree ADT
• Predicates:
def is_element_of_set(x, s):
if is_empty_set(s):
return False
elif x == entry(s):
return True
elif x < entry(s):
return is_element_of_set(x, left_branch(s))
else:
return is_element_of_set(x, right_branch(s))
• Time complexity: O(log n)
Balancing trees
• Operation is O(log n) 1
assuming that tree is
balanced.
3
• But they can become
unbalanced after several
operations. 5
– Unbalanced trees break the log n
complexity. 7
• One solution: define a
function to restore balance. 9
Call it ever so often.
11
Question of the Day
• How do we convert an unbalanced binary
tree into a balanced tree?
• If time allows, do your own research and
write a function balance_tree that will
take a binary tree and return a balanced
tree (or as balanced as you can make it)