0% found this document useful (0 votes)
4 views39 pages

CSPC24 Chapter 2 - Data Structures

The document provides an overview of data structures, defining them as formats for organizing, managing, and storing data for efficient access and modification. It discusses various types of data structures, including graphs and trees, and explains key concepts such as binary trees and traversal methods (preorder, inorder, and postorder). Additionally, it highlights the characteristics and classifications of data structures, emphasizing their importance in computer science.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views39 pages

CSPC24 Chapter 2 - Data Structures

The document provides an overview of data structures, defining them as formats for organizing, managing, and storing data for efficient access and modification. It discusses various types of data structures, including graphs and trees, and explains key concepts such as binary trees and traversal methods (preorder, inorder, and postorder). Additionally, it highlights the characteristics and classifications of data structures, emphasizing their importance in computer science.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

CSPC 24

Algorithm and
Complexity
Data Structures
Objectives
• Define and explain Data Structure.

• Differentiate the different types of Data Structure.

• Identify the classification and application of Data


Structure.

• Appreciate the application and importance of Data


Structure.
DATA STRUCTURE
• a data organization, management, and storage format
that enables efficient access and modification.

• a collection of data values, the relationships among


them, and the functions or operations that can be
applied to the data.
CHARACTERISTICS OF DATA
STRUCTURES
• Linear or non-linear - the data items are arranged in
chronological sequence, such as with an array, or in an
unordered sequence, such as with a graph.
• Homogeneous or non-homogeneous - all data items in
a given repository are of the same type or of various types.
• Static or dynamic - describes how the data structures are
compiled.
• Static data structures have fixed sizes, structures and memory
locations at compile time.
• Dynamic data structures have sizes, structures and memory
locations that can shrink or expand depending on the use.
TYPES OF DATA STRUCTURE
Data Structure - Graph Data Structure
• A graph is a pictorial representation of a set of objects
where some pairs of objects are connected by links.
• The interconnected objects are represented by points
termed as vertices, and the links that connect the
vertices are called edges.
Data Structure - Trees Data Structure
• Tree represents the nodes connected by edges.
• A structure that may have multiple relations among its
nodes.
Data Structure - Trees Data Structure
• Binary Tree is a special data structure used for data
storage purposes.

• A binary tree has a special condition that each node can


have a maximum of two children.
Following are the important terms with
respect to tree.
• Path − Path refers to the sequence of nodes along the
edges of a tree.
• Root − The node at the top of the tree is called root. There
is only one root per tree and one path from the root node to
any node.
• Parent − Any node except the root node has one edge
upward to a node called parent.
• Child − The node below a given node connected by its edge
downward is called its child node.
• Leaf − The node which does not have any child node is
called the leaf node.
Following are the important terms with
respect to tree.
• Subtree − Subtree represents the descendants of a node.
• Visiting − Visiting refers to checking the value of a node
when control is on the node.
• Traversing − Traversing means passing through nodes in
a specific order.
• Levels − Level of a node represents the generation of a
node. If the root node is at level 0, then its next child node
is at level 1, its grandchild is at level 2, and so on.
• keys − Key represents a value of a node based on which a
search operation is to be carried out for a node.
Binary Search Tree Representation
The basic operations that can be performed on a binary
search tree data structure, are the following −
• Insert − Inserts an element in a tree/create a tree.
• Search − Searches an element in a tree.
• Preorder Traversal − Traverses a tree in a pre-order
manner.
• Inorder Traversal − Traverses a tree in an in-order
manner.
• Postorder Traversal − Traverses a tree in a post-order
manner.
Preorder Traversal
Preorder traversal is defined as a type of tree traversal
that follows the Root-Left-Right policy where:

• The root node of the subtree is visited first.

• Then the left subtree is traversed.

• At last, the right subtree is traversed.


Algorithm for Preorder Traversal of
Binary Tree
The algorithm for preorder traversal is shown as follows:

Preorder(root):
1. Follow step 2 to 4 until root != NULL
2. Write root -> data
3. Preorder (root -> left)
4. Preorder (root -> right)
5. End loop
How does Preorder Traversal of Binary
Tree work?
Consider the following tree:

2 3

4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 1: At first the root will be visited, i.e. node 1.

2 3

4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 2: After this, traverse in the left subtree. Now the root of the
left subtree is visited i.e., node 2 is visited.

2 3

4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 3: Again the left subtree of node 2 is traversed and the root
of that subtree i.e., node 4 is visited.

2 3

