Definition 2 - PowerPoint PPT Presentation

About This Presentation
Title:

Definition 2

Description:

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 ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 13
Provided by: tns95
Learn more at: https://www.tnstate.edu
Category:

less

Transcript and Presenter's Notes

Title: Definition 2


1
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

2
Array
3
Linked lists
4
Ordered doubly linked lists
5
Binary Search Trees (BSTs)
Node in a BST struct node node left, right,
parent, int key
6
Search
Inputpointer 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
7
Insert
Node z leftzNIL,rightzNIL, keyzv x
pointer At beginning, let x point to the root
and let y x.
Insert time height of the tree
8
Remove
Delete node z when z is given.
Remove time height of the tree
9
Comparison of ordered arrays, ordered linked
lists and binary search trees
Height of BSTs worst case O(n),average O(log n)
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
  • 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
ltstdlib.hgt includeltiostream.hgt 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
1000rand()/RAND_MAX info
1.0rand()/RAND_MAX int scan(istream is
cin) return (is gtgt keyval gtgt info) !
0 void show(ostream os cout)
os ltlt keyval ltlt ltlt info ltlt endl
ostream operatorltlt(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 ltclass Item, class Keygt
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 (h0) return
nullItem() Key th-gtitem.key()
if (vt) return h-gtitem
if (vltt) return searchR(h-gtl, v)
else return searchR(h-gtr, v)

void insertR(link h, Item x) if (h0) h
new node(x) return if (x.key() lt
h-gtitem.key()) insertR(h-gtl,x)
else insertR(h-gtr, x) public ST(int
maxN) heard 0) Item search(Key v)
return searchR(head, v) void insert(Item
x) insertR(heard, x)
Write a Comment
User Comments (0)
About PowerShow.com