DATA STRUCTURES AND ALGORITHMS
1
Binary Trees
2 Binary Tree - Definition
▪ Definition: a binary tree, T, is either empty or such that
▪ T has a special node called the root node
▪ T has two sets of nodes, LT and RT, called the left subtree and
right subtree of T, respectively
▪ LT and RT are binary trees
▪ Can be shown pictorially
▪ Parent, left child, right child
▪ Node represented as a circle
▪ Circle labeled by the node
3 Binary Trees
A binary tree is a tree in which each node has at most 2 children.
struct defining node of a binary tree
For each node
The data stored in info
A pointer to the left child stored in llink
A pointer to the right child stored in rlink
root
O
left M T right
subtree subtree
of node O of node O
C E • P U
•
•
left subtree right subtree
of node M of node M
4 Binary Trees
If A is the root of a binary tree and B is the root of its left or right subtree,
then A is said to be parent of B, and B is said to be the left or right child
of A.
left child of A B C right child of A
D E • F G
•
•
Left child of B right child of B
5 Binary Tree Examples
6 Binary Trees
Level:
The root of the tree has level 0, and the level of any other node in the tree
is one more than the level of its father.
A
Level 0
B C Level 1
Level 2 D E F G
I J
7 Binary Trees
Depth:
The depth of a binary tree is the maximum level of any
leaf in the tree.
B C
D E F G
Depth: 3 I J
2. Types of Binary Trees
8
Strictly (Full/Proper) Binary Trees
Complete Binary Trees
2.1 Strictly Binary Tree
9
Definition:
✔ Each node have either 0 or 2 children.
- If every non-leaf node in a binary tree has nonempty left and right
subtrees, the tree is known as Strictly Binary Tree.
✔ h
A A
B C B C
D E F G D E F G
H I J I J
Strictly Binary Tree?? ✔Strictly Binary Tree
2.2 Complete Binary Tree
(1/5)
10
A complete binary tree of depth d is a strictly binary tree all of
whose leaves are at level d.
B C
D E F G
A complete binary tree of depth 2
11 Binary Expression Tree
A special kind of binary tree in which:
Each leaf node contains a single operand
Each non leaf node contains a single binary
operator
The root contains the operator that is to be applied
to the results of evaluating the expressions in the
left and right subtrees
12 Binary Expression Trees
Infix Tree
Postfix Tree
13 A binary expression tree
treePtr
‘-’
‘8’ ‘5’
INORDER TRAVERSAL: 8 - 5
PREORDER TRAVERSAL: - 8 5
POSTORDER TRAVERSAL: 8 5 -
14 A Binary Expression Tree
‘*’
‘+’ ‘3’
‘4’ ‘2’
Evaluate the expression?
( 4 + 2 ) * 3 = 18
15 A Binary Expression Tree
‘*’
‘+’ ‘3’
‘4’ ‘2’
What infix, prefix, postfix expressions does it represent?
16 A Binary Expression Tree
‘*’
‘+’ ‘3’
‘4’ ‘2’
Infix: ((4+2)*3)
Prefix: * + 4 2 3
Postfix: 4 2 + 3 *
17 Representing Expressions
A * B + C A * (B + C) ((A + B) * C) / (D - E)
+ * /
* A * -
C +
A B + C D E
B C
A B
Δ
x Δ y
x y
18
A Binary Expression Tree
Draw an expression tree for the following:
(a+b*c)+((d*e+f)*g)
/
Traverse the tree in Left-Right-Root order (postorder)
to get RPN:
19 * -
A B + C * D E - /
+ C D E
Traverse tree in Root-Left-Right /
order (preorder) to get prefix: A B
* -
/ * + A B- CD E
+ C D E
Traverse tree in Left-Root-Right /
order (inorder) to get infix A B
— must insert ()'s
* -
(((A + B) * C)/ (D - E)) + E
C D
A B
20 Binary expression tree
‘*’
‘-’ ‘/’
‘8’ ‘5’ ‘+’ ‘4’
‘4’ ‘2’
What infix, prefix, postfix expressions does it
represent?
21 Binary Expression Tree
‘*’
‘-’ ‘/’
‘8’ ‘5’ ‘+’ ‘4’
‘4’ ‘2’
Infix: ((8-5)*((4+2)/4))
Prefix: *-85 /+424
Postfix: 85- 42+4/*
22 Applications
Expression Trees
Infix tree
Postfix tree
Huffman Tree
Binary Search Tree
Heaps
Balanced Trees
AVL Trees
23 Binary Tree Applications
Expression trees
24 Binary Tree Implementation
Static (Using Arrays)
Dynamic (Using Linked Nodes)
25 Array-Based Implementation:
An array can be used to store some binary trees.
Number the nodes level by level, from left to right,
0 Max # nodes on level i:
O 2i
1 2 In array representation,
M T children of i are at:
2i + 1, 2i + 2
3 4 5 6
C E • P U
Parent of i is at:
•
• (i - 1) / 2
Store node #0 in array location 0, node #1 in array location 1,
etc.
i 0 1 2 3 4 5 6 . ..
t [i ] O M T C E P U . ..
But, unless each level of the tree
26 is full so there are no "dangling
limbs," there can be much
wasted space in the array.
For example, this binary tree
contains the same characters as
before but requires ___ array
58
positions for storage:
Max # nodes on level i:
2i
i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
In array representation, t[i] E C M U T
children of i are at:
2i + 1, 2i + 2 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
P
Parent of i is at:
(i - 1) / 2 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 …
O …
27 Linked Nodes Implementation:
Use nodes of the form
dat
a
lef righ
t t
Left Right child
child
And maintain a pointer to the root.
28 Implementing Binary Trees
Operations typically performed on a binary tree
Determine if binary tree is empty
Search binary tree for a particular item
Insert an item in the binary tree
Delete an item from the binary tree
Find the height of the binary tree
Find the number of nodes in the binary tree
Find the number of leaves in the binary tree
Traverse the binary tree
Copy the binary tree
29 C++ Implementation:
class Node
{
public:
int data;
Node *left, *right;
};
class BinaryTree
{
public:
Node *root; // pointer to root node
BinaryTree();
Node* insert(int);
//Node* search(int); //tree type dependent
//void delete_node(int); //tree type
dependent
void traverse(); //pre, in, post order
};
30 BinaryTree::BinaryTree()
{
root=NULL;
}
Node* BinaryTree::insert(int val)
{
Node *p= new Node;
p->data=val;
p->left=NULL;
p->right=NULL;
return p; 4
}
//Driver.cpp 6 7
Void main()
{
BinaryTree b; 9
b->root = b.insert(4);
b->root->left = b.insert(6);
b->root->right = b.insert(7);
b->root->right->left = b.insert(9);
}
31
Implementing Tree
Traversals
32 Tree Traversal
Moving through a tree, visiting each node exactly once
• Preorder Traversal
• Inorder Traversal
• Postorder Traversal
33 Traversal of a Binary Tree
Used to display/access the data in a tree in a certain
order.
In traversing always right sub-tree is traversed after left
sub-tree.
Three methods of traversing
Preorder Traversing
Root – Left –Right
Inorder Traversing
Left – Root – Right
Postorder Traversing
Left – Right - Root
34 Traversal of a Binary Tree
35 Tree Traversal
Preodrder:
✔Visit the root
✔Traverse the left subtree in preorder
✔Traverse the right subtree in preorder
Also known as depth-first traversal.
36 Preorder Traversal
✔Visit the root
✔Traverse the left subtree in preorder
✔Traverse the right subtree in preorder
32
left subtree 79 42 right subtree
13 95 16
Output: 32
Problem now reduced to traversal of two smaller binary trees.
37 Preorder Traversal
79
Visit root
Traverse left subtree
Traverse right subtree
13
Output: 32 79
•Left subtree is empty
•Now, traverse the right subtree
38 Preorder Traversal
Visit root
13
Traverse left subtree
Traverse right subtree
Output: 32 79 13
39 Preorder Traversal
32
79 42
13 95 16
Output: 32 79 13
40 Preorder Traversal
42 Output: 32 79 13 42
Output: 32 79 13 42 95
Output: 32 79 13 42 95 16
95 16
95 16
41 Preorder Traversal
32
79 42
13 95 16
Output: 32 79 13 42 95 16
42 Preorder Traversal
32
79 42
13 95 16
32 79 13 42 95 16
43 Tree Traversal
Inorder:
✔Traverse the left subtree in inorder
✔Visit the root
✔Traverse the right subtree in inorder
Also known as Symmetric order.
44 Inorder Traversal
32
✔Traverse the left subtree
✔Visit the root
✔Traverse the right subtree
79 42
13 95 16
79 13 32 95 42 16
45 Tree Traversal
Postorder:
✔Traverse the left subtree in postorder
✔Traverse the right subtree in postorder
✔Visit the root
46 Postorder Traversal
✔Traverse the left subtree
✔Traverse the right subtree
32 ✔Visit the root
79 42
13 95 16
13 79 95 16 42 32
47 Tree Traversal
Preorder:
root – left – right
Postorder:
left – right – root
Inorder:
left – root – right
48 Tree Traversal
A
Preorder: A B D G C E F H
Inorder: D G B A E C H F
B C
Postorder: G D B E H F C A
D
E F
G
H
Tree Traversal
1
5 1
5 6
2
3 1
0
2 2
1 1 1
3
0 3 8
6
Pre-order: 15, 5, 3, 12, 10, 6, 7, 13, 16, 20, 18, 23
Post-order: 3, 7, 6, 10, 13, 12, 5, 18, 23, 20, 16, 15
In-order: 3, 5, 6, 7, 10, 12, 13, 15, 16, 18, 20, 23
50 Tree Traversal
Preorder:
root – left – right
Postorder:
left – right – root
Inorder:
left – root – right