330 likes | 465 Views
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)
E N D
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) • 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
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
Example 40 20 60 10 30 50 70 5 55
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)
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
Implementation // in file “bst.c” #include “bst.h” struct bst { poly key; poly value; bst left; bst right; }; t left key v right
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 /\
How to search in a BST?---lookup (bst, key) 40 20 60 10 30 50 70 5 55
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)
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); }
Example: search 55 40 20 60 10 30 50 70 5 55
Example: search 55 40 20 60 10 30 50 70 5 55
Example: search 55 40 20 60 10 30 50 70 5 55
Example: search 55 40 20 60 10 30 50 70 5 55
Example: search 55 40 20 60 10 30 50 70 5 55
Example: search 55 40 20 60 10 30 50 70 5 55
Example: search 55 40 20 60 10 30 50 70 5 55
Example: search 55 40 20 60 10 30 50 70 5 55
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; }
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?
Example: insert 45 40 20 60 10 30 50 70 5 45 55
Example: insert 45 40 20 60 searchParent 10 30 50 70 5 45 55
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; }
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 }
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); }
Example: insert 45 40 40 20 60 60 10 30 50 70 50 5 45 55
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
remove case#1: leaf node 40 20 60 10 30 50 70 5 55
remove case#2: 1-degree node 40 20 60 10 30 50 70 5 55
remove case#3: 2-degree node 40 20 60 10 30 50 70 5 55
remove case#3: 2-degree node 40 20 55 10 30 50 70 5 60