Binary Trees in Data Structure
Binary Trees in Data Structure
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.
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.
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:
B C
D E F G
Depth: 3 I J
2. Types of Binary Trees
8
A A
B C B C
D E F G D E F G
H I J I J
B C
D E F G
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’
( 4 + 2 ) * 3 = 18
15 A Binary Expression Tree
‘*’
‘+’ ‘3’
‘4’ ‘2’
‘*’
‘+’ ‘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
20 Binary expression tree
‘*’
‘-’ ‘/’
‘4’ ‘2’
‘*’
‘-’ ‘/’
‘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
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
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
• Preorder Traversal
• Inorder Traversal
• Postorder Traversal
33 Traversal of a Binary Tree
Preodrder:
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
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:
32
79 42
13 95 16
79 13 32 95 42 16
45 Tree Traversal
Postorder:
79 42
13 95 16
13 79 95 16 42 32
47 Tree Traversal
Preorder:
Postorder:
Inorder:
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
Preorder:
Postorder:
Inorder:
left – root – right