LT11b-+Binary+Tree+(2023)
LT11b-+Binary+Tree+(2023)
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
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
11
Pre-order traversal
12
In-order traversal
13
Post-order traversal
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)
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
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))