Dictionaries - Data Structures and Algorithm - Lecture Slides, Slides of Data Structures and Algorithms

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

2012/2013

Uploaded on 04/27/2013

shareeka_555
shareeka_555 🇮🇳

4

(6)

74 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Dictionaries
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Dictionaries - Data Structures and Algorithm - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Dictionaries

Dictionary / Dynamic Set Operations

Perhaps the most important class of data structures maintain a set of items, indexed by keys.

  • Search(S,k) – A query that, given a set S and a key value k, returns a pointer x to an element in S such that key[x] = k, or nil if no such element belongs to S.
  • Insert(S,x) – A modifying operation that augments the set S with the element x.
  • Delete(S,x) – Given a pointer x to an element in the set S, remove x from S. Observe we are given a pointer to an element x, not a key value.

Problem of the Day

What is the asymptotic worst-case running times for each of the seven fundamental dictionary operations when the data structure is implemented as

  • A singly-linked unsorted list,
  • A doubly-linked unsorted list,
  • A singly-linked sorted list, and finally
  • A doubly-linked sorted list.

Solution Blank

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

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

Binary Search Trees

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.

Searching in a Binary Tree: Implementation

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) ); }

Searching in a Binary Tree: How Much

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).

Finding the Minimum

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).

Where is the Predecessor: Internal Node

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.

In-Order Traversal

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

Tree Insertion

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).

Tree Deletion

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!

Cases of Deletion

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