Symbol Tables Computing 2 COMP1927 15s1. S YMBOL T ABLES Searching: like sorting, searching is a fundamental element of many computational tasks data.

Slides:



Advertisements
Similar presentations
1 Symbol Tables Chapter Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Comp 245 Data Structures Trees. Introduction to the Tree ADT A tree is a non-linear structure. A treenode can point to 0 to N other nodes. There is one.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Advanced Data Structures
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees Aaron Bauer Winter 2014.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
B+ Trees COMP
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
IS 2610: Data Structures Searching March 29, 2004.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Even MORE Balanced Trees Computing 2 COMP s1.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Symbol Tables and Search Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Lecture1 introductions and Tree Data Structures 11/12/20151.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
Dictionaries CS /02/05 L7: Dictionaries Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
IS 2610: Data Structures Priority Queue, Heapsort, Searching March 15, 2004.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 2015.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
COMP261 Lecture 23 B Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
BST Trees
Binary Search Tree (BST)
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Data Structures and Programming Techniques
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 7 Objectives
CSC 143 Binary Search Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Symbol Tables Computing 2 COMP s1

S YMBOL T ABLES Searching: like sorting, searching is a fundamental element of many computational tasks data bases dictionaries compiler symbol tables Symbol table: a symbol table is a data structure of items with keys that supports at least two basic operations: insert a new item (key,value) (student id, student data) – in a database (word, meaning) – in a dictionary return an item identified by a given key

A BSTRACTING OVER CONCRETE I TEM AND K EY TYPE We abstract over the concrete item type by defining these types and some basic operations on them in a separate header file, Item.h : typedef int Key; struct record { Key keyval; char value[10]; }; typedef struct record *Item; #define key(A) ((A)->keyval) #define eq(A,B) {A == B} #define less(A,B) {A < B} #define NULLitem NULL // special value for no item int ITEMscan (Item *); // read from stdin int ITEMshow (Item); // print to stdout typedef int Key; struct record { Key keyval; char value[10]; }; typedef struct record *Item; #define key(A) ((A)->keyval) #define eq(A,B) {A == B} #define less(A,B) {A < B} #define NULLitem NULL // special value for no item int ITEMscan (Item *); // read from stdin int ITEMshow (Item); // print to stdout

S YMBOL TABLE AS A BSTRACT D ATA T YPE Symbol Table ADT: typedef struct symbolTable *ST; // new symbol table ST STinit (void); // number of items in the table int STcount (ST); // insert an item void STinsert (ST, Item); // find item with given key Item STsearch (ST, Key); // delete given item void STdelete (ST, Item); // find nth item Item STselect (ST, int); // visit items in order of their keys void STsort (ST, void (*visit)(Item)); typedef struct symbolTable *ST; // new symbol table ST STinit (void); // number of items in the table int STcount (ST); // insert an item void STinsert (ST, Item); // find item with given key Item STsearch (ST, Key); // delete given item void STdelete (ST, Item); // find nth item Item STselect (ST, int); // visit items in order of their keys void STsort (ST, void (*visit)(Item));

S YMBOL TABLE AS A BSTRACT D ATA T YPE How do we deal with duplicate keys? depends on the application: Do not allow duplicates Insertion of duplicates does nothing – fails silently Insertion of duplicates returns an error store all items with the same key in one entry in the symbol table store duplicates as separate entries in the symbol table Our approach will not allow duplicates and ignore attempts to insert them.

A SIMPLE S YMBOL T ABLE CLIENT PROGRAM We start by writing a simple client program: reads items from stdin insert item if not yet in table print resulting table in order print out the smallest, largest and median values.

