Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Tree C and Data Structures Baojian Hua

Similar presentations


Presentation on theme: "Binary Search Tree C and Data Structures Baojian Hua"— 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 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=, 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 2060 10 3050 5 70 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 leftkeyrightv

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 2060 10 3050 5 70 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 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 k) return lookup (t->left, key); else return lookup (t->right, key); }

12 Example: search 55 40 2060 10 3050 5 70 55

13 Example: search 55 40 2060 10 3050 5 70 55

14 Example: search 55 40 2060 10 3050 5 70 55

15 Example: search 55 40 2060 10 3050 5 70 55

16 Example: search 55 40 2060 10 3050 5 70 55

17 Example: search 55 40 2060 10 3050 5 70 55

18 Example: search 55 40 2060 10 3050 5 70 55

19 Example: search 55 40 2060 10 3050 5 70 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 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 What ’ s a proper position?

22 Example: insert 45 40 2060 10 3050 5 70 5545

23 Example: insert 45 40 2060 10 3050 5 70 5545 searchParent

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 key) || (p->right && key>p->key)) error (“key already exists”); if (key 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 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 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 2060 10 3050 5 70 5545 50 60 40

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 2060 10 3050 5 70 55

30 remove case#2: 1-degree node 40 2060 10 3050 5 70 55

31 remove case#3: 2-degree node 40 2060 10 3050 5 70 55

32 remove case#3: 2-degree node 40 2055 10 3050 5 70 60


Download ppt "Binary Search Tree C and Data Structures Baojian Hua"

Similar presentations


Ads by Google