4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 4: There is no subtree of 4 and the left subtree of node 2 is
visited. So now the right subtree of node 2 will be traversed and
the root of that subtree i.e., node 5 will be visited.
1

2 3

4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 5: The left subtree of node 1 is visited. So now the right
subtree of node 1 will be traversed and the root node i.e., node 3 is
visited.
1

2 3

4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 6: Node 3 has no left subtree. So the right subtree will be traversed and
the root of the subtree i.e., node 6 will be visited. After that there is no node that
is not yet traversed. So the traversal ends.

2 3

4 5 6

So the order of traversal of nodes is 1 -> 2 -> 4 -> 5 -


> 3 -> 6.
Inorder Traversal
Inorder traversal is defined as a type of tree traversal
technique which follows the Left-Root-Right pattern, such
that:

• The left subtree is traversed first

• Then the root node for that subtree is traversed

• Finally, the right subtree is traversed


Algorithm for Inorder Traversal of
Binary Tree
The algorithm for inorder traversal is shown as follows:

Inorder(root):
1. Follow step 2 to 4 until root != NULL
2. Inorder (root -> left)
3. Write root -> data
4. Inorder (root -> right)
5. End loop
How does Inorder Traversal of Binary
Tree work?
Consider the following tree:

2 3

4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from 2 to its
left subtree root, i.e., 4. Now 4 has no left subtree, so it will be visited. It also
does not have any right subtree. So no more traversal from 4

2 3

4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 2: As the left subtree of 2 is visited completely, now it read data of node 2
before moving to its right subtree.

2 3

4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 3: Now the right subtree of 2 will be traversed i.e., move to node 5. For
node 5 there is no left subtree, so it gets visited and after that, the traversal
comes back because there is no right subtree of node 5.

2 3

4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 4: As the left subtree of node 1 is, the root itself, i.e., node 1 will be
visited.

2 3

4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 5: Left subtree of node 1 and the node itself is visited. So now the right
subtree of 1 will be traversed i.e., move to node 3. As node 3 has no left subtree
so it gets visited.

2 3

4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 6: The left subtree of node 3 and the node itself is visited. So traverse to
the right subtree and visit node 6. Now the traversal ends as all the nodes are
traversed.

2 3

4 5 6

So the order of traversal of nodes is 4 -> 2 -> 5 -> 1 -> 3 -


> 6.
Postorder Traversal
Inorder traversal is defined as a type of tree traversal
technique which follows the Left-Right-Root pattern, such
that:

• The left subtree is traversed first

• Then the right subtree is traversed

• Finally, the root node of the subtree is traversed


Algorithm for Postorder Traversal of
Binary Tree
The algorithm for postorder traversal is shown as follows:

Postorder(root):
1. Follow step 2 to 4 until root != NULL
2. Postorder (root -> left)
3. Postorder (root -> right)
4. Write root -> data
5. End loop
How does Postorder Traversal of Binary
Tree work?
Consider the following tree:

2 3

4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from 2 to its
left subtree root, i.e., 4. Now 4 has no subtree, so it will be visited.

2 3

4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 2: As the left subtree of 2 is visited completely, now it will traverse the
right subtree of 2 i.e., it will move to 5. As there is no subtree of 5, it will be
visited.

2 3

4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 3: Now both the left and right subtrees of node 2 are visited. So now visit
node 2 itself.

2 3

4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 4: As the left subtree of node 1 is traversed, it will now move to the right
subtree root, i.e., 3. Node 3 does not have any left subtree, so it will traverse the
right subtree i.e., 6. Node 6 has no subtree and so it is visited.

2 3

4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 5: All the subtrees of node 3 are traversed. So now node 3 is visited.

2 3

4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 6: As all the subtrees of node 1 are traversed, now it is time for node 1 to
be visited and the traversal ends after that as the whole tree is traversed.

2 3

4 5 6

So the order of traversal of nodes is 4 -> 5 -> 2 -> 6 -> 3 -> 1.


Thank you!
REFERENCES:
Cormen T., Leiserson C., Rivest R., & Stein C. (2009). Introduction to Algorithms Third
Edition. The MIT Press, Cambridge, Massachusets
Fleck, Margaret M., (2013). Building Blocks for Theoretical Computer Science. Version
1.3 (January 2013)
Lehman, Eric F., Leighton, Thomson & Meyer, Albert R. (2018). Mathematics for
Computer Science. June 2018
ONLINE REFERENCES:
https://www.geeksforgeeks.org/
http://www.freebookcentre.net/CompuScience/free-computer-algorithm-books.html

You might also like