1 / 32

Binary Search Tree

Binary Search Tree. C and Data Structures Baojian Hua bjhua@ustc.edu.cn. 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)

ryan-kidd
Download Presentation

Binary Search Tree

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. 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’d discussed a linear list-based representation of dictionary, these slides study a 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 typedef struct bst *bst; bst new (); bst new2 (bst l, bst r, poly key, poly value); bst insert (bst t, poly key, poly value); poly lookup (bst t, poly key); void delete (bst t, poly key); … #endif

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

  8. Operations: “new” bst new () { bst t = checkedMalloc (sizeof (*t)); t->key = NULL; t->value = NULL; t->left = NULL; t->right = NULL; return t; } t /\ key value /\

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

  10. How to search in a BST?---lookup (bst, key) • Top-down recursion: • if bst if NULL, search fails • else compare key with bst->key: • if (key == bst->key), search succeeds, return t->value • if (key < bst->key), then key may only appear in left sub-tree, so we continue search (bst->left, key) • if (key > t->key), then key may only appear in right sub-tree, so we continue search(bst->right, key)

  11. Operations: “lookup” poly lookup (bst t, poly key) { if (!t) return NULL; else if (key == t->key) // what’s “==“ ? return t->value; else if (k < t->k) return lookup (t->left, key); else return lookup (t->right, key); }

  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. Example: search 55 40 20 60 10 30 50 70 5 55

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

  21. 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?

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

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

  24. Operations: “insert” bst insert (bst t, poly key, poly value) { bst newTree = new2 (NULL, NULL, key, value); if (!t) return newTree; bst p = searchParent (t, key); if (!p || (p->left && key<p->key) || (p->right && key>p->key)) error (“key already exists”); if (key < p->key) p->left = newTree; else p->right = newTree; return t; }

  25. How to Search a Parent? bst searchParent (bst t, poly key) { if (!t) error (“empty tree”); else if (key == t->key) return NULL; else if (key < t->key) { if (!t->left || key==t->left->key) // “==”? return t; else return searchParent (t->left, key); } else {…} // symmetry case for right sub-tree }

  26. A Functional Version of Insert bst insert (bst t, poly key, poly value) { if (search (t, k)) error (“key already exists”); else if (!t) return new2 (NULL, NULL, k, v); else if (key < t->key) return new2 (insert (t->left, key, value), t->right, t->key, t->value); else return new2 (t->left, insert (t->right, key, value), t->key, t->value); }

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

  28. Functional Style • Always construct new data from older ones • older ones left untouched and unchanged • Support equational reasoning very well • this time f(x)=5, then next time f(x)=5 • much like mathematical functions (hence the name) • Rely on garbage collection

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

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

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

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

More Related