340 likes | 483 Views
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
E N D
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 • 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)
Data Structures: Binary search tree • The same tree T may be organized to more compact structure, preserving order of nodes (by key values)
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
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
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
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
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
Data Structures: AVL tree - sample • minimum and maximum operations are both O(h) • weight operation is O(h)
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
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)
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)
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)
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
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
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
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)