SlideShare a Scribd company logo
1 of 254
DATA STRUCTURES AND ALGORITHMS
Day One Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Introduction to Data Structures ,[object Object],[object Object],[object Object],[object Object],[object Object]
Introduction to Data Structures ,[object Object],[object Object],[object Object],[object Object],[object Object]
Introduction to Abstract Data Types ,[object Object],[object Object],[object Object],create Create a new collection add Add an item to a collection delete Delete an item from a collection find Find an item matching some criterion in the collection destroy Destroy the collection
Lifetime Of A Variable ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Data Memory Model run-time stack - activation records added and removed as program runs (expands and shrinks in an orderly LIFO manner) space for global variables space for variables allocated at run-time (allocation and de-allocation requests occur in unpredictable order) static data automatic data heap data
Heap Variables ,[object Object],[object Object],[object Object],[object Object]
Array Usage – A Perspective ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Array Usage – A Perspective ,[object Object],[object Object],[object Object]
Array Usage – A Perspective ,[object Object],[object Object]
Array Usage – A Perspective ,[object Object],[object Object],[object Object]
Dynamic Data Structures ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Dynamic Data Structures ,[object Object],[object Object]
Dynamic Data Structures ,[object Object],[object Object],[object Object]
The malloc( ) Function ,[object Object],[object Object],[object Object]
The malloc( ) Function ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The malloc( ) Function ptr 100 Stack Heap Name Marks 100
The malloc( ) Function ,[object Object],[object Object],[object Object]
The free() Function ,[object Object],[object Object]
Self-Referential Structures ,[object Object],[object Object],[object Object]
Self-Referential Structures ,[object Object],[object Object],[object Object]
Self-Referential Structures ,[object Object],[object Object],[object Object]
Linear and non- linear data structures  ,[object Object],[object Object],[object Object],[object Object]
What is the need for ADT, Data types and Data Structures?
LINEAR DATA STRUCTURES
[object Object]
Objectives ,[object Object],[object Object],[object Object],[object Object]
What is a Stack? ,[object Object],[object Object],[object Object]
What is a Stack? ,[object Object],[object Object]
What is a Stack? (c) top 2 4 6 8 10 2 4 6 8 10 12 2 4 6 8 10 12 14 (a) (b) top top
Characteristics of a Stack ,[object Object],[object Object],[object Object]
Characteristics of a Stack ,[object Object],[object Object]
Operations on Stacks ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Implementation of Stacks ,[object Object],[object Object],[object Object]
Implementation of Stacks ,[object Object],[object Object],[object Object]
Code Implementing for a Stack  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Code Implementing for a Stack ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Code Implementing for a Stack ,[object Object],[object Object],[object Object],[object Object]
Implementing push( )  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Implementing push( ) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A View of the Stack After Insertion next info info next new top 1 2 3
Creating a Node on a Stack ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Implementing pop( ) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Implementing pop( ) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A View of the Stack After Deletion next top temp A B
Applications of Stacks ,[object Object],[object Object],[object Object],[object Object]
Applications of Stacks ,[object Object],[object Object],[object Object]
Applications of Stacks ,[object Object],[object Object]
Applications of Stacks ,[object Object],[object Object],[object Object]
Applications of Stacks ,[object Object],[object Object],[object Object],[object Object]
[object Object]
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object]
Defining a Queue ,[object Object],[object Object],[object Object]
Defining a Queue ,[object Object],[object Object],[object Object]
Queue Insertions and Deletions rear front front rear front 1  2  3 1  2  3  4  2  3  4 rear (a) (b) (c)
Queue Operations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Queue Operations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Implementing Queues ,[object Object],[object Object]
Implementing Queues ,[object Object],[object Object],rear front 2 4 7
Queue Declaration & Operations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Queue Operations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Insertion into a Queue ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Insertion into a Queue ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Insertion into a Queue ,[object Object],[object Object],[object Object],[object Object]
Insertion into a Queue ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Node on a Queue ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Insertion into a Queue New node inserted at rear of queue next next next rear front 2 4 7 8 New
Deletion from a Queue ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Deletion of a Node From a Queue Node being deleted at the front of the queue rear front 2 4 7
Applications of Queues ,[object Object],[object Object],[object Object]
Applications of Queues ,[object Object],[object Object],[object Object],[object Object]
Disadvantages of a Queue ,[object Object],[object Object],[object Object],[object Object]
Double – ended Queue ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Circular Queue ,[object Object],[object Object],[object Object]
Pictorial Representation of Circular Queue f, r (a) (b) After inserting 20 & 30 After inserting 40 & 50 (c) 10 0 2 4 1 3 10 0 2 4 1 3 20 30 f r 10 0 2 4 1 3 20 30 f r 40 50 10 f 20 30 r 40 50 0  1  2  3  4 10 f 20 30 r 10 f r 0  1  2  3  4 0  1  2  3  4
Pictorial Representation of Circular Queue (contd…) (d) After deleting 10 & 20 (e) After inserting 60 f r 0 2 4 1 3 30 40 50 f r 0 2 4 1 3 30 40 50 60 f 30 r 40 50 0  1  2  3  4 f 30 r 40 50 60 0  1  2  3  4
[object Object],[object Object],[object Object],[object Object],[object Object],Pictorial Representation of Circular Queue (contd…)
[object Object],[object Object],[object Object],[object Object],Pictorial Representation of Circular Queue (contd…) >>Program
Priority Queue ,[object Object],[object Object],[object Object]
Types of Priority Queues ,[object Object],[object Object],[object Object],[object Object],[object Object]
LINKED LISTS
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Linked Lists – An Introduction ,[object Object],[object Object],[object Object],[object Object],[object Object]
Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Types of linked lists ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Elementary linked list functions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Declaring a Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Declaring a Linked List ,[object Object],[object Object],[object Object]
Visualizing a Linked List marks next 180 100 140 start 100 75  140 85  180 95  230 100   x marks next marks next marks next Stack  Heap
Insertion of an element into a linked list ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Inserting an element at the head of a linked list   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Inserting an element in the middle of  a linked list ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Inserting an element at the tail of a linked list  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Deleting a node in a linked list
Creating a Sorted Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Searching a Value in a Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Deleting a Node From a Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Deleting a Node From a Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Need For a Doubly Linked List ,[object Object],[object Object],[object Object]
Properties of a Doubly Linked List ,[object Object],[object Object],[object Object]
Properties of a Doubly Linked List ,[object Object],[object Object],[object Object],[object Object]
Declaration of a Doubly Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Visualizing a Doubly Linked List prior next marks null 1 120 100 prior next marks 100 2 140 120 prior next marks 120 3 null 140 100 start 140 last
Creating a Sorted Doubly Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Doubly Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Doubly Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Doubly Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Doubly Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Doubly Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Doubly Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating a Sorted Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Searching a Value in a Doubly Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Deleting a Node From a Doubly Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],/* deletion of the first node in the list */  if (score = = start-> marks) { temp =start; start = start-> next; start->prior = null; free(temp); }
Deleting a Node From a Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],/* deletion at the end of the list */ if (ptr->next = = null) { temp = ptr; prev->next = null; last = ptr->prior; } } }
Traversal of a Doubly Linked List ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Summary ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Trees ,[object Object],[object Object],[object Object],[object Object]
Trees Rajeev Ravi Vijay Anjali Ramesh Suresh Sukesh Arvind
Trees ,[object Object],[object Object],[object Object]
Trees ,[object Object],[object Object],[object Object],[object Object]
Trees ,[object Object],[object Object],[object Object],[object Object]
Tree ,[object Object],[object Object],[object Object],[object Object]
Binary Tree ,[object Object],[object Object],[object Object]
Binary Tree ,[object Object],[object Object],[object Object],[object Object]
Balanced Binary Tree 4 6 5 7 3 1 2 N N N N N N N N
Data Structure Representation of a  Binary Trees ,[object Object],[object Object],[object Object]
Data Structure Representation of a  Binary Trees ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Traversing a Binary Tree ,[object Object],[object Object],[object Object],[object Object]
Traversing a Binary Tree ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Traversing a Binary Tree ,[object Object],[object Object],[object Object],[object Object]
Traversing a Binary Tree ,[object Object],[object Object],[object Object],[object Object]
Traversing a Binary Tree ,[object Object],[object Object],[object Object],[object Object]
Traversing a Binary Tree A B C D E
Preorder Traversal  ,[object Object],[object Object],[object Object],[object Object]
Preorder Traversal ,[object Object],[object Object],[object Object]
Inorder Traversal ,[object Object],[object Object],[object Object]
Inorder Traversal ,[object Object],[object Object],[object Object]
Postorder Traversal ,[object Object],[object Object],[object Object],[object Object]
Code - Preorder Traversal ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Code – Inorder Traversal ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Code – Postorder Traversal ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Accessing Values From a Binary Search Tree Using Inorder Traversal ,[object Object],[object Object],[object Object]
Insertion into a Tree  ,[object Object],[object Object],[object Object],[object Object]
Insertion into a Tree ,[object Object],[object Object],10 Root
Insertion into a Tree ,[object Object],10 Root 15
Insertion into a Tree ,[object Object],[object Object],10 Root 15 12
Insertion into a Tree ,[object Object],10 15 12 7
Insertion into a Tree ,[object Object],10 15 12 7 8 10 15 12 7 8 18
Insertion into a Tree 10 15 12 7 8 18 6 10 15 12 7 8 18 6 20
Insertion into a Tree ,[object Object],[object Object],[object Object]
Insertion into a Tree ,[object Object],[object Object]
Insertion into a Tree 10 15 12 7 8 18 6 20 Root p
Insertion into a Tree ,[object Object],[object Object]
Insertion into a Tree 10 15 12 7 8 18 6 20 Root p null
Insertion into a Tree ,[object Object],[object Object],[object Object]
Insertion into a Tree ,[object Object],[object Object],[object Object],[object Object]
Insertion into a Tree ,[object Object],[object Object]
Creating a Tree – A Special Case  of Insertion ,[object Object],[object Object],[object Object]
Code Implementation For Insertion  Into a Tree ,[object Object],[object Object]
Code Implementation For Insertion  Into a Tree ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Code Implementation For Insertion  Into a Tree ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Code Implementation For Insertion  Into a Tree ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Code Implementation For Insertion  Into a Tree Using Recursion ,[object Object],[object Object],[object Object],[object Object]
Code Implementation For Insertion  Into a Tree Using Recursion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Code Implementation For Insertion  Into a Tree Using Recursion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Circumstances When a Binary Tree Degenerates into a Linked List  ,[object Object],[object Object],[object Object]
Circumstances When a Binary Tree Degenerates into a Linked List  10 15 12 7 8 18 6 20 Root
Circumstances When a Binary Tree Degenerates into a Linked List  ,[object Object],[object Object],[object Object]
Circumstances When a Binary Tree Degenerates into a Linked List  A Lopsided Binary Tree With Only Right Subtrees 6 7 8 10 12 15 18 20
Circumstances When a Binary Tree Degenerates into a Linked List  ,[object Object],[object Object],[object Object]
Circumstances When a Binary Tree Degenerates into a Linked List A Lopsided Binary Tree With Only Left Subtrees 20 18 15 12 10 8 7 6
Deletion from a Binary Search Tree ,[object Object],[object Object],[object Object]
Case I – Deletion Of The Leaf Node  ,[object Object],[object Object],[object Object]
Case I – Deletion Of The Leaf Node 2 p 2 null 3 3 1
Case II – Deletion Of a Node  With a Single Child ,[object Object],[object Object],[object Object]
Case II – Deletion Of a Node  With a Single Child To delete node containing the value 3, where the right subtree of 3 is empty, we simply make the link of the parent of the node with the value 3 (node with value 5) point to the child of 3 (node with the value 2). Node to be deleted 5 4 3 2 Node to be deleted 5 4 3 2
Case III – Deletion Of a Node  With Two Child Nodes ,[object Object],[object Object],[object Object]
Case III – Deletion Of a Node  With Two Child Nodes ,[object Object],[object Object],[object Object]
Case III – Deletion Of a Node  With Two Child Nodes y Delete node x r q x t s u z Before Deletion of Node x r q t y s u z After Deletion of Node x
Code Implementation for Node Deletion for Cases I, II & III ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],p = p->right; free(temp); } else if (p->right == null) { temp = p; p = p->left; free (temp); }
Code Implementation for Node Deletion for Cases I, II & III ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Code Implementation for Node Deletion for Cases I, II & III ,[object Object],[object Object]
Search The Tree ,[object Object],[object Object],[object Object],[object Object]
Search the Tree ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
GRAPHS
SEARCHING
Searching ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Eliminative or a Binary Search ,[object Object],[object Object],[object Object]
Eliminative or a Binary Search ,[object Object],[object Object],[object Object],[object Object]
Eliminative or a Binary Search ,[object Object],Coin Search in a Group A B C D E F G H I J K L M N O P Q R X YES NO
Eliminative or a Binary Search ,[object Object],[object Object]
Searching - Binary search ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Dominant term
Employing the Linear Search  ,[object Object],[object Object],[object Object]
Employing the Linear Search ,[object Object],[object Object]
Employing the Eliminative or  The Binary Search  ,[object Object],[object Object],[object Object],[object Object]
Employing the Eliminative or  The Binary Search  ,[object Object],[object Object],[object Object]
Employing the Eliminative or  The Binary Search  ,[object Object],J K L M N O P Q R X 6 searches saved NO YES
Employing the Eliminative or  The Binary Search  ,[object Object],[object Object],[object Object],P Q R X NO YES
Eliminative or Binary Search ,[object Object],[object Object],[object Object]
Binary Vs. Linear Search ,[object Object],[object Object],[object Object]
Linear Search in a Linked List 100 1  120 2  140 3  160  4  180 5  200 6  220 7  null
Binary Search in a Binary Search Tree 4 6 5 7 3 1 2 N N N N N N N N 7 Search Value 7 Search Value 7 Search Value
The Essence of a Binary Search ,[object Object],[object Object],[object Object]
The Essence of a Binary Search ,[object Object],[object Object]
Searching - Re-visited ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Tree Traversal ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],x  A  + x + B C  x D E  F L R L L R     
Tree Traversal ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],          11 A  x  B + C  x D x E  + F  L R L
Tree Traversal ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],          11 A  B  C  +  D  E  x x  F + x  L R L
Tree Traversal ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],(A (((BC+)(DE x )  x ) F +) x  )            (A x(((B+C)(D x E))+F))  11
Trees -  Searching ,[object Object],[object Object],[object Object]
Trees -  Searching ,[object Object],[object Object],[object Object]
Trees -  Searching ,[object Object],[object Object],[object Object],[object Object]
Trees -  Rotations ,[object Object],[object Object],[object Object],[object Object]
Trees -  Rotations ,[object Object],[object Object],[object Object]
Trees -  Red-Black Trees ,[object Object],[object Object],[object Object],[object Object]
Trees -  Red-Black Trees ,[object Object],[object Object],[object Object],When you examine rb-tree code, you will see  sentinel  nodes (black)  added as the leaves. They contain no data. Sentinel  nodes (black)
Trees -  Red-Black Trees ,[object Object],[object Object],[object Object],[object Object],This implies that no path may have two adjacent RED  nodes. (But any number of BLACK nodes may be adjacent.)
Trees -  Red-Black Trees ,[object Object],[object Object],[object Object],[object Object],[object Object],From the root, there are 3 BLACK nodes  on every path
Trees -  Red-Black Trees ,[object Object],[object Object],[object Object],[object Object],[object Object],The length of this path is the black height  of the tree
Trees -  Red-Black Trees ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Trees -  Red-Black Trees ,[object Object],[object Object],[object Object],Same as a binary tree with these two attributes added struct t_red_black_node { enum { red, black } colour; void *item; struct t_red_black_node *left, *right, *parent; }
Trees -  Insertion ,[object Object],[object Object],rb_insert( Tree T, node x ) { /* Insert in the tree in the usual way */ tree_insert( T, x ); /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; Label the current node   x Insert node   4 Mark it   red
Trees -  Insertion rb_insert( Tree T, node x ) { /* Insert in the tree in the usual way */ tree_insert( T, x ); /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; While we haven’t reached the root and x’s parent is  red   x->parent
Trees -  Insertion rb_insert( Tree T, node x ) { /* Insert in the tree in the usual way */ tree_insert( T, x ); /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; If x is to the left of it’s granparent x->parent x->parent->parent
Trees -  Insertion /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; y is x’s right  uncle x->parent x->parent->parent right “uncle”
Trees -  Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; x->parent x->parent->parent right “uncle” If the  uncle  is  red,  change the colours of y, the grand-parent  and the parent
Trees -  Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right &apo
Data structures
Data structures
Data structures
Data structures
Data structures
Data structures
Data structures
Data structures
Data structures
Data structures
Data structures
Data structures
Data structures

More Related Content

What's hot

Data structure power point presentation
Data structure power point presentation Data structure power point presentation
Data structure power point presentation Anil Kumar Prajapati
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
Queue implementation
Queue implementationQueue implementation
Queue implementationRajendran
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked ListReazul Islam
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structureMAHALAKSHMI P
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST Kathirvel Ayyaswamy
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structuresDurgaDeviCbit
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked listchauhankapil
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applicationsJsaddam Hussain
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expressionDrkhanchanaR
 

What's hot (20)

Data structure power point presentation
Data structure power point presentation Data structure power point presentation
Data structure power point presentation
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Stacks
StacksStacks
Stacks
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Data structures
Data structuresData structures
Data structures
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Stack
StackStack
Stack
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
Queue
QueueQueue
Queue
 

Viewers also liked

DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 
Moving on from Narcissistic Abuse due to Narcissistic Personality Disorder (N...
Moving on from Narcissistic Abuse due to Narcissistic Personality Disorder (N...Moving on from Narcissistic Abuse due to Narcissistic Personality Disorder (N...
Moving on from Narcissistic Abuse due to Narcissistic Personality Disorder (N...Jeni Mawter
 
Skeletal system
Skeletal systemSkeletal system
Skeletal systemcoachhuey
 
Introductory Lecture on photography
Introductory Lecture on photographyIntroductory Lecture on photography
Introductory Lecture on photographyAditya Rao
 
Collection and Conveyance of Water
Collection and Conveyance of WaterCollection and Conveyance of Water
Collection and Conveyance of WaterGAURAV. H .TANDON
 
Off the Page Into the Wild: Designing For the Internet of Things
Off the Page Into the Wild: Designing For the Internet of ThingsOff the Page Into the Wild: Designing For the Internet of Things
Off the Page Into the Wild: Designing For the Internet of Thingsfrog
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structuresNiraj Agarwal
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and AlgorithmDhaval Kaneria
 
Augmented Reality and Education: Learning connected to life - Reloaded
Augmented Reality and Education: Learning connected to life - ReloadedAugmented Reality and Education: Learning connected to life - Reloaded
Augmented Reality and Education: Learning connected to life - ReloadedRaúl Reinoso
 
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...Matt Raible
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
10 steps to your career makeover
10 steps to your career makeover10 steps to your career makeover
10 steps to your career makeoverGetSmarter
 

Viewers also liked (20)

Data Structure
Data StructureData Structure
Data Structure
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
Dental Amalgam
Dental AmalgamDental Amalgam
Dental Amalgam
 
Moving on from Narcissistic Abuse due to Narcissistic Personality Disorder (N...
Moving on from Narcissistic Abuse due to Narcissistic Personality Disorder (N...Moving on from Narcissistic Abuse due to Narcissistic Personality Disorder (N...
Moving on from Narcissistic Abuse due to Narcissistic Personality Disorder (N...
 
Skeletal system
Skeletal systemSkeletal system
Skeletal system
 
Introductory Lecture on photography
Introductory Lecture on photographyIntroductory Lecture on photography
Introductory Lecture on photography
 
Brand Management Careers
Brand Management CareersBrand Management Careers
Brand Management Careers
 
Collection and Conveyance of Water
Collection and Conveyance of WaterCollection and Conveyance of Water
Collection and Conveyance of Water
 
Off the Page Into the Wild: Designing For the Internet of Things
Off the Page Into the Wild: Designing For the Internet of ThingsOff the Page Into the Wild: Designing For the Internet of Things
Off the Page Into the Wild: Designing For the Internet of Things
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
 
Stroke
StrokeStroke
Stroke
 
Augmented Reality and Education: Learning connected to life - Reloaded
Augmented Reality and Education: Learning connected to life - ReloadedAugmented Reality and Education: Learning connected to life - Reloaded
Augmented Reality and Education: Learning connected to life - Reloaded
 
Data science and_analytics_for_ordinary_people_ebook
Data science and_analytics_for_ordinary_people_ebookData science and_analytics_for_ordinary_people_ebook
Data science and_analytics_for_ordinary_people_ebook
 
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
 
How to do lean planning
How to do lean planningHow to do lean planning
How to do lean planning
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
 
10 steps to your career makeover
10 steps to your career makeover10 steps to your career makeover
10 steps to your career makeover
 

Similar to Data structures

C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&aKumaran K
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparationKushaal Singla
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfkarymadelaneyrenne19
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)rashmita_mishra
 
Tcs NQTExam technical questions
Tcs NQTExam technical questionsTcs NQTExam technical questions
Tcs NQTExam technical questionsAniketBhandare2
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.pptshivani366010
 
C basic questions&ansrs by shiva kumar kella
C basic questions&ansrs by shiva kumar kellaC basic questions&ansrs by shiva kumar kella
C basic questions&ansrs by shiva kumar kellaManoj Kumar kothagulla
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxGIRISHKUMARBC1
 
C interview question answer 1
C interview question answer 1C interview question answer 1
C interview question answer 1Amit Kapoor
 
implementing oop_concept
 implementing oop_concept implementing oop_concept
implementing oop_conceptAmit Gupta
 
Interview preparation for programming.pptx
Interview preparation for programming.pptxInterview preparation for programming.pptx
Interview preparation for programming.pptxBilalHussainShah5
 

Similar to Data structures (20)

C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
Technical Interview
Technical InterviewTechnical Interview
Technical Interview
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
What is c language
What is c languageWhat is c language
What is c language
 
Bt0065
Bt0065Bt0065
Bt0065
 
B T0065
B T0065B T0065
B T0065
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Lecture 3.mte 407
Lecture 3.mte 407Lecture 3.mte 407
Lecture 3.mte 407
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Tcs NQTExam technical questions
Tcs NQTExam technical questionsTcs NQTExam technical questions
Tcs NQTExam technical questions
 
structures.ppt
structures.pptstructures.ppt
structures.ppt
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.ppt
 
C basic questions&ansrs by shiva kumar kella
C basic questions&ansrs by shiva kumar kellaC basic questions&ansrs by shiva kumar kella
C basic questions&ansrs by shiva kumar kella
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
Structure & union
Structure & unionStructure & union
Structure & union
 
C interview question answer 1
C interview question answer 1C interview question answer 1
C interview question answer 1
 
implementing oop_concept
 implementing oop_concept implementing oop_concept
implementing oop_concept
 
Interview preparation for programming.pptx
Interview preparation for programming.pptxInterview preparation for programming.pptx
Interview preparation for programming.pptx
 

More from Saurabh Mishra (8)

Sorting2
Sorting2Sorting2
Sorting2
 
Sorting
SortingSorting
Sorting
 
Searching
SearchingSearching
Searching
 
Presentation1
Presentation1Presentation1
Presentation1
 
Graps 2
Graps 2Graps 2
Graps 2
 
Graphs
GraphsGraphs
Graphs
 
Trees
TreesTrees
Trees
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 

Data structures

  • 1. DATA STRUCTURES AND ALGORITHMS
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. Data Memory Model run-time stack - activation records added and removed as program runs (expands and shrinks in an orderly LIFO manner) space for global variables space for variables allocated at run-time (allocation and de-allocation requests occur in unpredictable order) static data automatic data heap data
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18. The malloc( ) Function ptr 100 Stack Heap Name Marks 100
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25. What is the need for ADT, Data types and Data Structures?
  • 27.
  • 28.
  • 29.
  • 30.
  • 31. What is a Stack? (c) top 2 4 6 8 10 2 4 6 8 10 12 2 4 6 8 10 12 14 (a) (b) top top
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42. A View of the Stack After Insertion next info info next new top 1 2 3
  • 43.
  • 44.
  • 45.
  • 46. A View of the Stack After Deletion next top temp A B
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56. Queue Insertions and Deletions rear front front rear front 1 2 3 1 2 3 4 2 3 4 rear (a) (b) (c)
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68. Insertion into a Queue New node inserted at rear of queue next next next rear front 2 4 7 8 New
  • 69.
  • 70. Deletion of a Node From a Queue Node being deleted at the front of the queue rear front 2 4 7
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76. Pictorial Representation of Circular Queue f, r (a) (b) After inserting 20 & 30 After inserting 40 & 50 (c) 10 0 2 4 1 3 10 0 2 4 1 3 20 30 f r 10 0 2 4 1 3 20 30 f r 40 50 10 f 20 30 r 40 50 0 1 2 3 4 10 f 20 30 r 10 f r 0 1 2 3 4 0 1 2 3 4
  • 77. Pictorial Representation of Circular Queue (contd…) (d) After deleting 10 & 20 (e) After inserting 60 f r 0 2 4 1 3 30 40 50 f r 0 2 4 1 3 30 40 50 60 f 30 r 40 50 0 1 2 3 4 f 30 r 40 50 60 0 1 2 3 4
  • 78.
  • 79.
  • 80.
  • 81.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90. Visualizing a Linked List marks next 180 100 140 start 100 75 140 85 180 95 230 100 x marks next marks next marks next Stack Heap
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114. Visualizing a Doubly Linked List prior next marks null 1 120 100 prior next marks 100 2 140 120 prior next marks 120 3 null 140 100 start 140 last
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132. Trees Rajeev Ravi Vijay Anjali Ramesh Suresh Sukesh Arvind
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139. Balanced Binary Tree 4 6 5 7 3 1 2 N N N N N N N N
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147. Traversing a Binary Tree A B C D E
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163. Insertion into a Tree 10 15 12 7 8 18 6 10 15 12 7 8 18 6 20
  • 164.
  • 165.
  • 166. Insertion into a Tree 10 15 12 7 8 18 6 20 Root p
  • 167.
  • 168. Insertion into a Tree 10 15 12 7 8 18 6 20 Root p null
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181. Circumstances When a Binary Tree Degenerates into a Linked List 10 15 12 7 8 18 6 20 Root
  • 182.
  • 183. Circumstances When a Binary Tree Degenerates into a Linked List A Lopsided Binary Tree With Only Right Subtrees 6 7 8 10 12 15 18 20
  • 184.
  • 185. Circumstances When a Binary Tree Degenerates into a Linked List A Lopsided Binary Tree With Only Left Subtrees 20 18 15 12 10 8 7 6
  • 186.
  • 187.
  • 188. Case I – Deletion Of The Leaf Node 2 p 2 null 3 3 1
  • 189.
  • 190. Case II – Deletion Of a Node With a Single Child To delete node containing the value 3, where the right subtree of 3 is empty, we simply make the link of the parent of the node with the value 3 (node with value 5) point to the child of 3 (node with the value 2). Node to be deleted 5 4 3 2 Node to be deleted 5 4 3 2
  • 191.
  • 192.
  • 193. Case III – Deletion Of a Node With Two Child Nodes y Delete node x r q x t s u z Before Deletion of Node x r q t y s u z After Deletion of Node x
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199. GRAPHS
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215. Linear Search in a Linked List 100 1 120 2 140 3 160 4 180 5 200 6 220 7 null
  • 216. Binary Search in a Binary Search Tree 4 6 5 7 3 1 2 N N N N N N N N 7 Search Value 7 Search Value 7 Search Value
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237. Trees - Insertion rb_insert( Tree T, node x ) { /* Insert in the tree in the usual way */ tree_insert( T, x ); /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; While we haven’t reached the root and x’s parent is red x->parent
  • 238. Trees - Insertion rb_insert( Tree T, node x ) { /* Insert in the tree in the usual way */ tree_insert( T, x ); /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; If x is to the left of it’s granparent x->parent x->parent->parent
  • 239. Trees - Insertion /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; y is x’s right uncle x->parent x->parent->parent right “uncle”
  • 240. Trees - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; x->parent x->parent->parent right “uncle” If the uncle is red, change the colours of y, the grand-parent and the parent
  • 241. Trees - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right &apo

Editor's Notes

  1. Dynamic memory allocation and de-allocation of memory is therefore, the responsibility of the programmer. The malloc( ) function allocates a block of memory on heap, and returns the starting offset of the block of memory allocated on the heap. This can be stored in a pointer declared as part of the stack, and access to the heap variable can be had using this pointer. When returning from the function, care must be taken by the programmer to return the storage occupied by the heap variable back to the heap. This is done using the function free( ) to which the pointer pointing to the heap variable is passed as an argument. The free( ) function de-allocates the block of memory on the heap used by the heap variable, and returns it back to the heap. If the call to the function free( ) is not issued before returning from the function, the pointer (stack variable) pointing to the heap variable will be destroyed when returning from the function, and therefore, the address to the heap variable will be lost. There will then be no way to de-allocate this block of memory on the heap, and return it back to the heap. This amount of memory will not be available for future use by other applications. This is what is referred to as a memory leak.
  2. The size of the stack cannot grow to accommodate these runtime variables as the stack size is determined at the time of compilation. Therefore, dynamically created variables are accommodated into another area of memory called the heap or free store.
  3. One way of determining the size of the structure marks_data and pass it as an argument to malloc() is to determine the size of the element name (11 bytes) and marks (2 bytes or 4 bytes) depending on the platform. Therefore, the size argument to be passed to malloc() can be 13 bytes or 15 bytes. Rather than count the number of bytes constituting the structure marks_data that is platform dependent, a better way should be for the program to determine the size of the elements constituting the structure marks_data based on the platform. This is where the sizeof( ) operator comes into the picture. What we had requested malloc() in the aforesaid example is a block on the Heap representing the structure marks_data and accordingly this structure on the heap was allocated.
  4. In diagram (a), 10 is the current top element of the stack. If we add any element in the stack, it will be placed on top of 10, and if we delete an element, it will be 10 that is on top of the stack.
  5. If a stack is empty, and it contains no element, it is not possible to pop the stack. Therefore, before popping an element, you must ensure that the stack is not empty. As the definition of a stack does not presuppose the number of elements in it, there is no upper limit on the number of elements in the stack, memory constraints notwithstanding.
  6. Therefore, implementing a stack as an array prohibits the growth of the stack beyond the finite number of elements that the declared array can contain.
  7. To understand queues in a better way, consider a queue containing three elements as shown if figure (a). If you want to add element 4 to this queue, you can do so only at the rear end of the queue as shown in figure (b). Now, if you remove an element from this queue, it should be element 1 that is to be deleted because this is the element that has come first in the queue and is at the front. After deleting 1, element 2 will be at the front as shown in figure (c).
  8. You can insert a new node at the end of the list after the last node that is pointed to by pointer rear. You must take care when you insert an element into an empty queue as in this case you need to adjust the front pointer as well to point to this element.
  9. Whenever you remove an element from a queue, you must ensure that the queue is not empty. If you are deleting the last element, you must ensure that q->rear = null to indicate that the queue is now empty.
  10. Point 4: If we used the statement r= r+1; the value of r would be 5, but because it is a circular queue, the value of r should be 0. This can be achieved by using the statement:- r = (r+1)%QUEUE_SIZE;
  11. The first node at address 100 (fictitious memory addresses) is referenced by a pointer called start (declared on the stack) . Once you are able to access the first node through start , you would be able to access all subsequent nodes through next . The pointer next of the last node points to no node in particular, and therefore contains the value NULL .
  12. above For example, we wish to use a data structure to represent a person, and all of his or her descendants. Assume that the person’s name is Rajeev and that he has three children, Ravi, Vijay and Anjali. Ravi has three children Ramesh, Suresh and Sukesh. And Vijay has Arvind.
  13. A search in such lopsided trees would degenerate into a linear search, and such a tree bears the functionality of a linked list. Such lopsided trees are not efficient from the perspective of search efficiency
  14. You should also note that this type of deletion maintains the binary search tree but increases the height of the tree. Thus, it increases the time required for a search. Hence, to optimize searching through a binary search tree, you need methods that make the left and the right subtrees as balanced as possible.
  15. In the calling routine, you can check whether the returned value of p is null or not. If it is null, the calling routine can give a message that the searched value is not there in the tree. The maximum number of comparisons in a binary search tree is equal to the depth of the tree whereas in the case of a linear linked list, the maximum number of comparisons is equal to the number of nodes in the list.
  16. Consider the worst-case scenario of searching for the value 7 in the linked list. This would entail starting with the first node in the list and progressing linearly through the subsequent nodes till one comes to the value being searched for, which is 7. This would involve a maximum of 7 searches.
  17. Consider the scenario of searching for the same value 7 in a balanced binary search tree. The search begins at the root node. Since the value being searched for is greater than the value in the root node, you need to search from the top of the right subtree to locate 7. Since the value being searched is also greater than the value at the top of the right subtree, you move to the top of the next right subtree of the current subtree where the search value 7 is located.