1 / 34

Algorithms and Data Structures

Algorithms and Data Structures. Lecture 5. Agenda:. Binary search trees – continued Balanced trees, AVL trees Hash tables. Data Structures: Binary search tree. Most operations under the BSTs are O(h), where h-height of a tree

vega
Download Presentation

Algorithms and Data Structures

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. Algorithms and Data Structures Lecture 5

  2. Agenda: • Binary search trees – continued • Balanced trees, AVL trees • Hash tables

  3. Data Structures: Binary search tree • Most operations under the BSTs are O(h), where h-height of a tree • Height depends on structure of a BST; depends on how nodes are organized into the tree • E.g. nodes of tree T may be organized to linear structure, in this case h=n, where n is a cardinality (number of nodes) of tree T (|T|=n)

  4. Data Structures: Binary search tree

  5. Data Structures: Binary search tree • The same tree T may be organized to more compact structure, preserving order of nodes (by key values)

  6. Data Structures: Binary search tree • Every binary tree has a marginal capacity; it is a sum of possible nodes occurred at all levels of tree • The very first level of a binary tree may have only one node (root node) • Each next level of a tree may contain two times more nodes than the previous level may • For given tree T; n – number of nodes, h –height of tree, N- marginal capacity of T • N=1+2+ … + 2h = 2h+1-1

  7. Data Structures: Binary search tree

  8. Data Structures: Binary search tree • If n is close to N then tree is a packed tree • Most operations on BST (excepting enumeration) are O(h) • N=2h+1-1 => N+1=2h+1 => log2(N+1)=log22h+1 => log2(N+1)=h+1 => h=log2(N+1)-1 • O(h)=O(log2(N+1)-1) => O(log2N) assuming N>=1

  9. Data Structures: Binary search tree • In best case, when n=N (tree is packed) O(log2n) • In worst case, when n=h, O(n) • Packed trees allow to perform more efficient operations under the data they represent

  10. Data Structures: Balanced tree • Definition: balanced tree is a binary search tree, which is either (a) empty or (b) its left and right sub-trees are “more or less” of equal height • Definition: AVL (Adelson-Velski, Landis) tree is a binary search tree T, which is either (a) empty or (b) difference between heights of its left and right sub-trees is not greater than one and both sub-trees are also AVL trees • |hleft-hright| <=1

  11. Data Structures: AVL tree

  12. Data Structures: AVL tree • Any binary search tree may be transformed to a corresponding AVL tree preserving order of nodes (by key values) • Transformation is performed by means of rotations • In order to transform a BST to an AVL form a number of rotations might be required

  13. Data Structures: AVL tree

  14. Data Structures: AVL tree - sample

  15. Data Structures: AVL tree - sample

  16. Data Structures: AVL tree - sample • minimum and maximum operations are both O(h) • weight operation is O(h)

  17. Data Structures: AVL tree - sample • Balance operation – makes tree balanced (AVL) • Must be used after any operation that modifies an AVL tree (insert, remove and etc.) • Operations preserves order of nodes (by values of keys) • May be used in order to convert BST to AVL tree • Balancing starts from smallest sub-trees and continues until all the sub-trees become AVL trees

  18. Data Structures: AVL tree - sample

  19. Data Structures: AVL tree - sample • isbalanced operation is O(n) • Any modifying operation (like insert, remove) requires (a) recalculation of sub-trees weights and (b) balancing • Modifying operations are O(n)

  20. Data Structures: hash table • Hash table is an auxiliary structure that allows increasing performance of operations like search, element access and etc. • Hash tables may be used along with other data structures • Hash tables may be used independently on other data structures • Usage of hash tables may enable very quick access to data; e.g. search in BST is O(h), hash table may speed up search to be O(1)

  21. Data Structures: hash table • The main idea behind a hash table is usually an array of elements (e.g. pointers) that allow direct access to elements of some data structure (e.g. tree) by their key values • Generally, hash table is a map from a set of keys to a set of data elements; if we know key we can quickly access an element of some data structure (e.g. tree)

  22. Data Structures: direct-address table • Direct address table is a variation of hash table • DAT is an array of pointers (to an element of some data structure); • Each slot of the array corresponds to a key value, if we know a key we can access corresponding element directly using pointer from array of keys

  23. Data Structures: direct-address table

  24. Data Structures: direct-address table • DATs are usually used if number of possible keys is constrained and keys are unique • If number of possible keys is large enough, usage of DAT may become memory consuming

  25. Data Structures: DAT - sample

  26. Data Structures: DAT - sample • Set of keys: K = { x | x from N, 0 =< x < 1000, x is unique } • Keys are stored as elements of linked list • Direct address table provides quick access to the elements by their key values • Operations: search, add, deletebykey, deletebyrecord

  27. Data Structures: DAT - sample

  28. Data Structures: DAT - sample

  29. Data Structures: DAT - sample

  30. Data Structures: DAT - sample

  31. Data Structures: DAT - sample

  32. Data Structures: DAT - sample

  33. Data Structures: DAT - sample • Add operation is O(1), in case of list it was O(1) too • Search operation is O(1), in case of list it was O(n) • DeleteByRecord is O(1), in case of list it was O(1) too • DeleteByKey is O(1), in case of list it was O(n)

  34. Q&A

More Related