1 / 24

The BSTreeTE, KF class

an example. struct treeItem{ int id; // key string data; // value int key( ) const { return id; }};. . BSTree<treeItem, int> myBSTree;. basic BST search algorithm. void search (bstree, searchKey){ if (bstree is empty) //base case:

phillip
Download Presentation

The BSTreeTE, KF class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. the BSTree<TE, KF> 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

    3. basic BST search algorithm

    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.

    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 (log2 n) maximum: n - 1 average: ?

    10. faster searching "balanced" search trees guarantee O(log2 n) search path by controlling height of the search tree AVL tree 2-3-4 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 <key, value> pair all items stored in the array chained hash table element type is a pointer to a linked list of nodes containing <key, value> 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

    16. Handling Collisions

    17. Search Performance

    18. A Chained Hash Table

    19. Search Performance

    20. successful search performance

    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

More Related