Binary Search Tree

# Binary Search Tree

## Binary Search Tree

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Binary Search Tree C and Data Structures Baojian Hua bjhua@ustc.edu.cn

2. Dictionary-like Data Structure • A dictionary-like data structure • contains a collection of tuple data: • data =<key, value> • key is comparable and distinct • supports these operations: • new () • insert (dict, k, v) • lookup (dict, k) • delete (dict, k) • We discussed a linear list-based representation of dictionary, this class studies another strategy based on binary trees

3. Binary Search Tree • A binary search tree is a binary tree satisfies: • every node contain data=<key, value>, and every key is unique • all keys in left sub-tree is less than that in the root • all keys in right sub-tree is greater than that in the root • both the left and right sub-trees are also binary search trees

4. Example 40 20 60 10 30 50 70 5 55

5. Operations • All the tree operations we’ve discussed also apply to binary search tree • And BST also supports (as a general dictionary-like data structure): • search (bst, key) • insert (bst, key, value) • delete (bst, key)

6. Abstract Data Types in C: Interface // in file “bst.h” #ifndef BST_H #define BST_H #define T Bst_t typedef struct T *T; T Bst_new (); T Bst_insert (T t, poly key, poly value); poly Bst_lookup (T t, poly key); #undef T #endif

7. Implementation // in file “bst.c” #include “bst.h” #define T Bst_t struct T { poly data; T left; T right; }; t left key v right

8. Operations: “new” T Bst_new () { return 0; }

9. How to search in a BST?---lookup (bst, key) 40 20 60 10 30 50 70 5 55

10. Operations: “lookup” poly Bst_lookup (T t, poly key) { if (0==t) return 0; if (key == t->key) // what’s “==“ ? return t->value; if (key < t->key) // what’s “<“? return lookup (t->left, key); return lookup (t->right, key); }

11. Example: search 55 40 20 60 10 30 50 70 5 55

12. Example: search 55 40 20 60 10 30 50 70 5 55

13. Example: search 55 40 20 60 10 30 50 70 5 55

14. Example: search 55 40 20 60 10 30 50 70 5 55

15. Example: search 55 40 20 60 10 30 50 70 5 55

16. Example: search 55 40 20 60 10 30 50 70 5 55

17. Example: search 55 40 20 60 10 30 50 70 5 55

18. Example: search 55 40 20 60 10 30 50 70 5 55

19. An Iterative Version poly lookup2 (T t, poly key) { if (0==t) return 0; T p = t; while (p && p->key!=key){ // what’s “!=“? if (key < p->key) p = p->left; else p = p->right; } return p; }

20. Adding New Bindings:insert (bst t, poly key, poly value) • Main idea: • search the tree, if these already exists a key k’==k, then insertion fails • else if tree t==NULL, return a new bst • else search a proper position to insert the tuple <key, value> • What’s a proper position?

21. Example: insert 45 40 20 60 10 30 50 70 5 45 55

22. Example: insert 45 40 20 60 searchParent 10 30 50 70 5 45 55

23. A Functional Version T insert (T t, poly key, poly value) { if (!t) return Bst_new2 (0, 0, key, value); switch (compare (key < t->key)) { case “==“: error (“key already exists”); case “<“: return Bst_new2 (insert (t->left, key, value), t->right, t->key, t->value); case “>”: return Bst_new2 (t->left, insert (t->right, key,value), t->key, t->value); } }

24. Example: insert 45 40 40 20 60 60 10 30 50 70 50 5 45 55

25. remove case#1: leaf node 40 20 60 10 30 50 70 5 55

26. remove case#2: 1-degree node 40 20 60 10 30 50 70 5 55

27. remove case#3: 2-degree node 40 20 60 10 30 50 70 5 55

28. remove case#3: 2-degree node 40 20 55 10 30 50 70 5 60