Data Structures and
Algorithms
Binary Trees
1 Data Structure & Algorithms (Fall24) 03/26/25
2 Data Structure & Algorithms (Fall24) 03/26/25
3 Data Structure & Algorithms (Fall24) 03/26/25
Binary-Tree Related
Definitions
The children of any node in a binary tree
are ordered into a left child and a right
child 1
A node can have a left and 3 7
a right child, a left child
only, a right child only, 5 8 9
or no children 10
4 6
The tree made up of a left
child (of a node x) and all its 11 12
descendents is called the left subtree of x
Right subtrees are defined similarly
4 Data Structure & Algorithms (Fall24) 03/26/25
Parts of a binary tree
A binary tree is composed of zero or more nodes
Each node contains:
A value (some sort of data item)
A reference or pointer to a left child (may be null),
and
A reference or pointer to a right child (may be null)
A binary tree may be empty (contain no nodes)
If not empty, a binary tree has a root node
Every node in the binary tree is reachable from the
root node by a unique path
A node with no left child and no right child is called a
leaf
In some binary trees, only the leaves contain a value
5 Data Structure & Algorithms (Fall24) 03/26/25
6 Data Structure & Algorithms (Fall24) 03/26/25
7 Data Structure & Algorithms (Fall24) 03/26/25
8 Data Structure & Algorithms (Fall24) 03/26/25
9 Data Structure & Algorithms (Fall24) 03/26/25
Left ≠ Right
The following two binary trees are different:
A A
B B
In the first binary tree, node A has a left child but no
right child; in the second, node A has a right child but
no left child
Put another way: Left and right are not relative terms
10 Data Structure & Algorithms (Fall24) 03/26/25
Size and depth
The size of a binary tree is
a the number of nodes in it
This tree has size 12
b c The depth of a node is its
distance from the root
d e f a is at depth zero
e is at depth 2
g h i j k The depth of a binary tree
is the depth of its deepest
l node
This tree has depth 4
11 Data Structure & Algorithms (Fall24) 03/26/25
Balance
a a
b c b
d e f g c e
d f
h i j
A balanced binary tree g h
i j
An unbalanced binary tree
A binary tree is balanced if every level above the lowest is “full”
(contains 2n nodes)
In most applications, a reasonably balanced binary tree is
desirable
12 Data Structure & Algorithms (Fall24) 03/26/25
Sorted binary trees
A binary tree is sorted if every node in the
tree is larger than (or equal to) its left
descendants, and smaller than (or equal to)
its right descendants
Equal nodes can go either on the left or the
right (but it has to be consistent)
10
8 15
4 12 20
13 Data Structure & Algorithms (Fall24) 17 03/26/25
14 Data Structure & Algorithms (Fall24) 03/26/25
15 Data Structure & Algorithms (Fall24) 03/26/25
16 Data Structure & Algorithms (Fall24) 03/26/25
Preoder, Inorder, Postorder
In Preorder, the root Preorder Traversal:
1. Visit the root
is visited before (pre) 2. Traverse left subtree
the subtrees traversals 3. Traverse right subtree
In Inorder, the root is Inorder Traversal:
visited in-between left 1. Traverse left subtree
2. Visit the root
and right subtree traversal
3. Traverse right subtree
In Preorder, the root
Postorder Traversal:
is visited after (pre) 1. Traverse left subtree
the subtrees traversals 2. Traverse right subtree
3. Visit the root
17 Data Structure & Algorithms (Fall24) 03/26/25
Illustrations for Traversals
Assume: visiting a node 1
is printing its label 3 7
Preorder:
1 3 5 4 6 7 8 9 10 11 12 5 8 9
Inorder:
10
4 5 6 3 1 8 7 9 11 10 12 4 6
Postorder:
11 12
4 6 5 3 8 11 12 10 9 7 1
18 Data Structure & Algorithms (Fall24) 03/26/25
Illustrations for Traversals
(Contd.)
Assume: visiting a node 15
is printing its data 8 20
Preorder: 15 8 2 6 3 7
2 11 27
11 10 12 14 20 27 22 30
Inorder: 2 3 6 7 8 10 11
6 10 12 22 30
12 14 15 20 22 27 30
Postorder: 3 7 6 2 10 14 3 7 14
12 11 8 22 30 27 20 15
19 Data Structure & Algorithms (Fall24) 03/26/25
Tree traversals using “flags”
The order in which the nodes are visited during a
tree traversal can be easily determined by imagining
there is a “flag” attached to each node, as follows:
preorder inorder postorde
r
To traverse the tree, collect the flags:
A A A
B C B C B C
D E F G D E F G D E F G
20 DataAStructure
BDE C F G (Fall24)
& Algorithms DBEAFCG D E B F G C 03/26/25
A
Other traversals
The other traversals are the reverse of these
three standard ones
That is, the right subtree is traversed before the
left subtree is traversed
Reverse preorder: root, right subtree, left
subtree
Reverse inorder: right subtree, root, left
subtree
Reverse postorder: right subtree, left subtree,
root
21 Data Structure & Algorithms (Fall24) 03/26/25
The End
22 Data Structure & Algorithms (Fall24) 03/26/25