2. Data Structure is the structural representation of logical
relationships between elements of data.
Data Structure = Organized Data + Operations
Data 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 Algorithm
Time 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)
Tuesday, April 8, 2014 2
3. Tuesday, April 8, 2014 3
Data Structure
Primitive DS Non-primitive DS
Integer Float Character Pointer Arrays Lists Files
Linear List Non-linear List
Stacks Queues Graphs Trees
4. Tuesday, April 8, 2014 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.
5. Tuesday, April 8, 2014 5
LISTS
A 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
6. Tuesday, April 8, 2014 6
Types of linked lists
1. Singly linked list
Begins with a pointer to the first node
Terminates with a null pointer
Only 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 element
Each node has a forward pointer and a backward pointer
Allows 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
7. Tuesday, April 8, 2014 7
STACKS
A 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
9. Tuesday, April 8, 2014 9
29 while ( choice != 3 ) {
30
31 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 ) );
42
43 printStack( stackPtr );
44 break;
45 default:
46 printf( "Invalid choice.nn" );
47 instructions();
48 break;
49 }
50
51 printf( "? " );
52 scanf( "%d", &choice );
53 }
54
55 printf( "End of run.n" );
56 return 0;
57 }
58
Outline
2.1 switch statement
10. Tuesday, April 8, 2014 10
59 /* Print the instructions */
60 void instructions( void )
61 {
62 printf( "Enter choice:n"
63 "1 to push a value on the stackn"
64 "2 to pop a value off the stackn"
65 "3 to end programn" );
66 }
67
68 /* Insert a node at the stack top */
69 void push( StackNodePtr *topPtr, int info )
70 {
71 StackNodePtr newPtr;
72
73 newPtr = malloc( sizeof( StackNode ) );
74 if ( newPtr != NULL ) {
75 newPtr->data = info;
76 newPtr->nextPtr = *topPtr;
77 *topPtr = newPtr;
78 }
79 else
80 printf( "%d not inserted. No memory
available.n",81 info );
82 }
83
Outline
3. Function definitions
11. Tuesday, April 8, 2014 11
84 /* Remove a node from the stack top */
85 int pop( StackNodePtr *topPtr )
86 {
87 StackNodePtr tempPtr;
88 int popValue;
89
90 tempPtr = *topPtr;
91 popValue = ( *topPtr )->data;
92 *topPtr = ( *topPtr )->nextPtr;
93 free( tempPtr );
94 return popValue;
95 }
96
97 /* Print the stack */
98 void printStack( StackNodePtr currentPtr )
99 {
100 if ( currentPtr == NULL )
101 printf( "The stack is empty.nn" );
102 else {
103 printf( "The stack is:n" );
104
105 while ( currentPtr != NULL ) {
106 printf( "%d --> ", currentPtr->data );
107 currentPtr = currentPtr->nextPtr;
108 }
109
110 printf( "NULLnn" );
111 }
112 }
113
Outline
3. Function definitions
12. Tuesday, April 8, 2014 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 stack
2 to pop a value off the stack
3 to end program
? 1
Enter an integer: 5
The stack is:
5 --> NULL
? 1
Enter an integer: 6
The stack is:
6 --> 5 --> NULL
? 1
Enter an integer: 4
The stack is:
4 --> 6 --> 5 --> NULL
? 2
The popped value is 4.
The stack is:
6 --> 5 --> NULL
Outline
3. Function definitions
Program Output
13. Tuesday, April 8, 2014 13
? 2
The popped value is 6.
The stack is:
5 --> NULL
? 2
The popped value is 5.
The stack is empty.
? 2
The stack is empty.
? 4
Invalid choice.
Enter choice:
1 to push a value on the stack
2 to pop a value off the stack
3 to end program
? 3
End of run.
Outline
Program Output
14. Tuesday, April 8, 2014 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.
15. Tuesday, April 8, 2014 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.
Step
1
Step
2
Step
3
Tower(N, A, B, C)
If N=1
Tower(1, A, B, C) or A->C
If N>1
1. Tower(N-1, A, C, B)
2. Tower(1, A, B, C) or A->C
3. Tower(N-1, B, A, C)
16. Tuesday, April 8, 2014 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
17. Tuesday, April 8, 2014 17
QUEUES
A 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
22. Tuesday, April 8, 2014 22
123
124 while ( currentPtr != NULL ) {
125 printf( "%c --> ", currentPtr->data );
126 currentPtr = currentPtr->nextPtr;
127 }
128
129 printf( "NULLnn" );
130 }
131 }
Enter your choice:
1 to add an item to the queue
2 to remove an item from the queue
3 to end
? 1
Enter a character: A
The queue is:
A --> NULL
? 1
Enter a character: B
The queue is:
A --> B --> NULL
? 1
Enter a character: C
The queue is:
A --> B --> C --> NULL
Outline
3 function definitions
Output
23. Tuesday, April 8, 2014 23
? 2
A has been dequeued.
The queue is:
B --> C --> NULL
? 2
B has been dequeued.
The queue is:
C --> NULL
? 2
C has been dequeued.
Queue is empty.
? 2
Queue is empty.
? 4
Invalid choice.
Enter your choice:
1 to add an item to the queue
2 to remove an item from the queue
3 to end
? 3
End of run.
Outline
Output
24. Tuesday, April 8, 2014 24
Types of Queues
1. Circular Queue
Elements 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 end
Output restricted deque-deletion at only one end
3. Priority Queue
Each element is assigned with a priority
An element with higher priority is processed first
Two elements with same priority are processed in FIFO order
25. Tuesday, April 8, 2014 254/8/2014 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
26. Tuesday, April 8, 2014 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
27. Tuesday, April 8, 2014 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
28. Tuesday, April 8, 2014 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
29. Tuesday, April 8, 2014 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
30. Tuesday, April 8, 2014 30
Binary Trees
A 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
31. Tuesday, April 8, 2014 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
4
5
32. Tuesday, April 8, 2014 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 traversal
Post 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
33. Tuesday, April 8, 2014 33
preorder: A B C D E F G H I
inorder: 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
34. Tuesday, April 8, 2014 34
1 /* tree.c
2 Create a binary tree and traverse it
3 preorder, inorder, and postorder */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <time.h>
7
8 struct treeNode {
9 struct treeNode *leftPtr;
10 int data;
11 struct treeNode *rightPtr;
12 };
13
14 typedef struct treeNode TreeNode;
15 typedef TreeNode *TreeNodePtr;
16
17 void insertNode( TreeNodePtr *, int );
18 void inOrder( TreeNodePtr );
19 void preOrder( TreeNodePtr );
20 void postOrder( TreeNodePtr );
21
22 int main()
23 {
24 int i, item;
25 TreeNodePtr rootPtr = NULL;
26
27 srand( time( NULL ) );
28
Outline
1. Define struct
1.1 Function prototypes
1.2 Initialize variables
35. Tuesday, April 8, 2014 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" );
31
32 for ( i = 1; i <= 10; i++ ) {
33 item = rand() % 15;
34 printf( "%3d", item );
35 insertNode( &rootPtr, item );
36 }
37
38 /* traverse the tree preOrder */
39 printf( "nnThe preOrder traversal is:n" );
40 preOrder( rootPtr );
41
42 /* traverse the tree inOrder */
43 printf( "nnThe inOrder traversal is:n" );
44 inOrder( rootPtr );
45
46 /* traverse the tree postOrder */
47 printf( "nnThe postOrder traversal is:n" );
48 postOrder( rootPtr );
49
50 return 0;
51 }
52
53 void insertNode( TreeNodePtr *treePtr, int value )
54 {
55 if ( *treePtr == NULL ) { /* *treePtr is NULL */
56 *treePtr = malloc( sizeof( TreeNode ) );
57
58 if ( *treePtr != NULL ) {
59 ( *treePtr )->data = value;
60 ( *treePtr )->leftPtr = NULL;
61 ( *treePtr )->rightPtr = NULL;
62 }
37. Tuesday, April 8, 2014 37
Outline
3. Function
definitions
Program Output
93
94 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
38. Tuesday, April 8, 2014 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
39. Tuesday, April 8, 2014 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
687 17 31 44
40. Tuesday, April 8, 2014 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
41. Tuesday, April 8, 2014 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
42. Tuesday, April 8, 2014 42
Binary Tree Implementation
Class Node {
int data; // Could be int, a class, etc
Node *left, *right; // null if empty
void insert ( int data ) { … }
void delete ( int data ) { … }
Node *find ( int data ) { … }
…
}
43. Tuesday, April 8, 2014 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);
44. Tuesday, April 8, 2014 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);
45. Tuesday, April 8, 2014 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
46. Tuesday, April 8, 2014 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
47. Tuesday, April 8, 2014 47
Binary Search Tree – Insertion
• Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left subtree for Y
4. 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
48. Tuesday, April 8, 2014 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
50. Tuesday, April 8, 2014 50
Binary Search Tree – Deletion
• Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from subtree
Observation
– O( log(n) ) operation for balanced tree
– Deletions may unbalance tree
51. Tuesday, April 8, 2014 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
52. Tuesday, April 8, 2014 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
53. Tuesday, April 8, 2014 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
54. Tuesday, April 8, 2014 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-leaf
node
55. Tuesday, April 8, 2014 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
56. Tuesday, April 8, 2014 56
AVL tree
AVL trees are height-balanced binary search trees
Balance factor of a node=
height(left sub tree) - height(right sub tree)
An AVL tree has balance factor calculated at every node
For every node, heights of left and right sub tree can
differ by no more than 1
Store current heights in each node
AVL property
violated here
57. Tuesday, April 8, 2014 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
A
B
C
y
x
A
B C
Before Rotation After Rotation
e.g. Single Rotation
58. Tuesday, April 8, 2014 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.
59. Tuesday, April 8, 2014 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.
60. Tuesday, April 8, 2014 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.
61. Tuesday, April 8, 2014 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.8
4 8
After rotation
62. Tuesday, April 8, 2014 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
63. Tuesday, April 8, 2014 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
64. Tuesday, April 8, 2014 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
65. Tuesday, April 8, 2014 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
3
Fig 4
2
1
3
4
Fig 5
2
1
3
4
5
Fig 6
Single rotation
Single rotation
69. Tuesday, April 8, 2014 69
j
k
X Y
Z
Consider a valid
AVL subtree
AVL Insertion: Outside Case
h
h
h
70. Tuesday, April 8, 2014 70
j
k
X
Y
Z
Inserting into X
destroys the AVL
property at node j
AVL Insertion: Outside Case
h
h+1 h
71. Tuesday, April 8, 2014 71
j
k
X
Y
Z
Do a “right rotation”
AVL Insertion: Outside Case
h
h+1 h
72. Tuesday, April 8, 2014 72
j
k
X
Y
Z
Do a “right rotation”
Single right rotation
h
h+1 h
73. Tuesday, April 8, 2014 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
74. Tuesday, April 8, 2014 74
j
k
X Y
Z
AVL Insertion: Inside Case
Consider a valid
AVL subtree
h
hh
75. Tuesday, April 8, 2014 75
Inserting into Y
destroys the
AVL property
at node j
j
k
X
Y
Z
AVL Insertion: Inside Case
Does “right rotation”
restore balance?
h
h+1h
76. Tuesday, April 8, 2014 76
j
k
X
Y
Z
“Right rotation”
does not restore
balance… now k is
out of balance
AVL Insertion: Inside Case
h
h+1
h
77. Tuesday, April 8, 2014 77
Consider the structure
of subtree Y… j
k
X
Y
Z
AVL Insertion: Inside Case
h
h+1h
78. Tuesday, April 8, 2014 78
j
k
X
V
Z
W
i
Y = node i and
subtrees V and W
AVL Insertion: Inside Case
h
h+1h
h or h-1
79. Tuesday, April 8, 2014 79
j
k
X
V
Z
W
i
AVL Insertion: Inside Case
We will do a left-right
“double rotation” . . .
80. Tuesday, April 8, 2014 80
j
k
X V
Z
W
i
Double rotation : first rotation
left rotation complete
81. Tuesday, April 8, 2014 81
j
k
X V
Z
W
i
Double rotation : second rotation
Now do a right rotation
82. Tuesday, April 8, 2014 82
jk
X V ZW
i
Double rotation : second rotation
right rotation complete
Balance has been
restored
hh h or h-1
83. Tuesday, April 8, 2014 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:
1. Difficult to program & debug; more space for balance factor.
2. Asymptotically faster but rebalancing costs time.
3. Most large searches are done in database systems on disk and use other
structures (e.g. B-trees).
4. 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
84. Tuesday, April 8, 2014 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
85. Tuesday, April 8, 2014 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:
86. Tuesday, April 8, 2014 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
87. Tuesday, April 8, 2014 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
88. Tuesday, April 8, 2014 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
89. Tuesday, April 8, 2014 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
90. HEAP
A 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
Tuesday, April 8, 2014 90
92. Tuesday, April 8, 2014 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
93. Tuesday, April 8, 2014 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)
94. Tuesday, April 8, 2014 94
Example of Deletion from Max Heap
20
remove
15 2
14 10
10
15 2
14
15
14 2
10
95. Tuesday, April 8, 2014 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 emptyn”);
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;
96. Tuesday, April 8, 2014 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;
}
97. Tuesday, April 8, 2014 97
Thank
You
Contact @
Email: kuberchandra@yahoo.com