S YMBOL TABLE IMPLEMENTATIONS Symbol tables can be represented in many ways: key-indexed array (max # items, restricted key space) key-sorted arrays (max # items, using binary search) linked lists (unlimited items, sorted list?) binary search trees (unlimited items, traversal orders) Costs (assuming N items): TypeSearch Cost MinMaxAverage Key Indexed ArrayO(1) Key sorted ArrayO(1)O(log n) Linked ListO(1)O(n) Binary Search TreeO(1)O(n)O(log n)

8 I MPLEMENTATION : K EY I NDEXED A RRAY Use key to determine index position in the array requires dense keys (i.e., few gaps) keys must be integral (or easy to map to integral value) Properties: insert, search and delete are constant time O(1) init, select, and sort are linear in table size NULLitem [0][1][2][7][6][5][3][4] 1,data3,data4,data5,data7,data items

I MPLEMENTATION : B INARY S EARCH T REES Binary tree: key (and maybe items) in internal nodes key in a node is larger than any key in its left subtree smaller than any key in its right subtree Properties: init & count are constant time insert, delete, search & select are logarithmic in the number of stored items in average case, linear in worst case (degenerate tree) sort linear in numbers of stored items

I MPLEMENTATION : B INARY S EARCH T REES In our implementation, we use a dummy node to represent empty trees Representation of an empty tree: 5 previously: 5 00 Representation of a tree with a single value node: previously:new implementation : 0 dummy value new implementation :

B INARY S EARCH T REE : INSERTION OF NEW NODE Insert item with key ‘3’ into tree: rootNodeLink

B INARY S EARCH T REE : INSERTION OF NEW NODE Insert item with key ‘3’ into tree: rootNodeLink 0 3 0

B INARY S EARCH T REE To save space, all the empty subtrees are actually represented by the same struct: emptyTree

I MPLEMENTATION : B INARY S EARCH T REES :In our implementation, we use a dummy node to represent empty trees: struct st{ link root; } typedef struct STnode* link; struct STnode { Item item; link left,; link right; int size; //Size of sub-tree rooted at this node }; static link emptyTree = NULL; // dummy node representing empty tree static link newNode(Item item, link l, link r, int size); ST STinit (void) { ST st = malloc(sizeof(struct st)); if(emptyTree == NULL) //only one actual copy of emptyTree is ever created emptyTree = newNode(NULLitem, NULL, NULL, 0); st->root = emptyTree; return st; } struct st{ link root; } typedef struct STnode* link; struct STnode { Item item; link left,; link right; int size; //Size of sub-tree rooted at this node }; static link emptyTree = NULL; // dummy node representing empty tree static link newNode(Item item, link l, link r, int size); ST STinit (void) { ST st = malloc(sizeof(struct st)); if(emptyTree == NULL) //only one actual copy of emptyTree is ever created emptyTree = newNode(NULLitem, NULL, NULL, 0); st->root = emptyTree; return st; }

I MPLEMENTATION : B INARY S EARCH T REES Implementation of recursive insertion: link insertR (link currentLink, Item item) { Key v = key (item); Key currentKey = key (currentLink->item); if (currentLink == emptyTree) { return newNode(item, emptyTree, emptyTree, 1); } if (less(v, currentKey)) { currentLink->left = insertR (currentLink->left, item); } else { currentLink->right = insertR (currentLink->right, item); } (currentLink->size)++; return currentLink; } link insertR (link currentLink, Item item) { Key v = key (item); Key currentKey = key (currentLink->item); if (currentLink == emptyTree) { return newNode(item, emptyTree, emptyTree, 1); } if (less(v, currentKey)) { currentLink->left = insertR (currentLink->left, item); } else { currentLink->right = insertR (currentLink->right, item); } (currentLink->size)++; return currentLink; }

BST: SELECT How can we select the k th smallest element of a search tree? Can be done quite easily if we store the size of the subtree in each node (start with 0) Base case 1: if tree is empty tree search was unsuccessful Base case 2: if left subtree has k items return node item Recursive case 1: left subtree has m > k items continue search of k th item in left subtree Recursive case 2: left subtree has m < k items continue search of (k-m-1) th item in right subtree

S ELECT K TH I TEM For a tree with N Nodes, indexes are 0..N-1

I MPLEMENTATION : B INARY S EARCH T REES Implementation of select static Item selectR (link currentTree, int k) { if (currentTree == emptyTree) { return NULLitem; } if (currentTree->left->size == k) { return (currentTree->item); } if (currentTree->left->size > k) { return (selectR (currentTree->left, k)); } return (selectR (currentTree->right, k currentTree->left->size)); } Item STselect (ST s,int k) { return (selectR (s->root, k)); } static Item selectR (link currentTree, int k) { if (currentTree == emptyTree) { return NULLitem; } if (currentTree->left->size == k) { return (currentTree->item); } if (currentTree->left->size > k) { return (selectR (currentTree->left, k)); } return (selectR (currentTree->right, k currentTree->left->size)); } Item STselect (ST s,int k) { return (selectR (s->root, k)); }

P ERFORMANCE CHARACTERISTICS OF BST S We already discussed the performance of binary search trees: on average, O( log n ) steps to search, insert in a tree with n items worst case (degenerate tree) O( n ) steps

S YMBOL T ABLES AS I NDEXES Scenario: large set of items; need efficient access via key but also need sequential access to items items might be stored in very large array or file Solution: leave items in place use symbol table holding (key,ref) pairs Commonly used as an access mechanism in databases.