1. Unit- 4 (Part -1)
TREES
CA3CRT09- Data Structures Using C++
2. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Syllabus
Data Structures using C++
Unit 1 (12 hrs. )
Concept of Structured data - Data structure definition, Different types and classification of data structures,
Arrays – Memory allocation and implementation of arrays in memory, array operations, Applications -
sparse matrix representation and operations, polynomials representation and addition, Concept of search
and sort – linear search, binary search, selection sort, insertion sort, quick sort.
Unit 2 (12 hrs.)
Stacks – Concepts, organization and operations on stacks using arrays (static), examples, Applications -
Conversion of infix to postfix and infix to prefix, postfix evaluation, subprogram calls and execution,
Multiple stacks representation.
Queues - Concepts, organization and operations on queues, examples.
Circular queue – limitations of linear queue, organization and operations on circular queue. Double ended
queue, Priority queue.
Unit 3 (18 hrs.)
Linked list: Concept of dynamic data structures, linked list, types of linked list, linked list using pointers,
insertion and deletion examples, circular linked list, doubly linked lists
Applications- linked stacks and queues, memory management basic concepts, garbage collection.
Unit 4 (15)
Trees - Concept of recursion, trees, tree terminology, binary trees, representation of binary trees, strictly
binary trees, complete binary tree, extended binary trees, creation and operations on binary tree, binary
search trees, Creation of binary search tree, tree traversing methods – examples, binary tree representation
of expressions.
Unit 5 (15)
File - Definition, Operations on file (sequential), File organizations - sequential, Indexed sequential,
random files, linked organization, inverted files, cellular partitioning, hashing – hash tables, hashing
functions, collisions, collision resolving methods.
38
3. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
1. Trees - Concept of recursion,
2. trees, tree terminology,
3. binary trees, representation of binary trees,
1. Strictly binary trees,
2. complete binary tree,
3. extended binary trees,
4. creation and operations on binary tree,
5. Binary search trees, Creation of binary search tree,
6. tree traversing methods – examples,
7. binary tree representation of expressions.
TREES
4. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
MES College Marampally
Tree
⚫ Tree is a non-linear data structure in which items are
arranged in a sorted sequence. It is used to represent
hierarchical relationship existing among several data items.
⚫ In tree data structure, every individual element is called
as Node. Node in a tree data structure, stores the actual data
of that particular element and link to next element in
hierarchical structure.
In a tree data structure, if we have N nS
uy
m
ste
b
m
e
Ad
rmi
o
nis
f
tra
n
ti
o
on
des then we
can have a maximum of N-1 number of links.
5. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
6. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Tree Terminology
1. Root
In a tree data structure, the first node is called as Root Node.
Every tree must have root node. We can say that root node is
the origin of tree data structure. In any tree, there must be
only one root node. We never have multiple root nodes in a
tree.
7. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
2. Edge
In a tree data structure, the connecting link between any two
nodes is called as EDGE. In a tree with 'N' number of nodes
there will be a maximum of 'N-1' number of edges.
8. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
3. Parent
In a tree data structure, the node which is predecessor of any
node is called as PARENT NODE. In simple words, the node
which has branch from it to any other node is called as parent
node. Parent node can also be defined as "The node which has
child / children".
9. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
4. Child
In a tree data structure, the node which is descendant of any
node is called as CHILD Node. In simple words, the node which
has a link from its parent node is called as child node. In a tree,
any parent node can have any number of child nodes. In a tree,
all the nodes except root are child nodes.
10. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
5. Siblings
In a tree data structure, nodes which belong to same Parent are
called as SIBLINGS. In simple words, the nodes with same
parent are called as Sibling nodes.
11. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
6. Leaf
In a tree data structure, the node which does not have a child
is called as LEAF Node. In simple words, a leaf is a node with no
child.
In a tree data structure, the leaf nodes are also called
as External Nodes. External node is also a node with no child.
In a tree, leaf node is also called as 'Terminal' node.
12. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
7. Internal Nodes
In a tree data structure, the node which has atleast one child is
called as INTERNAL Node. In simple words, an internal node is a
node with atleast one child.
In a tree data structure, nodes other than leaf nodes are called
as Internal Nodes. The root node is also said to be Internal
Node if the tree has more than one node. Internal nodes are also
called as 'Non-Terminal' nodes.
13. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
8. Degree
In a tree data structure, the total number of children of a node
is called as DEGREE of that Node. In simple words, the Degree
of a node is total number of children it has. The highest degree
of a node among all the nodes in a tree is called as 'Degree of
Tree'
14. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
9. Level
In a tree data structure, the root node is said to be at Level 0
and the children of root node are at Level 1 and the children of
the nodes which are at Level 1 will be at Level 2 and so on... In
simple words, in a tree each step from top to bottom is called as
a Level and the Level count starts with '0' and incremented by
one at each level (Step).
15. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
10. Height
In a tree data structure, the total number of egdes from leaf
node to a particular node in the longest path is called
as HEIGHT of that Node. In a tree, height of the root node is
said to be height of the tree. In a tree, height of all leaf
nodes is '0'.
16. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
11. Depth
In a tree data structure, the total number of egdes from root
node to a particular node is called as DEPTH of that Node. In a
tree, the total number of edges from root node to a leaf node
in the longest path is said to be Depth of the tree. In simple
words, the highest depth of any leaf node in a tree is said to be
depth of that tree. In a tree, depth of the root node is '0'.
17. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
12. Path
In a tree data structure, the sequence of Nodes and Edges
from one node to another node is called as PATH between that
two Nodes. Length of a Path is total number of nodes in that
path. In below example the path A - B - E - J has length 4.
18. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
13. Sub Tree
In a tree data structure, each child from a node forms a
subtree recursively. Every child node will form a subtree on its
parent node.
19. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Binary Tree
⚫ In a normal tree, every node can have any number of children.
Binary tree is a special type of tree data structure in which
every node can have a maximum of 2 children. One is known as
left child and the other is known as right child.
⚫ In a binary tree, every node can have either 0 children or 1
child or 2 children but not more than 2 children.
20. Binary Tree Representations
A binary tree data structure is represented using two methods.
Those methods are as follows...
⚫ Array Representation
⚫ Linked List Representation
Consider the following binary tree..
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
21. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
1. Binary tree Array Representation
⚫ In array representation of binary tree, we use a one dimensional
array (1-D Array) to represent a binary tree.
Consider the above example of binary tree and it is represented
as follows...
22. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
⚫ The root node is always at index 0.
⚫ Then in successive memory locations the left child and right
child are stored.
23. 2. Binary tree Linked List Representation
⚫ We use doubly linked list to represent a binary tree.
⚫ In a doubly linked list, every node consists of three fields.
⚫ First field for storing left child address,
⚫ second for storing actual data and
⚫ third for storing right child address.
In this linked list representation, a node has the following structure...
ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
24. ASH
Assis
Depa
Syst
MES
IKA K A
tant Professor & Head
rtment of Software Development and
em Administration
College Marampally
struct node
{
int data;
node *lchild;
node *rchild;
}
25. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
⚫ binary tree represented using Linked list representation is
shown as follows...
26. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Operations on binary trees
1. Creation
2. Insertion of nodes
3. Deletion of nodes
4. Tree traversal
5. Searching for the node
6. Copying the binary tree
27. Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Binary Tree Traversals
⚫ When we wanted to display a binary tree, we need to follow
some order in which all the nodes of that binary tree must be
displayed. In any binary tree displaying order of nodes depends
on the traversal method.
⚫ Displaying (or) visiting order of nodes in a binary tree is
called as Binary Tree Traversal.
⚫ There are three types of binary tree traA
S
vH
eI
K
rA
sK
aA
ls.
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
28. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
⚫ Inorder Traversal ( leftChild - root - rightChild )
⚫ Preorder Traversal ( root - leftChild - rightChild )
⚫ Postorder Traversal ( leftChild - rightChild - root )
29. ASHIKA K A
Department of Software Development and
Inorder traversal
The inorder traversal of non-empty binary tree is defined as
follows:
1. Traverse the left subtree in inorder
2. Visit the root node
3. Traverse the right subtree in inorder.
Ie. In inorder traversal left subtree is tra
Av
sse
ist
r
an
s
te
Pr
d
ofe
r
ss
e
or
c&
uH
r
es
ad
ively in
inorder before visiting the root node.aS
f
yt
ste
em
rAv
dm
is
ini
ist
tr
ia
n
tig
on the root
node , the right subtree is taken up and
M
E
S
it
C
o
i
l
l
s
e
g
e
t
M
r
a
a
r
a
v
m
e
p
a
r
l
l
y
sed
recursively again in inorder.
30. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
Inorder traversal
Void inorder(struct node *tree)
{
If (tree!=NULL)
{
Inorder (tree->left);
Cout << tree->num;
Inorder (tree->right);
}
}
31. ASHIKA K A
Department of Software Development and
System Administration
MES College Marampally
preorder traversal
The preorder traversal of non-empty binary tree is defined as
follows:
1. Visit the root node
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder.
Ie. In preorder traversal root node is visitA
s
es
i
ds
t
a
bn
teP
r
fo
f
oe
s
rs
o
er&
vH
ie
sa
id
ting left
and right subtrees.
32. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
preorder traversal
Void preorder(struct node *tree)
{
If (tree!=NULL)
{
Cout << tree->num;
preorder (tree->left);
preorder (tree->right);
}
}
33. ASHIKA K A
Department of Software Development and
System Administration
MES College Marampally
postorder traversal
The postorder traversal of non-empty binary tree is defined as
follows:
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder.
3. Visit the root node
Ie. In postorder traversal left and right sA
us
s
bi
s
tt
a
rn
t
eP
er
o
sf
e
s
as
o
rr
e
&Hreaedcursively
processed before visiting the root.
34. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
postrder traversal
Void postorder(struct node *tree)
{
If (tree!=NULL)
{
postorder (tree->left);
postorder (tree->right);
Cout << tree->num;
}
}
35. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
12 9
6
5
Inorder traversal : 5->12->6->1->9
Preorder traversal : 1->12->5->6->9
Postorder traversal : 5->6->12->9->1
36. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
12 9
6
5
Inorder traversal : 5->12->6->1->9
Preorder traversal : 1->12->5->6->9
Postorder traversal : 5->6->12->9->1
37. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
12 9
6
5
Inorder traversal : 5->12->6->1->9
Preorder traversal : 1->12->5->6->9
Postorder traversal : 5->6->12->9->1
38. #include <iostream>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node(int data) {
this->data = data;
left = right = NULL;
}
};
// Preorder traversal
void preorderTraversal(struct Node* node) {
if (node == NULL)
return;
cout << node->data << "->";
preorderTraversal(node->left);
preorderTraversal(node->right);
}
// Postorder traversal
void postorderTraversal(struct Node* no
de) {
}
) {
if (node == NULL)
return;
postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->data << "->";
// Inorder traversal
void inorderTraversal(struct Node* node
ASHIKA K A
if (node A
=s
=
sisN
ta
U
n
L
tL
P)
rofessor & Head
Department of Software Development and
return;System Administration
MES College Marampally
inorderTraversal(node->left);
cout << node->data << "->";
inorderTraversal(node->right);
}
39. ASHIKA K A
Assistant Professor & Head
Department of Software Development and
System Administration
MES College Marampally
int main() {
struct Node* root = new Node(1)
;
root->left = new Node(12);
root->right = new Node(9);
root->left->left = new Node(5);
root->left->right = new Node(6)
;
cout << "Inorder traversal ";
inorderTraversal(root);
cout << "nPreorder traversal
"; preorderTraversal(root);
cout << "nPostorder traversal ";
postorderTraversal(root);
}
Output:
Inorder traversal : 5->12->6->1->9
Preorder traversal : 1->12->5->6->9
Postorder traversal : 5->6->12->9->1