Лекция 5: Словари. Бинарные деревья поиска

  • View
    611

  • Download
    5

Embed Size (px)

Text of Лекция 5: Словари. Бинарные деревья поиска

  • 1. 5: . ... http://www.mkurnosov.net

2. 2 1. (, deque). 2. , , O? 3. (Map) 3 ( , associative array, map, dictionary) (key) (value). (Key) (Value) 890 1200 260 530 4. (Map) 4 Add(map, key, value) map (key, value) Lookup(map, key) map key Remove(map, key) map key Min(map) map Max(map) map 5. (Map) 5 , , : 1. (Search trees) 2. - (Hash tables) 3. 4. 6. 6 Add (map, key, value) O(1) ( ) O(n) ( ) Lookup (map, key) O(n) O(logn) ( ) Remove (map, key) O(n) ( ) O(n) ( ) Min(map) O(n) O(1) ( v[1]) Max(map) O(n) O(1) ( v[n]) 7. 7 Add (map, key, value) O(1) ( ) O(n) ( ) Lookup (map, key) O(n) O(n) Remove (map, key) O(n) ( ) O(n) ( ) Min(map) O(n) O(1) Max(map) O(n) O(n) O(1), 8. (Binary Search Trees) 8 (Binary Search Tree, BST) , : 1) x (node) (child nodes) (key) (value) 2) x 3) x Key: 530 Value: Left Right 9. (Binary Search Trees) 9 Key: 530 Value: Left Right (Key) (Value) 890 1200 260 530 Key: 260 Value: Left Right Key: 890 Value: Left Right Key: 1200 Value: Left Right 10. (Binary trees) 10 (Binary tree) ( ), (node) (child nodes). Root node Leaf node Child nodeParent node Depth 0 Depth 1 Depth 2 Depth 3 11. (Binary Search Trees) 11 60 35 90 8 15 180 200 4000 600 : 9 , (depth) = 3 12. (Binary Search Trees) 12 #include #include struct bstree { int key; /* */ char *value; /* */ struct bstree *left; struct bstree *right; }; 13. BST 13 struct bstree *bstree_create(int key, char *value) { struct bstree *node; node = malloc(sizeof(*node)); if (node != NULL) { node->key = key; node->value = value; node->left = NULL; node->right = NULL; } return node; } T = O(1) 14. BST 14 int main() { struct bstree *tree; tree = bstree_create(180, "Tigr"); return 0; } 15. BST 15 60 15 180 200 1. (180, ) 2. (200, ) 3. (15, ) 4. (60, ) (leaf node) 16. BST void bstree_add(struct bstree *tree, int key, char *value) { struct bstree *parent, *node; if (tree == NULL) return; /* */ for (parent = tree; tree != NULL; ) { parent = tree; if (key < tree->key) tree = tree->left; else if (key > tree->key) tree = tree->right; else return; } 16 17. BST () 17 /* */ node = bstree_create(key, value); if (key < parent->key) parent->left = node; else parent->right = node; } 18. BST 18 60 15 180 200 1. . , . 2. 1. 19. BST 19 struct bstree *bstree_lookup(struct bstree *tree, int key) { while (tree != NULL) { if (key == tree->key) { return tree; } else if (key < tree->key) { tree = tree->left; } else { tree = tree->right; } } return tree; } 20. BST 20 60 15 180 200 . . 21. BST 21 struct bstree *bstree_min(struct bstree *tree) { if (tree == NULL) return NULL; while (tree->left != NULL) tree = tree->left; return tree; } 22. BST 22 60 15 180 200 . . 23. BST 23 struct bstree *bstree_max(struct bstree *tree) { if (tree == NULL) return NULL; while (tree->right != NULL) tree = tree->right; return tree; } 24. 24 int main() { struct bstree *tree, *node; tree = bstree_create(180, "Tigr"); bstree_add(tree, 200, "Lev"); bstree_add(tree, 60, "Volk"); node = bstree_lookup(tree, 200); printf(Value = %sn", node->value); node = bstree_min(tree); printf("Min: value = %sn", node->value); return 0; } 25. BST 25 10 15 180 200 1. z O(n) 2. 3 : o z o z 1 o z 2 60 90 26. BST 26 10 15 180 200 ( 1) 1. (free) 2. (left right) NULL ->right = NULL 60 90 27. BST 27 10 15 180 200 ( 2) 1. 2. (left right) 3. 60 90 ->right = ->right 28. BST 28 10 15 180 200 ( 3) 1. 2. ( ) 3. 60 90 70 150 45 55 29. BST 29 10 15 180 200 ( 3) 60 90 70 150 45 55 10 180 200 60 90 70 150 45 55 30. BST 30 1 Value 2 Value 1. h . 2. O(n) ( ) 3. O(logn) 3 Value 4 Value NULL NULL NULL bstree_add(tree, Item, 1); bstree_add(tree, Item, 2); bstree_add(tree, Item, 3); bstree_add(tree, Item, 4); 31. BST 31 (average case) (worst case) Add (map, key, value) O(logn) O(n) Lookup (map, key) O(logn) O(n) Remove (map, key) O(logn) O(n) Min(map) O(logn) O(n) Max(map) O(logn) O(n) 32. 32 (self-balancing binary search tree) , k. . : o - (Red-black tree): 2 log( + 1) o - (AVL-tree): 1.44 log ; o 2-3- (2-3-tree): log o - O(logn) 33. 33 (self-balancing search trees)