

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Some concept of Data Structures and Algorithm are Permutation, Representation, Implemented, Algorithm Design, Dynamic Programming, Graph Data Structures, String Processing, General Trees. Main points of this lecture are: Dictionaries, Important, Class, Data Structures, Maintain, Indexed, Dynamic, Set Operations, Dictionary, Pointer
Typology: Slides
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Perhaps the most important class of data structures maintain a set of items, indexed by keys.
What is the asymptotic worst-case running times for each of the seven fundamental dictionary operations when the data structure is implemented as
singly singly doubly doubly unsorted sorted unsorted sorted Search(L, k) Insert(L, x) Delete(L, x) Successor(L, x) Predecessor(L, x) Minimum(L) Maximum(L)
Binary search trees provide a data structure which efficiently supports all six dictionary operations. A binary tree is a rooted tree where each node contains at most two children. Each child can be identified as either a left or right child.
parent
left right
A binary search tree labels each node x in a binary tree such that all nodes in the left subtree of x have keys < x and all nodes in the right subtree of x have key’s > x.
2 3 7 6 8 5
The search tree labeling enables us to find where any key is.
tree (^) *search tree(tree (^) *l, item type x) { if (l == NULL) return(NULL); if (l->item == x) return(l); if (x < l->item) return( search tree(l->left, x) ); else return( search tree(l->right, x) ); }
The algorithm works because both the left and right subtrees of a binary search tree are binary search trees – recursive structure, recursive algorithm. This takes time proportional to the height of the tree, O(h).
tree (^) *find minimum(tree (^) *t) { tree (^) min; ( pointer to minimum (^) *) if (t == NULL) return(NULL); min = t; while (min->left != NULL) min = min->left; return(min); }
Finding the max or min takes time proportional to the height of the tree, O(h).
X
PREDECESSOR(X) SUCCESSOR(X)
If X has two children, its predecessor is the maximum value in its left subtree and its successor the minimum value in its right subtree.
void traverse tree(tree (^) *l) { if (l != NULL) { traverse tree(l->left); process item(l->item); traverse tree(l->right); } }
H A F B G D C E
Do a binary search to find where it should be, then replace the termination NIL pointer with the new item.
3 7 6 8 5
1
2
Insertion takes time proportional to the height of the tree, O(h).
Deletion is trickier than insertion, because the node to die may not be a leaf, and thus effect other nodes. There are three cases: Case (a), where the node is a leaf, is simple - just NIL out the parents child pointer. Case (b), where a node has one chld, the doomed node can just be cut out. Case (c), relabel the node as its successor (which has at most one child when z has two children!) and delete the successor!
initial tree delete node with zero children (3)
5 5
2
6
8
7
3
1
2
8
7 4 3
1
2
5
6
8
7 4
1
delete node with 1 child (6) delete node with 2 children (4)
6
8
7 4 3
1
5
2