Dictionary ADT Todays announcements: MT1 tonight, 7-9:00p WOOD 2 - - PowerPoint PPT Presentation

dictionary adt
SMART_READER_LITE
LIVE PREVIEW

Dictionary ADT Todays announcements: MT1 tonight, 7-9:00p WOOD 2 - - PowerPoint PPT Presentation

Dictionary ADT Todays announcements: MT1 tonight, 7-9:00p WOOD 2 HW2 out, due Feb 5, 11:59p Todays Plan Binary Search Trees key value (data) Multics MULTiplexed Information and Computing Service Dictionary ADT Operations


slide-1
SLIDE 1

Dictionary ADT

Today’s announcements:

◮ MT1 tonight, 7-9:00p WOOD 2 ◮ HW2 out, due Feb 5, 11:59p

Today’s Plan

◮ Binary Search Trees

Dictionary ADT

Operations

◮ insert ◮ remove ◮ find

key value (data) Multics MULTiplexed Information and Computing Service Unix Uniplexed Multics BSD Berkeley Software Distribution GNU GNU’s Not Unix

◮ insert(Linux, Linus Torvald’s Unix) ◮ find(Unix) returns “Uniplexed Multics”

1 / 8

slide-2
SLIDE 2

Dictionary ADT Implementations

Worst Case time insert find remove (after find) Linked list Unsorted array Sorted array

2 / 8

slide-3
SLIDE 3

Binary Search in a Sorted Array

2 4 5 7 8 9 12 14 17 20 21 25 2 4 5 7 8 12 14 17 20 21 25 2 4 7 8 4 8 12 14 20 21 25 20 25 14

int bSearch(int A[], int key, int i, int j) { if (j < i) return -1; int m = (i + j) / 2; if (key < A[m]) return bSearch(A, key, i, m-1); else if (key > A[m]) return bSearch(A, key, m+1, j); else return m; }

3 / 8

slide-4
SLIDE 4

Binary Search Tree as Dictionary Data Structure

2 8 5 20 17 9 7 14 12 25 21 4

Kai Kuro Shiro Mei Koko Fuku Mikan Sakura Rin Hime Kinako Maru

Binary tree property

◮ each node has ≤ 2 children

Search tree property

For all nodes x,

◮ all keys in left subtree of x smaller than x’s key ◮ all keys in right subtree of x larger than x’s key

Result: easy to find any given key

4 / 8

slide-5
SLIDE 5

Dictionary ADT: BST implementation

2 9 5 20 15 10 7 30 17

Chiro Shiro Kuro Kai Tora Hime Rin Minto Fuku

template<class K, class D> class Dictionary{ public: //ctors etc. ... private: struct Node { K key; D data; Node * left; Node * right; }; Node * root; ... };

5 / 8

slide-6
SLIDE 6

Finding a Node

2 9 5 20 15 10 7 30 17

Chiro Shiro Kuro Kai Tora Hime Rin Minto Fuku

Node *& pfind(K & key, Node *& r) { if (r == NULL) return r; if (key < r->key) return pfind(key, r->left); if (key > r->key) return pfind(key, r->right); return r; } Runtime?

6 / 8

slide-7
SLIDE 7

Insert

2 9 5 20 15 10 7 30 17

Chiro Shiro Kuro Kai Tora Hime Rin Minto Fuku

void pinsert(K & key, D & data, Node *& root) { Node *& target = pfind(key, root); if( target != NULL) { cerr<<"Duplicate:"<<key<<"\n"; } target = new Node(key, data); } One reason to have the *& version of pfind.

7 / 8

slide-8
SLIDE 8

Multiple inserts into a BST

What BST results from inserting into an empty BST:

◮ 1, Momo, 2, Kuro, 3, Hana, 4, Koko, 5, Shiro, 6, Sora, 7, Fuku ◮ 7, Fuku, 6, Sora, 5, Shiro, 4, Koko, 3, Hana, 2, Kuro, 1, Momo ◮ 4, Koko, 2, Kuro, 6, Sora, 1, Momo 3, Hana, 5, Shiro, 7, Fuku,

How long do these take?

8 / 8