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)

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Hashing Techniques.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Trees, Binary Trees, and Binary Search Trees COMP171.
Hash Tables and Associative Containers CS-212 Dick Steflik.
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)
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
COSC 2007 Data Structures II
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.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
1 Road Map Associative Container Impl. Unordered ACs Hashing Collision Resolution Collision Resolution Open Addressing Open Addressing Separate Chaining.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Binary Search Tree Qamar Abbas.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
David Stotts Computer Science Department UNC Chapel Hill.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
CISC220 Fall 2009 James Atlas Dec 04: Hashing and Maps K+W Chapter 9.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Foundation of Computing Systems Lecture 4 Trees: Part I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Lecture 18. Basics and types of Trees
Week 11 - Friday CS221.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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.
Hash table another data structure for implementing a map or a set
Advanced Associative Structures
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Hash Tables and Associative Containers
Advanced Implementation of Tables
Binary Trees, Binary Search Trees
Advanced Implementation of Tables
Binary Trees, Binary Search Trees
Presentation transcript:

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) which has  a data member for the value  a data member for the key  a method with the signature: KF key( ) const; where KF is the type of the key

2 an example struct treeItem { int id; // key string data; // value int key( ) const { return id; } }; BSTree myBSTree;

3 basic BST search algorithm void search (bstree, searchKey) { if (bstree is empty) //base case: item not found // take needed action else if (key in bstree's root == search Key) // base case: item found // take needed action else if (searchKey < key in bstree's root ) search (leftSubtree, searchKey); else search (rightSubtree, searchKey); }

4 deletion cases  item to be deleted is in a leaf node  pointer to its node (in parent) must be changed to NULL  item to be deleted is in a node with one empty subtree  pointer to its node (in parent) must be changed to the non-empty subtree  item to be deleted is in a node with two non-empty subtrees

5 the easy cases

6 the “hard” case

7 the “hard” case replace with smallest in right subtree (inorder successor) replace with largest in left subtree (inorder predecessor)

8 traversing a binary search tree  can use any of the binary tree traversal orders – preorder, inorder, postorder  base case is reaching an empty tree  inorder traversal visits the elements in order of their key values  how would you visit the elements in descending order of key values?

9 big Oh of BST operations  measured by length of the search path  depends on the height of the BST  height determined by order of insertion  height of a BST containing n items is  minimum: floor (log 2 n)  maximum: n - 1  average: ?

10 faster searching  "balanced" search trees guarantee O(log 2 n) search path by controlling height of the search tree  AVL tree  tree  red-black tree (used by STL associative container classes)  hash table allows for O(1) search performance  search time does not increase as n increases

11 Hash Table  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables (Nyhoff – Ch.9.3)  open hash table  array element type is a pair  all items stored in the array  chained hash table  element type is a pointer to a linked list of nodes containing pairs  items are stored in the linked list nodes  keys are used to generate an array index  home address (0.. Tsize-1)

12 Considerations  How big an array?  load factor of a hash table is n/Tsize  Hash function to use?  int hash(KeyType key) -> 0.. Tsize-1  Collision resolution strategy?  hash function is many-to-one

13 Hash Function  a hash function is used to map a key to an array index (home address)  search starts from here  insert, retrieve, update, delete all start by applying the hash function to the key

14 Some hash functions  if KeyType is int - key % TSize  if KeyType is a string - convert to an integer and then % Tsize  goals for a hash function  fast to compute  even distribution  cannot guarantee no collisions unless all key values are known in advance

15 An Open Hash Table key value Hash (key) produces an index in the range 0 to 6. That index is the “home address” Some insertions: K1 --> 3 K2 --> 5 K3 --> 2 K1 K1info K2 K2info K3 K3info

16 Handling Collisions K3 K3info K1 K1info K2 K2info Some more insertions: K4 --> 3 K5 --> 2 K6 --> 4 K4 K4info K5 K5info K6 K6info Linear probing collision resolution strategy

17 Search Performance K3 K3info K1 K1info K2 K2info K4 K4info K5 K5info K6 K6info Average number of probes needed to retrieve the value with key K? K hash(K) #probes K1 3 1 K2 5 1 K3 2 1 K4 3 2 K5 2 5 K /6 = 2.33 (successful) unsuccessful search?

18 A Chained Hash Table insert keys: K1 --> 3 K2 --> 5 K3 --> 2 K4 --> 3 K5 --> 2 K6 --> 4 linked lists of synonyms K3 K3info K1 K1info K5 K5info K4 K4info K6 K6info K2 K2info

19 Search Performance Average number of probes needed to retrieve the value with key K? K hash(K) #probes K1 3 1 K2 5 1 K3 2 1 K4 3 2 K5 2 2 K /6 = 1.33 (successful) K3 K3info K1 K1info K5 K5info K4 K4info K6 K6info K2 K2info unsuccessful search?

20 successful search performance open addressing open addressing chaining (linear probing) (double hashing) load factor

21 Factors affecting Search Performance  quality of hash function  how uniform?  depends on actual data  collision resolution strategy used  load factor of the HashTable  N/Tsize  the lower the load factor the better the search performance

22 Traversal  Visit each item in the hash table  Open hash table  O(Tsize) to visit all n items  Tsize is larger than n  Chained hash table  O(Tsize + n) to visit all n items  Items are not visited in order of key value

23 Deletions?  search for item to be deleted  chained hash table  find node and delete it  open hash table  must mark vacated spot as “deleted”  is different than “never used”

24 Hash Table Summary  search speed depends on load factor and quality of hash function  should be less than.75 for open addressing  can be more than 1 for chaining  items not kept sorted by key  very good for fast access to unordered data with known upper bound  to pick a good TSize