120 likes | 321 Views
What data structure is the best?. Select a data structure for supporting Dynamic Dictionary. Definition 2 A Dynamic Dictionary is a data structure of item with keys that support the following basic operations: Insert a new item Remove an item with a given key
E N D
What data structure is the best? Select a data structure for supporting Dynamic Dictionary • Definition 2 • A Dynamic Dictionary is a data structure of item • with keys that support the following basic operations: • Insert a new item • Remove an item with a given key • (3) Search an item with a given key
Array Data structure Operation ordered array Insert O(n) Remove O(n) Search O(log n)
x->prev (prev(x)) x->key (Key(x)) x->next (next(x)) x Doubly-linked list Head(L) 9 16 4 1 Linked lists
Data structure Operation ordered array ordered linked list Insert O(n) O(n) Remove O(n) O(n) Search O(log n) O(n) Ordered doubly linked lists
Binary Search Trees (BSTs) • Binary search tree is the binary tree satisfying the following conditions: • Each node x contains key, information and three links pointing to its parent, left son and right son, respectively, which are denoted as key[x], inf[x], p[x], left[x], right[x], respectively. • For any node y in x’s left subtree, key[y] key[x], and • for any node y in x’s right subtree, key[x] key[y]. 5 3 6 2 5 8 7 Node in a BST struct node {node*left, *right, *parent, intkey; };
15 6 18 3 7 17 20 13 1 4 9 Search Input:pointer x the root and key k Output: the node whose key is k (it is Null if there is not any node whose key is k) Search time: height of the tree
12 5 18 2 9 15 19 13 17 Insert Node z: left[z]=NIL,right[z]=NIL, key[z]=v x: pointer At beginning, let x point to the root and let y = x. Insert time: height of the tree
15 y 5 16 x=NIL 3 12 20 y 10 13 18 23 15 x 5 16 6 3 12 20 10 13 18 23 6 7 7 15 5 16 3 12 20 y 10 13 18 23 x 6 7 Remove Delete node z when z is given. Remove time: height of the tree
Binary search tree height of the tee height of the tree height of the tree Data structure Operation ordered array ordered linked list Insert O(n) O(n) Remove O(n) O(n) Search O(log n) O(n) Comparison of ordered arrays, ordered linked lists and binary search trees Height of BSTs: worst case O(n),averageO(log n)
Project 1 Task I: Design a Binary Search Tree which support: (1) Search, (2) Insertion, (3) Deletion and (4) Transversal in in-order Task II: Add the following two functions: (1) finding the height of the tree, and finding the number of nodes in the tree. • Requirements • To simplify the problem, the data in each node contains only one key which is an integer between -10000 and 10000 and a name which is an English string not longer than 20. The key and name can be generated randomly. • Each of the algorithms for search, insert, transversal and finding the height and number of nodes has to use recursive algorithms. • Implement the algorithms using C or C++. • A simple user interface has to be contained. That is, user can select operations and read the results of the operations. Submission Project description, Algorithms, Algorithm analysis, Experiment output, Code.
Program Sample Implementation for item Key(access keys), null(test whether items are null), Scan(read an Item), rand(generate a random Item), show(print an Item) #include <stdlib.h> #include<iostream.h> static int maxKey = 1000; typedef int Key; class Item { private: Key keyval; float info; public: Item() { keyval = maxKey;} Key key() { return keyval;} int null() { return keyval == maxKey; } void rand() { keyval = 1000*rand()/RAND_MAX; info = 1.0*rand()/RAND_MAX;} int scan(istream& is = cin) {return (is >> keyval >> info) != 0;} void show(ostream& os = cout) { os << keyval << “ “ << info << endl; } }; ostream& operator<<(ostream& os, Item& x) { x.show(os); return os;}
Implement of BST-based symbol table Program 12.8 BST-based symbol table Basic operations search and insert are supported. The link head points to the root of the tree. Template <class Item, class Key> { private: struct node {Item item; node *l, *r; node(Item x) { item = x; l =0; r = 0; } }; typedef node *link; link head; Item nullItem; Item searchR(link h, Key v) { if (h==0) return nullItem(); Key t=h->item.key(); if (v==t) return h->item if (v<t) return searchR(h->l, v); else return searchR(h->r, v); } void insertR(link& h, Item x) { if (h==0) {h= new node(x); return;} if (x.key() < h->item.key()) insertR(h->l,x); else insertR(h->r, x); } public: ST(int maxN) {heard =0;) Item search(Key v) {return searchR(head, v);} void insert(Item x) { insertR(heard, x);} }