Post on 25-May-2015
transcript
Data Structures &
Applications
By: M V B REDDY
GITAM UNIVERSITY ,BLRWednesday, April 12, 2023 1
Data Structure is the structural representation of logical relationships between elements of data.
Data Structure = Organized Data + OperationsData Structure + Algorithm = Program
Algorithm is a step-by-step finite sequence of instructions, to solve a well-defined computational problem.
• Data Structure (In Memory) = Storage Structure• Data Structure (In Auxiliary Memory) = File Structure
Complexity Analysis of AlgorithmTime Complexity* Space Complexity**
* Time depends on Algorithms, Languages, Compilers, CPU Speed etc. It is analyzed for best, average and worst case of input data.** Space needed by instruction space, data space and environ. Stack space.
Amstrong Complexity (amortized)2
Wednesday, April 12, 2023 3
Data Structure
Primitive DS Non-primitive DS
Integer Float Character Pointer Arrays Lists Files
Linear List Non-linear List
Stacks Queues Graphs Trees
Wednesday, April 12, 2023 4
ARRAYS
An array is a collection of homogeneous data elements described by a single name. Each element is referenced by a subscripted variable or value, called subscript or index enclosed in parenthesis.
• If an element is referenced by a single subscript, then array is known as 1-D or single array (linear array). --- Array[N]
• If two subscripts are required, the array is known as 2-D or matrix array. ---- Array[M][N]
• An array referenced by two or more subscripts is known as multidimensional array. ----- Array[X][Y][Z]
Sparse array is an application of arrays where nearly all the elements have same values (usually zeros) and this value is constant. 1-D sparse array is called sparse vector and 2-D sparse array is called sparse matrix.
Wednesday, April 12, 2023 5
LISTSA list is a ordered set consisting of a varying number of elements (or nodes) linked by means of pointers. Each node is divided into two parts:
A list overcome the limitation of fixed size in an array. A list may be linear ( stack, Queue) or non-linear (Tree, Graph).
NODE
Types of linked list (LL):
1. Singly LL 2. Doubly LL 3. Circular LL
INFO LINK
DATA LINK DATA LINK DATA LINK DATA LINK
START
Wednesday, April 12, 2023 6
Types of linked lists
1. Singly linked listBegins with a pointer to the first nodeTerminates with a null pointerOnly traversed in one direction
2. Circular, singly linked list (e.g., time sharing in OS, multiplayer game)Pointer in the last node points back to the first node
3. Doubly linked list (e.g., applications need more deletions)Two “start pointers” – first element and last elementEach node has a forward pointer and a backward pointerAllows traversals both forwards and backwards
4. Circular, doubly linked list Forward pointer of the last node points to the first node and backward pointer of the first node points to the last node
* Free storage, garbage collection, Dangling reference, reference counter, storage compaction
Wednesday, April 12, 2023 7
STACKSA stack is a non-primitive linear data structure (LIFO). It is an ordered collections of items where insertion and deletion take place at one end only called top of stack (TOS).
Stack Operations:Push() Pop() Top() Size() IsEmpty()
Stack Applications:• Page-visited history in a Web browser• Undo sequence in a text editor• Saving local variables when there is recursive function calls• Conversion of infix to postfix expression• Auxiliary data structure for algorithms• Component of other data structures
Wednesday, April 12, 2023 8
1 /* stack.c2 dynamic stack program */3 #include <stdio.h>4 #include <stdlib.h>56 struct stackNode { /* self-referential structure */7 int data;8 struct stackNode *nextPtr;9 };1011 typedef struct stackNode StackNode;12 typedef StackNode *StackNodePtr;1314 void push( StackNodePtr *, int );15 int pop( StackNodePtr * );16 int isEmpty( StackNodePtr );17 void printStack( StackNodePtr );18 void instructions( void );1920 int main()21 { 22 StackNodePtr stackPtr = NULL; /* points to stack top */23 int choice, value;2425 instructions();26 printf( "? " );27 scanf( "%d", &choice );28
Outline1. Define struct
1.1 Function definitions
1.2 Initialize variables
2. Input choice
Wednesday, April 12, 2023 9
29 while ( choice != 3 ) { 3031 switch ( choice ) { 32 case 1: /* push value onto stack */33 printf( "Enter an integer: " );34 scanf( "%d", &value );35 push( &stackPtr, value );36 printStack( stackPtr );37 break;38 case 2: /* pop value off stack */39 if ( !isEmpty( stackPtr ) )40 printf( "The popped value is %d.\n", 41 pop( &stackPtr ) );4243 printStack( stackPtr );44 break;45 default:46 printf( "Invalid choice.\n\n" );47 instructions();48 break;49 }5051 printf( "? " );52 scanf( "%d", &choice );53 }5455 printf( "End of run.\n" );56 return 0;57 }58
Outline2.1 switch statement
Wednesday, April 12, 2023 10
59 /* Print the instructions */60 void instructions( void )61 { 62 printf( "Enter choice:\n"63 "1 to push a value on the stack\n"64 "2 to pop a value off the stack\n"65 "3 to end program\n" );66 }6768 /* Insert a node at the stack top */69 void push( StackNodePtr *topPtr, int info )70 { 71 StackNodePtr newPtr;7273 newPtr = malloc( sizeof( StackNode ) );74 if ( newPtr != NULL ) { 75 newPtr->data = info;76 newPtr->nextPtr = *topPtr;77 *topPtr = newPtr;78 }79 else80 printf( "%d not inserted. No memory available.\n",81 info );82 }83
Outline3. Function definitions
Wednesday, April 12, 2023 11
84 /* Remove a node from the stack top */85 int pop( StackNodePtr *topPtr )86 { 87 StackNodePtr tempPtr;88 int popValue;8990 tempPtr = *topPtr;91 popValue = ( *topPtr )->data;92 *topPtr = ( *topPtr )->nextPtr;93 free( tempPtr );94 return popValue;95 }9697 /* Print the stack */98 void printStack( StackNodePtr currentPtr )99 { 100 if ( currentPtr == NULL )101 printf( "The stack is empty.\n\n" );102 else { 103 printf( "The stack is:\n" );104105 while ( currentPtr != NULL ) { 106 printf( "%d --> ", currentPtr->data );107 currentPtr = currentPtr->nextPtr;108 }109110 printf( "NULL\n\n" );111 }112 }113
Outline3. Function definitions
Wednesday, April 12, 2023 12
114 /* Is the stack empty? */115 int isEmpty( StackNodePtr topPtr )116 { 117 return topPtr == NULL;118 }
Enter choice:1 to push a value on the stack2 to pop a value off the stack3 to end program? 1Enter an integer: 5The stack is:5 --> NULL ? 1Enter an integer: 6The stack is:6 --> 5 --> NULL
? 1Enter an integer: 4The stack is:4 --> 6 --> 5 --> NULL ? 2The popped value is 4.The stack is:6 --> 5 --> NULL
Outline3. Function definitions
Program Output
Wednesday, April 12, 2023 13
? 2The popped value is 6.The stack is:5 --> NULL ? 2The popped value is 5.The stack is empty. ? 2The stack is empty. ? 4Invalid choice. Enter choice:1 to push a value on the stack2 to pop a value off the stack3 to end program? 3End of run.
Outline
Program Output
Wednesday, April 12, 2023 14
Tower of Hanoi: Application of stack
History from Kashi Vishwanath temple which contains a large room with three time-worn posts in it surrounded by 64 golden disks. Brahmin priests, acting out the command of an ancient prophecy, have been moving these disks, in accordance with the immutable rules of the Brahma, since that time. The puzzle is therefore also known as the Tower of Brahma puzzle. According to the legend, when the last move of the puzzle will be completed, the world will end. If the legend were true, and if the priests were able to move disks at a rate of one per second, using the smallest number of moves, it would take them 264−1 seconds or roughly 585 billion years or 18,446,744,073,709,551,615 turns to finish, or about 45 times the life span of the sun.
Wednesday, April 12, 2023 15
Tower of Hanoi: Application of stack
Three pegs A, B, C are given. Peg A contains N disks with decreasing size. The objective is to move disks from A to C using B.
Step1
Step2
Step3
Tower(N, A, B, C)If N=1Tower(1, A, B, C) or A->CIf N>11. Tower(N-1, A, C, B)
2. Tower(1, A, B, C) or A->C
3. Tower(N-1, B, A, C)
Wednesday, April 12, 2023 16
STACKS - Excercise
Describe the output of the following series of stack operations
• Push(8)• Push(3)• Pop()• Push(2)• Push(5)• Pop()• Pop()• Push(9)• Push(1)
3
8
5
2
8
1
9
8
Wednesday, April 12, 2023 17
QUEUESA queue is a non-primitive linear data structure (FIFO). It is an ordered collections of items where insertion takes place at one end called rear and deletion takes place at other end called front.
Queue Operations:Enqueue() Dequeue() Front() Size() IsEmpty()
Queue Applications:Direct applications
• Waiting lines• Access to shared resources (e.g., printer)• Multiprogramming
Indirect applications• Auxiliary data structure for algorithms• Component of other data structures
Wednesday, April 12, 2023 18
1 /* queue.c2 Operating and maintaining a queue */34 #include <stdio.h>5 #include <stdlib.h>67 struct queueNode { /* self-referential structure */8 char data;9 struct queueNode *nextPtr;10 };1112 typedef struct queueNode QueueNode;13 typedef QueueNode *QueueNodePtr;1415 /* function prototypes */16 void printQueue( QueueNodePtr );17 int isEmpty( QueueNodePtr );18 char dequeue( QueueNodePtr *, QueueNodePtr * );19 void enqueue( QueueNodePtr *, QueueNodePtr *, char );20 void instructions( void );2122 int main()23 { 24 QueueNodePtr headPtr = NULL, tailPtr = NULL;25 int choice;26 char item;2728 instructions();29 printf( "? " );30 scanf( "%d", &choice );
Outline1. Define struct
1.1 Function prototypes
1.2 Initialize variables
2. Input choice
Wednesday, April 12, 2023 19
3132 while ( choice != 3 ) { 3334 switch( choice ) { 3536 case 1:37 printf( "Enter a character: " );38 scanf( "\n%c", &item );39 enqueue( &headPtr, &tailPtr, item );40 printQueue( headPtr );41 break;42 case 2:43 if ( !isEmpty( headPtr ) ) { 44 item = dequeue( &headPtr, &tailPtr );45 printf( "%c has been dequeued.\n", item );46 }4748 printQueue( headPtr );49 break;5051 default:52 printf( "Invalid choice.\n\n" );53 instructions();54 break;55 }5657 printf( "? " );58 scanf( "%d", &choice );59 }6061 printf( "End of run.\n" );62 return 0;63 }64
Outline2.1 Switch statement
Wednesday, April 12, 2023 20
65 void instructions( void )66 { 67 printf ( "Enter your choice:\n"68 " 1 to add an item to the queue\n"69 " 2 to remove an item from the queue\n"70 " 3 to end\n" );71 }7273 void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr, 74 char value )75 { 76 QueueNodePtr newPtr;7778 newPtr = malloc( sizeof( QueueNode ) );7980 if ( newPtr != NULL ) { 81 newPtr->data = value;82 newPtr->nextPtr = NULL;8384 if ( isEmpty( *headPtr ) )85 *headPtr = newPtr;86 else87 ( *tailPtr )->nextPtr = newPtr;8889 *tailPtr = newPtr;90 }91 else92 printf( "%c not inserted. No memory available.\n",93 value );94 }95
Outline3 function definitions
Wednesday, April 12, 2023 21
96 char dequeue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr )97 {
98 char value;
99 QueueNodePtr tempPtr;
100
101 value = ( *headPtr )->data;
102 tempPtr = *headPtr;
103 *headPtr = ( *headPtr )->nextPtr;
104
105 if ( *headPtr == NULL )
106 *tailPtr = NULL;
107
108 free( tempPtr );
109 return value;
110 }
111
112 int isEmpty( QueueNodePtr headPtr )
113 {
114 return headPtr == NULL;
115 }
116
117 void printQueue( QueueNodePtr currentPtr )
118 {
119 if ( currentPtr == NULL )
120 printf( "Queue is empty.\n\n" );
121 else {
122 printf( "The queue is:\n" );
Outline3 function definitions
Wednesday, April 12, 2023 22
123124 while ( currentPtr != NULL ) { 125 printf( "%c --> ", currentPtr->data );126 currentPtr = currentPtr->nextPtr;127 }128129 printf( "NULL\n\n" );130 }131 }
Enter your choice: 1 to add an item to the queue 2 to remove an item from the queue 3 to end? 1Enter a character: AThe queue is:A --> NULL ? 1Enter a character: BThe queue is:A --> B --> NULL ? 1Enter a character: CThe queue is:A --> B --> C --> NULL
Outline3 function definitions
Output
Wednesday, April 12, 2023 23
? 2A has been dequeued.The queue is:B --> C --> NULL ? 2B has been dequeued.The queue is:C --> NULL ? 2C has been dequeued.Queue is empty. ? 2Queue is empty. ? 4Invalid choice. Enter your choice: 1 to add an item to the queue 2 to remove an item from the queue 3 to end? 3End of run.
Outline
Output
Wednesday, April 12, 2023 24
Types of Queues
1. Circular QueueElements are represented in circular fashion.Insertion is done at very first location if last location is full.Rear=(rear + 1) % Size Front=(Front +1) % Size
2. Double Ended Queue (deque)Elements can be inserted or deleted from both ends.Input restricted deque-insertion at only one endOutput restricted deque-deletion at only one end
3. Priority QueueEach element is assigned with a priorityAn element with higher priority is processed firstTwo elements with same priority are processed in FIFO order
Wednesday, April 12, 2023 2504/12/2023 25
QUEUES - Excercise
Describe the output of the following series of queue operations
• enqueue(8)• enqueue(3)• dequeue()• enqueue(2)• enqueue(5)• dequeue()• dequeue()• enqueue(9)• enqueue(1)
8 3
3 2 5
5 9 1
Wednesday, April 12, 2023 26
The Trees
A tree is a hierarchical representation of a finite set of one or more data items such that:
• There is a special node called the root of the tree.• Data items are partitioned into mutually exclusive subsets each of which
is itself a sub tree.
Trees
2-way tree (binary tree) m-way tree
Binary Search Balanced Binary Expression Height Balanced Weight Balanced
Height Balanced Weight Balanced
Wednesday, April 12, 2023 27
Trees Data Structures
• Tree– Nodes– Each node can have 0 or more children– A node can have at most one parent
• Binary tree– Tree with 0–2 children per node
Tree Binary Tree
Wednesday, April 12, 2023 28
Trees
• Terminology– Root no parent– Leaf no child– Interior non-leaf– Height distance from root to leaf
Root node
Leaf nodes
Interior nodes Height
Wednesday, April 12, 2023 29
The degree (shown in green color)of a tree is the maximum degree of node in a tree.
Depth (shown in red color) of a tree is the maximum level of any node in a tree.
K L
E F
B
G
C
M
H I J
D
A
Level
1
2
3
4
3
2 1 3
2 0 0 1 0 0
0 0 0
1
2 2 2
3 3 3 3 3 3
4 4 4
Wednesday, April 12, 2023 30
Binary TreesA binary tree is a tree in which no node can have more than two child nodes (left and right child).
2-tree or extended binary tree: Each node has either no children or two children. It is used to represent and compute any algebraic expression using binary operation.
e.g. E = (a + b) / ((c - d) * e)/
+ *
a b - e
c d
Fig. - expression tree
Wednesday, April 12, 2023 31
Samples of Trees
A
B
A
B
A
B C
GE
I
D
H
F
Complete Binary Tree
Skewed Binary Tree
E
C
D
1
2
3
45
Wednesday, April 12, 2023 32
Tree Traversal
1. Preorder Traversal (Root -> Left -> Right)2. In order Traversal (Left -> Root -> Right)3. Post order traversal (Left -> Right -> Root)
In order traversal – prints the node values in ascending order• Traverse the left sub tree with an in order traversal• Process the value in the node (i.e., print the node value)• Traverse the right sub tree with an in order traversal
Preorder traversal• Process the value in the node• Traverse the left sub tree with a preorder traversal• Traverse the right sub tree with a preorder traversalPost order traversal• Traverse the left sub tree with a post order traversal• Traverse the right sub tree with a post order traversal• Process the value in the node
Wednesday, April 12, 2023 33
preorder: A B C D E F G H Iinorder: B C A E D G H F I
A
B, C D, E, F, G, H, I
A
D, E, F, G, H, IB
C
A
B
C
D
E F
G I
H
Wednesday, April 12, 2023 34
1 /* tree.c2 Create a binary tree and traverse it 3 preorder, inorder, and postorder */4 #include <stdio.h>5 #include <stdlib.h>6 #include <time.h>78 struct treeNode { 9 struct treeNode *leftPtr;10 int data;11 struct treeNode *rightPtr;12 };1314 typedef struct treeNode TreeNode;15 typedef TreeNode *TreeNodePtr;1617 void insertNode( TreeNodePtr *, int );18 void inOrder( TreeNodePtr );19 void preOrder( TreeNodePtr );20 void postOrder( TreeNodePtr );2122 int main()23 { 24 int i, item;25 TreeNodePtr rootPtr = NULL;2627 srand( time( NULL ) );28
Outline1. Define struct
1.1 Function prototypes
1.2 Initialize variables
Wednesday, April 12, 2023 35
Outline
1.3 Insert random elements
2. Function calls
3. Function definitions
29 /* insert random values between 1 and 15 in the tree */30 printf( "The numbers being placed in the tree are:\n" );3132 for ( i = 1; i <= 10; i++ ) { 33 item = rand() % 15;34 printf( "%3d", item );35 insertNode( &rootPtr, item );36 }3738 /* traverse the tree preOrder */39 printf( "\n\nThe preOrder traversal is:\n" );40 preOrder( rootPtr );4142 /* traverse the tree inOrder */43 printf( "\n\nThe inOrder traversal is:\n" );44 inOrder( rootPtr );4546 /* traverse the tree postOrder */47 printf( "\n\nThe postOrder traversal is:\n" );48 postOrder( rootPtr );4950 return 0;51 }5253 void insertNode( TreeNodePtr *treePtr, int value )54 { 55 if ( *treePtr == NULL ) { /* *treePtr is NULL */56 *treePtr = malloc( sizeof( TreeNode ) );5758 if ( *treePtr != NULL ) { 59 ( *treePtr )->data = value;60 ( *treePtr )->leftPtr = NULL;61 ( *treePtr )->rightPtr = NULL;62 }
Wednesday, April 12, 2023 36
Outline
3. Function definitions
63 else64 printf( "%d not inserted. No memory available.\n", 65 value );66 }67 else68 if ( value < ( *treePtr )->data )69 insertNode( &( ( *treePtr )->leftPtr ), value );70 else if ( value > ( *treePtr )->data )71 insertNode( &( ( *treePtr )->rightPtr ), value );72 else 73 printf( "dup" );74 }7576 void inOrder( TreeNodePtr treePtr )77 { 78 if ( treePtr != NULL ) { 79 inOrder( treePtr->leftPtr );80 printf( "%3d", treePtr->data );81 inOrder( treePtr->rightPtr );82 }83 }8485 void preOrder( TreeNodePtr treePtr )86 { 87 if ( treePtr != NULL ) { 88 printf( "%3d", treePtr->data );89 preOrder( treePtr->leftPtr );90 preOrder( treePtr->rightPtr );91 }92 }
Wednesday, April 12, 2023 37
Outline3. Function
definitions
Program Output
9394 void postOrder( TreeNodePtr treePtr )95 { 96 if ( treePtr != NULL ) { 97 postOrder( treePtr->leftPtr );98 postOrder( treePtr->rightPtr );99 printf( "%3d", treePtr->data );100 }101 }
The numbers being placed in the tree are: 7 8 0 6 14 1 0dup 13 0dup 7dup The preOrder traversal is: 7 0 6 1 8 14 13 The inOrder traversal is: 0 1 6 7 8 13 14 The postOrder traversal is: 1 6 0 13 14 8 7
Wednesday, April 12, 2023 38
Binary Search Trees
• Key property– Value at node
• Smaller values in left subtree• Larger values in right subtree
– Example• X > Y• X < Z
Y
X
Z
Wednesday, April 12, 2023 39
Binary Search Trees
Binary search tree • Values in left sub tree less than parent • Values in right sub tree greater than parent• Applications: Facilitates duplicate elimination,
generating Huffman codes, expression tree• Fast searches - for a balanced tree, maximum of log n
comparisons 47
25 77
11 43 65 93
68 7 17 31 44
Wednesday, April 12, 2023 40
Building expression tree: An Application
a b c * +Step 1: Push a, b and c sub trees into stack.Step 2: When operator * is encountered, pop top two sub
trees from stack and build tree as:*
b c Step 3: Push the above sub tree onto stack.Step 4: When operator + is encountered, pop top two sub
trees from stack and build the tree as:+
a *b c
Wednesday, April 12, 2023 41
Binary Search Trees
• Examples
Binary search trees
Not a binary search tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
Wednesday, April 12, 2023 42
Binary Tree Implementation
Class Node {int data; // Could be int, a class, etcNode *left, *right; // null if empty
void insert ( int data ) { … }void delete ( int data ) { … }Node *find ( int data ) { … }
…}
Wednesday, April 12, 2023 43
Iterative Search of Binary Tree
Node *Find( Node *n, int key) { while (n != NULL) {
if (n->data == key) // Found it return n;if (n->data > key) // In left subtree n = n->left;else // In right subtree n = n->right;
} return null;
}Node * n = Find( root, 5);
Wednesday, April 12, 2023 44
Recursive Search of Binary Tree
Node *Find( Node *n, int key) {if (n == NULL) // Not found
return( n );else if (n->data == key) // Found it
return( n );else if (n->data > key) // In left subtree
return Find( n->left, key );else // In right subtree
return Find( n->right, key );}Node * n = Find( root, 5);
Wednesday, April 12, 2023 45
Example Binary Searches
• Find ( root, 2 )
5
10
30
2 25 45
5
10
30
2
25
45
10 > 2, left
5 > 2, left
2 = 2, found
5 > 2, left
2 = 2, found
root
Wednesday, April 12, 2023 46
Example Binary Searches
• Find (root, 25 )
5
10
30
2 25 45
5
10
30
2
25
45
10 < 25, right
30 > 25, left
25 = 25, found
5 < 25, right
45 > 25, left
30 > 25, left
10 < 25, right
25 = 25, found
Wednesday, April 12, 2023 47
Binary Search Tree – Insertion
• Algorithm1. Perform search for value X2. Search will end at node Y (if X not in tree)3. If X < Y, insert new leaf X as new left subtree for Y4. If X > Y, insert new leaf X as new right subtree for
Y• Observations
– O( log(n) ) operation for balanced tree– Insertions may unbalance tree
Wednesday, April 12, 2023 48
Example Insertion
• Insert ( 20 )
5
10
30
2 25 45
10 < 20, right
30 > 20, left
25 > 20, left
Insert 20 on left
20
Wednesday, April 12, 2023 49
Insert Node in Binary Search Tree
40
30
5
2
30
5 40
2 35 80
Insert 80 Insert 35
30
5 40
2 80
Wednesday, April 12, 2023 50
Binary Search Tree – Deletion
• Algorithm 1. Perform search for value X2. If X is a leaf, delete X3. Else // must delete internal node
a) Replace with largest value Y on left subtree OR smallest value Z on right subtreeb) Delete replacement value (Y or Z) from subtree
Observation– O( log(n) ) operation for balanced tree– Deletions may unbalance tree
Wednesday, April 12, 2023 51
Example Deletion (Leaf)
• Delete ( 25 )
5
10
30
2 25 45
10 < 25, right
30 > 25, left
25 = 25, delete
5
10
30
2 45
Wednesday, April 12, 2023 52
Example Deletion (Internal Node)
• Delete ( 10 )
5
10
30
2 25 45
5
5
30
2 25 45
2
5
30
2 25 45
Replacing 10 with largest value in left
subtree
Replacing 5 with largest value in left
subtree
Deleting leaf
Wednesday, April 12, 2023 53
Example Deletion (Internal Node)
• Delete ( 10 )
5
10
30
2 25 45
5
25
30
2 25 45
5
25
30
2 45
Replacing 10 with smallest value in right
subtree
Deleting leaf Resulting tree
Wednesday, April 12, 2023 54
Deletion for A Binary Search Tree
40
20 60
10 30 50 70
45 55
52
40
20 55
10 30 50 70
45 52
Before deleting 60 After deleting 60
non-leafnode
Wednesday, April 12, 2023 55
Binary Search Properties
• Time of search– Proportional to height of tree– Balanced binary tree
• O( log(n) ) time
– Degenerate tree• O( n ) time• Like searching linked list / unsorted array
Wednesday, April 12, 2023 56
AVL tree
AVL trees are height-balanced binary search treesBalance factor of a node=
height(left sub tree) - height(right sub tree)An AVL tree has balance factor calculated at every nodeFor every node, heights of left and right sub tree can differ by no more than 1Store current heights in each node
AVL property violated here
Wednesday, April 12, 2023 57
Rotations
• When the tree structure changes (e.g., insertion or deletion), we need to transform the tree to restore the AVL tree property.
• This is done using single rotations or double rotations.
x
y
AB
C
y
x
AB C
Before Rotation After Rotation
e.g. Single Rotation
Wednesday, April 12, 2023 58
Rotations
• Since an insertion/deletion involves adding/deleting a single node, this can only increase/decrease the height of some subtree by 1
• Thus, if the AVL tree property is violated at a node x, it means that the heights of left(x) and right(x) differ by exactly 2.
• Rotations will be applied to x to restore the AVL tree property.
Wednesday, April 12, 2023 59
Single rotation
The new key is inserted in the subtree A. The AVL-property is violated at x height of left(x) is h+2 height of right(x) is h.
Wednesday, April 12, 2023 60
Single rotation
Single rotation takes O(1) time.Insertion takes O(log N) time.
The new key is inserted in the subtree C. The AVL-property is violated at x.
Wednesday, April 12, 2023 61
5
3
1 4
Insert 0.8
AVL Tree
8
0.8
5
3
1 4
8
x
y
A
B
C
3
51
0.84 8
After rotation
Wednesday, April 12, 2023 62
Double rotation
The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x.x-y-z forms a zig-zag shape
also called left-right rotate
Wednesday, April 12, 2023 63
Double rotation
The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x.
also called right-left rotate
Wednesday, April 12, 2023 64
5
3
1 4
Insert 3.5
AVL Tree
8
3.5
5
3
1 4
8
4
5
1
3
3.5 After Rotation
x
y
A z
B
C
8
Wednesday, April 12, 2023 65
An Extended Example
Insert 3,2,1,4,5,6,7, 16,15,14
3
Fig 1
3
2
Fig 2
3
2
1
Fig 3
2
1 3Fig 4
2
1 3
4Fig 5
2
1 3
4
5
Fig 6
Single rotation
Single rotation
Wednesday, April 12, 2023 66
Insert 3,2,1,4,5,6,7, 16,15,14
2
1 4
53
Fig 7 6
2
1 4
53
Fig 8
4
2 5
61 3
Fig 9
4
2 5
61 3
7Fig 10
4
2 6
71 3
5 Fig 11
Single rotation
Single rotation
Wednesday, April 12, 2023 67
4
2 6
71 3
5 16
Fig 12
4
2 6
71 3
5 16
15Fig 13
4
2 6
151 3 5
167Fig 14
Double rotation
Insert 3,2,1,4,5,6,7, 16,15,14
Wednesday, April 12, 2023 68
5
4
2 7
151 3 6
1614
Fig 16
4
2 6
151 3 5
167
14
Fig 15
Double rotation
Insert 3,2,1,4,5,6,7, 16,15,14
Wednesday, April 12, 2023 69
j
k
X Y
Z
Consider a validAVL subtree
AVL Insertion: Outside Case
h
hh
Wednesday, April 12, 2023 70
j
k
XY
Z
Inserting into Xdestroys the AVL property at node j
AVL Insertion: Outside Case
h
h+1 h
Wednesday, April 12, 2023 71
j
k
XY
Z
Do a “right rotation”
AVL Insertion: Outside Case
h
h+1 h
Wednesday, April 12, 2023 72
j
k
XY
Z
Do a “right rotation”
Single right rotation
h
h+1 h
Wednesday, April 12, 2023 73
j
k
X Y Z
“Right rotation” done!(“Left rotation” is mirror symmetric)
Outside Case Completed
AVL property has been restored!
h
h+1
h
Wednesday, April 12, 2023 74
j
k
X Y
Z
AVL Insertion: Inside Case
Consider a validAVL subtree
h
hh
Wednesday, April 12, 2023 75
Inserting into Y destroys theAVL propertyat node j
j
k
XY
Z
AVL Insertion: Inside Case
Does “right rotation”restore balance?
h
h+1h
Wednesday, April 12, 2023 76
jk
X
YZ
“Right rotation”does not restorebalance… now k isout of balance
AVL Insertion: Inside Case
hh+1
h
Wednesday, April 12, 2023 77
Consider the structureof subtree Y… j
k
XY
Z
AVL Insertion: Inside Case
h
h+1h
Wednesday, April 12, 2023 78
j
k
X
V
Z
W
i
Y = node i andsubtrees V and W
AVL Insertion: Inside Case
h
h+1h
h or h-1
Wednesday, April 12, 2023 79
j
k
X
V
Z
W
i
AVL Insertion: Inside Case
We will do a left-right “double rotation” . . .
Wednesday, April 12, 2023 80
j
k
X V
ZW
i
Double rotation : first rotation
left rotation complete
Wednesday, April 12, 2023 81
j
k
X V
ZW
i
Double rotation : second rotation
Now do a right rotation
Wednesday, April 12, 2023 82
jk
X V ZW
i
Double rotation : second rotation
right rotation complete
Balance has been restored
hh h or h-1
Wednesday, April 12, 2023 83
Arguments for AVL trees:
1. Search is O(log N) since AVL trees are always balanced.2. Insertion and deletions are also O(logn)3. The height balancing adds no more than a constant factor to the speed of
insertion.
Arguments against using AVL trees:4. Difficult to program & debug; more space for balance factor.5. Asymptotically faster but rebalancing costs time.6. Most large searches are done in database systems on disk and use other
structures (e.g. B-trees).7. May be OK to have O(N) for a single operation if total run time for many
consecutive operations is fast (e.g. Splay trees).
Pros and Cons of AVL Trees
Wednesday, April 12, 2023 84
All leaves are on the bottom level. All internal nodes (except perhaps the root node) have at least ceil(m / 2) (nonempty) children. The root node can have as few as 2 children if it is an internal node, and can obviously have no children if the root node is a leaf (that is, the whole tree consists only of the root node). Each leaf node (other than the root node if it is a leaf) must contain at least ceil(m / 2) - 1 keys.
B-tree is a fairly well-balanced tree.
B-Tree
Wednesday, April 12, 2023 85
Let's work our way through an example similar to that given by Kruse. Insert the following letters into what is originally an empty B-tree of order 5:
C N G A H E K Q M F W L T Z D P R X Y S
Order 5 means that a node can have a maximum of 5 children and 4 keys. All nodes other than the root must have a minimum of 2 keys. The first 4 letters get inserted into the same node, resulting in this picture:
Wednesday, April 12, 2023 86
When we try to insert the H, we find no room in this node, so we split it into 2 nodes, moving the median item G up into a new root node. Note that in practice we just leave the A and C in the current node and place the H and N into a new node to the right of the old one.
Inserting E, K, and Q proceeds without requiring any splits:
H E K Q M F W L T Z D P R X Y S
Wednesday, April 12, 2023 87
Inserting M requires a split. Note that M happens to be the median key and so is moved up into the parent node.
The letters F, W, L, and T are then added without needing any split.
M F W L T Z D P R X Y S
Wednesday, April 12, 2023 88
When Z is added, the rightmost leaf must be split. The median item T is moved up into the parent node. Note that by moving up the median key, the tree is kept fairly balanced, with 2 keys in each of the resulting nodes.
The insertion of D causes the leftmost leaf to be split. D happens to be the median key and so is the one moved up into the parent node. The letters P, R, X, and Y are then added without any need of splitting:
Z D P R X Y S
Wednesday, April 12, 2023 89
Finally, when S is added, the node with N, P, Q, and R splits, sending the median Q up to the parent. However, the parent node is full, so it splits, sending the median M up to form a new root node. Note how the 3 pointers from the old parent node stay in the revised node that contains D and G.
S
HEAPA max tree is a tree in which the key value in each node is no smaller than the key values in its children. A max heap is a complete binary tree that is also a max tree.
A min tree is a tree in which the key value in each node is no larger than the key values in its children. A min heap is a complete binary tree that is also a min tree.
Operations on heaps:- creation of an empty heap- insertion of a new element into the heap; - deletion of the largest element from the heap
Wednesday, April 12, 2023 90
Wednesday, April 12, 2023 91
14
12 7
810 6
9
6 3
5
30
25
[1]
[2] [3]
[5] [6]
[1]
[2] [3]
[4]
[1]
[2]
2
7 4
810 6
10
20 83
50
11
21
[1]
[2] [3]
[5] [6]
[1]
[2] [3]
[4]
[1]
[2]
[4]
Max Heap
Min Heap
Wednesday, April 12, 2023 92
Example of Insertion to Max Heap
20
15 2
14 10
initial location of new node
21
15 20
14 10 2
insert 21 into heap
20
15 5
14 10 2
insert 5 into heap
Wednesday, April 12, 2023 93
Insertion into a Max Heap
void insert_max_heap(element item, int *n){ int i; if (HEAP_FULL(*n)) { fprintf(stderr, “the heap is full.\n”); exit(1); } i = ++(*n); while ((i!=1)&&(item.key>heap[i/2].key)) { heap[i] = heap[i/2]; i /= 2; } heap[i]= item;}
2k-1=n ==> k=log2(n+1)O(log2n)
Wednesday, April 12, 2023 94
Example of Deletion from Max Heap
20remove
15 2
14 10
10
15 2
14
15
14 2
10
Wednesday, April 12, 2023 95
Deletion from a Max Heap
element delete_max_heap(int *n){ int parent, child; element item, temp; if (HEAP_EMPTY(*n)) { fprintf(stderr, “The heap is empty\n”); exit(1); } /* save value of the element with the highest key */
item = heap[1]; /* use last element in heap to adjust heap */ temp = heap[(*n)--]; parent = 1; child = 2;
Wednesday, April 12, 2023 96
while (child <= *n) { /* find the larger child of the current parent */ if ((child < *n)&& (heap[child].key<heap[child+1].key)) child++; if (temp.key >= heap[child].key) break; /* move to the next lower level */ heap[parent] = heap[child]; child *= 2; } heap[parent] = temp; return item;}
Wednesday, April 12, 2023 97
ThankYou
Contact @Email: kuberchandra@yahoo.com