Data Structures Course Overview
Data Structures Course Overview
Slides prepared by: Dr. Meenakshi Malhotra, Associate Professor, Department of CSE, Dayananda Sagar University, Harohalli, Karnataka. 2
Textbooks and Reference Books
1. A.M. Tannenbaum, Y Langsam, M J Augentien “Data Structures using C”, 1st
Edition, Pearson, 2019.
2. Ellis Horowitz, Susan Anderson-Freed, and Sartaj Sahni, “Fundamentals of Data
structures in C”, 2nd Edition, Orient Longman, 2008.
3
Module 4
INTRODUCTION TO ADT:
4
Coding competition
[Link]
[Link]
[Link]
[Link]
5
Trees
Introduction to Tree terminology
• A tree is an abstract model of a hierarchical structure that consists of nodes with a parent-child
relationship.
• for example, records, family trees and table of contents.
Introduction to Tree terminology
B C
Left subtree
Right subtree
D E F G
Edges H I Leaves
J K L M N
What is a Tree?
Tree is a non-linear data structure which organizes data in a hierarchical structure
• 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.
Tree-Basic Terminology
5) Sibling − nodes which belong to same Parent are called as SIBLINGS.
• The nodes with the same parent are called Sibling nodes.
Tree-Basic Terminology
Root Node: A
Parent of C and D: A
D is sibling of : B and C
Leaf Nodes: E, F, G, D
Internal Nodes: BCD
Depth of F: 2
Depth of Tree: 2
Depth of Root Node: 0
Height of Tree: 2
Height of A: 2
Height of G: 0
Degree of Node A: 3
Degree of Tree: 3
Degree of C 1
Characteristics of Trees
• Non-linear data structure
• Combines advantages of an ordered array
• Searching as fast as in an ordered array
• Insertion and deletion as fast as in the linked list
Application of Trees
• Directory structure of a file store
• Structure of arithmetic expressions
• Used in almost every 3D video game to determine what objects need
to be rendered.
• Used in almost every high-bandwidth router for storing router-tables.
• used in compression algorithms, such as those used by the .jpeg
and .mp3 file- formats.
Representation Of Trees
Left Child-Right Sibling Representation
List Representation
• Disadvantage: In case of skewed trees, array is not fully utilized leading to wastage of
memory space
Binary Tree – Array Representation
int main() { int set_right(char key, int parent) {
root('A'); if (tree[parent] == '\0')
printf("\nCan't set child at %d , no parent
set_left('B',0);
found",(parent * 2) + 2);
set_right('C', 0); else{
set_left('D', 1); int print_tree() { tree[(parent * 2) + 2] = key;
set_right('E', 1); printf("\n"); printf("right tree done: %c", tree[(parent *
set_right('F', 2); for (int i = 0; 2)i <+ 2]
10;);i++) {
print_tree(); }
if (tree[i] != '\0')
return 0; return 0;
printf("%c",} tree[i]);
} else
printf( "-");
OUTPUT
int set_left(char key, int parent) {
#include <stdio.h>
} if (tree[parent] == '\0')
char tree[10]; return 0; ABCDE-F---
printf("\nCan't set child at %d , no parent
int root(char key) { } A B C D E * F2) + 1);
found",(parent
if (tree[0] != '\0') else{
printf("\nTree already had
root"); tree[(parent * 2) + 1] = key;
else { printf("left tree done: %c", tree[(parent * 2) +
tree[0] = key; 1] );
printf("tree created"); }
} return 0;
return 0; }
}
Binary Tree – Linked List Representation
• Use a linked list to represent a binary tree.
• Every node consists of three fields.
• left child address,
• actual data
• right child address. struct node
• Advantages: {
int data;
• No wastage of space struct node *left;
• Insertions and deletions are easier struct node *right;
• Disadvantages: };
• Does not provide direct access
• Needs additional space for storing left and right subtrees
Binary Tree – Linked List Representation
Binary Tree – Linked List Representation
Binary Tree – Linked List Representation
#include <stdio.h> // Create a new Node
#include <stdlib.h> struct node* create(int value) {
struct node* newNode = malloc(sizeof(struct node));
struct node { newNode->item = value;
int item; newNode->left = NULL;
struct node* left; newNode->right = NULL;
struct node* right; return newNode;
}; }
// Preorder traversal
void preorderTraversal(struct node* root) {
if (root == NULL)
return;
printf("%d ", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
3. Post-order Traversal (follows LRD)
• The left subtree is visited first, then the right subtree and finally the root node.
// Postorder traversal
void postorderTraversal(struct node*
root) {
if (root == NULL)
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->item);
}
Binary Tree – Linked List Representation
int main() { struct node *tmp;
struct node* root = create(1); tmp = search(root, 6);
insertLeft(root, 4); if (tmp)
insertRight(root, 6); {
insertLeft(root->left, 42); printf("\nSearched node=%d in the tree\n", tmp-
insertRight(root->left, 3); >item);
insertLeft(root->right, 2); }
insertRight(root->right, 33); else
{
printf("Traversal of the inserted binary tree \ printf("%d Not found in tree.\n“,6);
n"); }
printf("Inorder traversal \n"); tmp = search(root, 99);
inorderTraversal(root); if (tmp)
{
printf("\nPreorder traversal \n"); printf("\nSearched node=%d in the tree\n", tmp-
preorderTraversal(root); >item);
}
else
printf("\nPostorder traversal \n"); {
printf("%d Not found in tree.\n", 99);
postorderTraversal(root); }
printf("\nheight of the tree: %d\n",
}
height(root));
Binary Tree – Linked List Representation
int height(struct node* node) struct node* search(struct node * tree, int
{ val)
if (node == NULL) {
if(!(tree))
return 0;
{
else { return NULL;
}
// Compute the height of each
subtree if(val == tree->item)
int lheight = height(node->left); {
int rheight = height(node->right); return tree;
// Use the larger one
}
if (lheight > rheight)
else if(search(tree->left, val)==NULL)
return (lheight + 1); {
else search(tree->right, val);
return (rheight + 1); }
} }
}
Binary Tree Traversals
Binary Tree Traversals
Binary Tree Traversals
Inorder Traversal
Preorder Traversal
Postorder Traversal
110
100
Rules
Types of Binary Trees
• AVL Tree
6. Extended Binary Tree
• The full binary tree obtained by adding dummy nodes to a binary tree is called as
Extended Binary Tree.
• A binary tree can be converted into Full Binary tree by adding dummy nodes to
existing nodes wherever required.
Binary Tree – Linked List Representation
#include<stdio.h>
typedef struct node void preorder(node *t) //address of root node is passed in t
{ {
int data; if(t!=NULL)
struct node *left; {
struct node *right; printf("\n%d",t->data); //visit the root
} node; preorder(t->left); //preorder traversal on left subtree
preorder(t->right); //preorder traversal om right subtree
node *create() }
{ }
node *p;
int x; int main()
printf("Enter data(-1 for no data):"); {
scanf("%d",&x); node *root;
if(x==-1) root=create();
return NULL; printf("\nThe preorder traversal of tree is:\n");
p=(node*)malloc(sizeof(node)); preorder(root);
p->data=x; return 0;
printf("Enter left child of %d:\n",x); }
p->left=create();
• The binary search tree is considered as efficient data structure compared to arrays and
linked lists. In the searching process, it removes half sub-tree at every step.
• It also speeds up the insertion and deletion operations compared to that in array and
linked list.
Operations on BST
• Insertion
• Search
• Deletion
• Traversal
BST Insertion
• Insert values 43, 10, 79, 90, 12, 54, 11, 9, 50 into a BST
BST Insertion
BST Operations
void create()
{
int data;
struct bstnode * temp;
printf("Enter data of node to be inserted : ");
scanf("%d", &data);
temp = (struct bstnode *)malloc(sizeof(struct
bstnode));
temp->value = data;
temp->l = temp->r = NULL;
}
tree—pointer search(tree—pointer root, int
key)
{
/*return a pointer to the node that contains
key. If there is no such node, return
NULL.*/
if (! root)
return NULL;
if (key == root->data)
return root;
if (key< root->data)
return search(root->left—child, key);
return search(root->right_child,key);
BST Operations
void insert—node(struct bstnode *node, int num)
/*If num is in the tree pointed at by node do nothing; otherwise add a
new node with data num * /
{
struct bstnode *temp = search(*node, num);
if (temp ==NULL)
{
/*num is not in the tree*/
struct bstnode *ptr= (struct bstnode*)malloc(sizeof(struct
bstnode));
ptr->data = num;
ptr->left—child =ptr->right—child = NULL;
110
100
Rules
Slides prepared by: Dr. Meenakshi Malhotra, Associate Professor, Department of CSE, Dayananda Sagar University, Harohalli, Karnataka. 89
Slides prepared by: Dr. Meenakshi Malhotra, Associate Professor, Department of CSE, Dayananda Sagar University, Harohalli, Karnataka. 90
Slides prepared by: Dr. Meenakshi Malhotra, Associate Professor, Department of CSE, Dayananda Sagar University, Harohalli, Karnataka. 91
Slides prepared by: Dr. Meenakshi Malhotra, Associate Professor, Department of CSE, Dayananda Sagar University, Harohalli, Karnataka. 92
Slides prepared by: Dr. Meenakshi Malhotra, Associate Professor, Department of CSE, Dayananda Sagar University, Harohalli, Karnataka. 93
Slides prepared by: Dr. Meenakshi Malhotra, Associate Professor, Department of CSE, Dayananda Sagar University, Harohalli, Karnataka. 94
2-Way THREADED BINARY TREES
struct threaded_tree {
int left_thread;
threaded_pointer left_child;
char data;
threaded_pointer rightchiId;
int right_thread;
};
2-Way THREADED BINARY TREES
So to maintain the uniformity of threads, we maintain a special node called the header node.
Inorder Traversal of a Threaded Binary Tree
Finding the inorder successor of a node
threaded_pointer insucc(threaded_pointer
tree)
{
/*find the inorder sucessor of tree in a threaded
binary tree */
threaded_pointer temp;
temp = tree->right_child; Inorder traversal of a threaded binary tree
if (!tree->right_thread)
void tinorder(threaded_pointer tree)
while (!temp->left_thread)
{
temp =temp->left_child;
/*traverse the threaded binary tree inorder
return temp; */
} threaded_pointer temp = tree;
for (;;) {
temp = insucc(temp);
if (temp == tree)
break;
printf("%3c", temp->data);
}
}
Inserting A Node Into A Threaded Binary Tree