CSCE 210
Data Structures and Algorithms
Trees
1
Trees
Binary Trees
Tree Traversal
2
1. Binary Trees
A Tree is a non-linear, hierarchical one-to-
many data structure.
A special tree is the
Binary Tree
a
b c
d e f g
3
Binary Trees
Can be implemented using arrays, structs
and pointers
Used in problems dealing with:
Searching
Hierarchy
Ancestor/descendant relationship
Classification
4
Tree Terminology
A tree consists of a finite set of nodes (or vertices)
and a finite set of edges that connect pairs of nodes.
A node is a container for data
An edge represents a relationship between two
nodes.
a
a node a an edge
b
5
Tree Terminology
A non-empty tree has a root node (e.g.node a). Every other
node can be reached from the root via a unique path
(e.g. a-d-f). a is the parent of nodes b and c because there is
an edge going from a down to each. Moreover, b and c are
children of a. root
a
parent of g
child of a b c d
e f g
6
Tree Terminology
a
b c Siblings
d e
The descendants of a are (b, c, d, and e), nodes that can be
reached by paths from a.
The ancestors of e are (c and a), nodes found on the path from e
to a.
Nodes (b, d, and e) are leaf nodes (they have no children).
Each of the nodes a and c has at least one child and is an
internal node.
7
Tree Terminology
a
b c
d e f g
Each node in the tree (except leaves) may have one or
more subtrees
For a tree with n nodes there are n-1 edges
8
The Binary Tree
Level 1 A binary tree of height h = 3
a
Left
Right
Level 2
b c
Level 3 d e f
A binary tree is a tree in which a parent has at most two
children. It consists of a root and two disjoint subtrees (left and
right).
The root is at level (L = 1) and the height h = maximum level
L −1
The maximum number of nodes in level L is n L=2
9
The Full Binary Tree
a
b c
d e f g
A binary tree is full iff the number of
nodes in level L is 2L-1
10
The Full Binary Tree
A full binary tree of height h has n
nodes, where
h−1
n=1+ 2+4 +. ..+ 2
h
n= ∑ 2 L−1 h
= 2 −1
L=1
Hence h= log 2 ( n+1)
11
The Balanced Tree
hL hR
A balanced binary tree has the property that the heights
of the left and right subtrees differ at most by one level.
i.e. |hL – hR| ≤ 1
A Full tree is also a balanced tree
12
Complete Binary Tree
A binary tree is Complete iff the number of Nodes at level 1 <=
L <= h-1 is 2L-1 and leaf nodes at level h occupy the leftmost
positions in the tree
i.e. all levels are filled except the rightmost of the last level.
Missing Leaves Missing Leaves
13
Complete Binary Tree
1
A B C D E A
2 3
B C
4 5
D E
A complete binary tree can be efficiently
implemented as an array, where a node at index i
has children at indexes 2i and 2i+1 and a parent
at index i/2 (with one-based indexing).
14
Binary Tree as a Recursive Structure
A binary tree is a recursive structure
e.g. it can be defined recursively as:
if (not empty tree) a
1. It has a root b c
2. It has a (Left Subtree)
3. It has a (Right Subtree) d e f
Recursive structure suggests recursive
processing of trees (e.g. Traversal)
15
Binary Tree as a Recursive Structure
Empty
a,b,c,d,e,f
b,d,e c,f
b c
d e f
16
2. Tree Traversal
Traversal is to visit every node ( to display,
process, …) exactly once
It can be done recursively
There are 3 different binary tree traversal
orders:
Pre-Order: Root is visited before its two
subtrees
In-Order: Root is visited in between its
two subtrees
Post-Order:Root is visited after its two
subtrees
17
Pre-Order Traversal
Algorithm:
PreOrder ( tree )
{ 1 a
if ( not empty tree) 5
2
{ b c
Visit (root);
4
PreOrder (left subtree); 3 d e f 6
PreOrder (right subtree);
}
}
The resulting visit order = {a} {b , d , e} {c , f }
18
Pre-Order Traversal
Pre-Order Traversal is also called Depth-First traversal
2 5
3 4 6 7
19
In-Order Traversal
Algorithm:
InOrder ( tree )
{ 4 a
if ( not empty tree) 6
2
{ b c
InOrder (left subtree);
3
Visit (root); 1 d e f 5
InOrder (right subtree);
}
}
The resulting visit order = {d , b , e} {a} {f , c }
20
Post-Order Traversal
Algorithm:
PostOrder ( tree )
{ 6 a
if ( not empty tree) 5
3
{ b c
PostOrder (left subtree);
2
PostOrder (right subtree); 1 d e f 4
Visit (root);
}
}
The resulting visit order = {d , e , b} {f , c } {a}
21
Example: Expression Tree
The expression A – B * C + D can be represented as a
tree.
In-Order traversal gives: A – B * C + D
+
This is the infix representation
Pre-Order traversal gives: + - A * B C D
- D
This is the prefix representation
Post-Order traversal gives:
A *
ABC*-D+
This is the postfix (RPN) representation
B C
22
Exercise :
Assume there is a binary tree with the following traversal
Preorder traversal sequence: F, B, A, D, C, E, G, I, H
Inorder traversal sequence: A, B, C, D, E, F, G, H, I
Can you draw the binary tree ?
23