300 likes | 347 Views
Learn about dictionary-like data structures using binary search trees in C programming: creation, insertion, lookup, deletion, and other essential operations. Code implementation included.
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 discussed a linear list-based representation of dictionary, this class studies another 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 #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
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
Operations: “new” T Bst_new () { return 0; }
How to search in a BST?---lookup (bst, key) 40 20 60 10 30 50 70 5 55
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); }
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 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; }
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
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); } }
Example: insert 45 40 40 20 60 60 10 30 50 70 50 5 45 55
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