Download presentation
Presentation is loading. Please wait.
Published byRalph Turner Modified over 9 years ago
1
Definition 2 A Dynamic Dictionary is a data structure of item with keys that support the following basic operations: (1) (1)Insert a new item (2) (2)Remove an item with a given key (3) Search an item with a given key What data structure is the best? Select a data structure for supporting Dynamic Dictionary
2
Array O(log n)Search O(n)Remove O(n)Insert ordered array Data structure Operation
3
Linked lists Head(L) 91641 Doubly-linked list x x->key (Key(x)) x->next (next(x)) x->prev (prev(x))
4
O(n)O(log n)Search O(n) Remove O(n) Insert ordered linked list ordered array Data structure Operation Ordered doubly linked lists
5
Binary Search Trees (BSTs) Binary search tree is the binary tree satisfying the following conditions: (i) (i)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. (ii) (ii)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 2 6 8 7 5 Node in a BST struct node {node *left, *right, *parent, int key; };
6
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 15 618 3 4 7 13 1 2017 9
7
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. 12 5 18 915 2 19 1317 Insert time : height of the tree
8
Remove Delete node z when z is given. 15 5 16 3 12 1013 6 7 20 1823 15 5 16 3 12 1013 6 7 20 1823 15 5 16 3 12 1013 6 7 20 1823 Remove time : height of the tree y y y x x x=NIL
9
Comparison of ordered arrays, ordered linked lists and binary search trees Height of BSTs: worst case O(n) , average O(log n) O(n)O(log n)Search O(n) Remove O(n) Insert ordered linked list ordered array Data structure Operation Binary search tree height of the tee height of the tree
10
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 which is an English string not longer than 20 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.
11
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 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;}
12
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 { 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 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() 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);} }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.