CSC-114 Data Structure and Algorithms: Slides Credit: Ms. Saba Anwar
CSC-114 Data Structure and Algorithms: Slides Credit: Ms. Saba Anwar
Tree
2
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Non-Linear Data Structure www.foxitsoftware.com/shopping
3
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Graph www.foxitsoftware.com/shopping
4
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree www.foxitsoftware.com/shopping
d e h e
In field of computer science, a specific form of trees is more common, which is called
rooted trees. a
a is root vertex.
These rooted trees are directed graphs b c d
e f g h
5
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree www.foxitsoftware.com/shopping
C:/
Program
Windows Users
Files
6
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree: a Data Structure www.foxitsoftware.com/shopping
Tree is a recursive data structure, it contains patterns that are themselves are
trees.
A data structure is recursive if it is composed of smaller pieces of it’s own data type.
Such as list and trees.
a is root of all nodes like b, d etc.
b, c, d are also root of their sub trees and so on. a
7
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Applications www.foxitsoftware.com/shopping
Search trees
More efficient than sorted list
8
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Applications www.foxitsoftware.com/shopping
Parse Trees
Used by compilers to produce machine code
Decision Trees
Used in artificial intelligence to build knowledge base
9
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Applications www.foxitsoftware.com/shopping
Games
are used in logic games
Data Compression
Huffman coding trees
Priority Queue
Heap Tree
10
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Terminologies www.foxitsoftware.com/shopping
Node/Vertex
Root a
One data unit of tree
Edge
Arc/link from one node to other Internal b c d
Root node
The top node of tree. A node with no parent e f g h
Leaf/External node
Edge
Node with no child
Internal Node: Leaf i j k l
Node with child
Ancestors of Node
Parent, all grand parents and all great grand parents of node.
a, b and e are ancestors of i.
11
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Terminologies www.foxitsoftware.com/shopping
Descendants of Node
a
Child , all grand children and great grand children of node.
i, j, e and f are descendants of b.
Sub Tree b c d
A node within tree with descendants
Degree of Node: e f g h
Number of its children
a’s degree is 3
b, h and e’s degree is 2 i j k l
c’s degree is 1
Degree of Tree
Maximum degree of any node Sub-tree
Since a has degree 3 that is maximum so degree of tree is 3
12
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Terminologies www.foxitsoftware.com/shopping
14
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree www.foxitsoftware.com/shopping
Binary Tree is a special tree where each node can have maximum two children.
In other words maximum degree of any node is 2.
Each node has a left child and a right child. Even if a node has only one child, other
child is still mentioned with NULL.
a General Tree
Binary Tree a
b d b d
h
e f h g i g j
15
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree www.foxitsoftware.com/shopping
Recursive Definition:
T is a binary tree if
T is empty (NULL) OR
T’s root node has maximum two children's, where each child is itself a binary tree.
Left child is called left subtree and right child is called right subtree
General Tree
Binary Tree Binary Tree
16
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Full Binary Tree www.foxitsoftware.com/shopping
A Full/Proper binary tree in which each leaf node has same depth/level.
Perfect & Full Not-Perfect But Full Not Perfect Nor Full
18
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Complete Binary Tree www.foxitsoftware.com/shopping
A tree that is completely filled at all levels, except the last level which is filled
from left to right
19
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree www.foxitsoftware.com/shopping
20
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree ADT www.foxitsoftware.com/shopping
21
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree Implementation www.foxitsoftware.com/shopping
Linked representation
Each node has two links left and right
If root node is null, means tree is empty
If node’s left, right links are NULL, it means its leaf node
Optionally, a parent field with a reference to the parent node
left data right
22
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree Implementation www.foxitsoftware.com/shopping
Array representation
A fixed size tree can be represented using 1-D array.
If we know the height of tree, we can define size of array to hold maximum possible
number of nodes 2h+1-1 a 0
23
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Traversal www.foxitsoftware.com/shopping
d e
24
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Breadth First Search (BFS) www.foxitsoftware.com/shopping
Starting from root node, visit all of its children, all of its grand children and all
of its great grand children
Order of nodes: a b g c f h d e
a
Nodes at same level must be visited first before nodes of next level
Also known as level order traversal b g
Implementation?
We should store nodes to keep track of them. c f h
The sequence in which we store them effects the
the sequence in which we retrieve them back d e
Which data structure can be used to store nodes?
array, stack or queue
25
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Breadth First Search (BFS) www.foxitsoftware.com/shopping
a a
b c b d
d e f e f h
g h i i j k l
abcdefghij abdefhijkl
26
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Breadth First Search (BFS) www.foxitsoftware.com/shopping
28
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Depth First Search (DFS) www.foxitsoftware.com/shopping
Using the top-down view of the tree, starting from root, go to each sub tree as
far as possible, then back track
Possible Orders:
a
Left sub tree and then right sub tree
abcdefgh
right sub tree and then left sub tree b g
aghbfced
Implementation: c f h
Can we use a stack instead of queue
d e
29
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Depth First Search (DFS) www.foxitsoftware.com/shopping
a a
b c b d
d e f e f h
g h i i j k l
abdeghjcfi abeijfdhkl
30
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Depth First Search (DFS) www.foxitsoftware.com/shopping
Depth First Search can also be implemented with recursive approach. And depending upon
the order in which we go in depth can bring different variations in order of node traversal.
Which are:
Pre-Order (simple DFS)
a
1. Visit node
2. Visit left child of node
3. Visit right child of node b g
Post-Order
1. Visit left child of node a
2. Visit right child of node
3. Visit node
b g
In-Order
1. Visit left child of node a
2. Visit node
3. Visit right child of node
b g
32
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Pre-Order vs. Post-Order vs. In-Order www.foxitsoftware.com/shopping
Pre-order (node-left-right)
abcdefgh
Post-order (left-right-node) a
decfbhga
In-order (left-node-right) b g
dcebfagh
c f h
d e
33
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Pre-Order vs. Post-Order vs. In-Order www.foxitsoftware.com/shopping
a a
b d
b c
d e f e f h
g h i i j k l
j
Pre-Order: a b d e g h j c f i Pre-Order: a b e i j f d h k l
Post-Order: d g j h e b i f c a Post-Order: i j e f b k l h d a
In-Order: dbgejhacif In-Order: i e j b f a k h l d
34
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Traversal-Recursive Algorithms www.foxitsoftware.com/shopping
35