Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees.

Similar presentations


Presentation on theme: "1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees."— Presentation transcript:

1 1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees

2 2 Comparison trees for binary search Binary search of the following list: Amy Ann Dot Eva Guy Jan Jim Jon Kay Kim Ron Roy Tim Tom Note that inorder traversal gives the list in alphabetical order.

3 3 Binary_node struct template struct Binary_node { // data members: Entry data; Binary_node *left; Binary_node *right; // constructors: Binary_node( ); Binary_node(const Entry &x); };

4 4 Binary_tree class template class Binary_tree { public: // Add methods here. protected: // Add auxiliary function prototypes here. Binary_node *root; };

5 5 Implementation of constructor and empty( ) functions template Binary_tree :: Binary_tree( ) { root = NULL; } template bool Binary_tree :: empty( ) const { return root == NULL; }

6 6 In order traversal template void Binary_tree :: inorder(void (*visit)(Entry &)) { recursive_inorder(root, visit); } template void Binary_tree :: recursive_inorder(Binary_node *sub_root, void (*visit)(Entry &)) { //We'll work this out in class }

7 7 Binary_tree specification template class Binary_tree { public: Binary_tree( ); bool empty( ) const; void preorder(void (*visit)(Entry &)); void inorder(void (*visit)(Entry &)); void postorder(void (*visit)(Entry &)); int size( ) const; void clear( ); int height( ) const; void insert(const Entry &); Binary_tree (const Binary_tree &original); Binary_tree & operator = (const Binary_tree &original); ~Binary_tree( ); protected: // Add auxiliary function prototypes here. Binary_node *root; };

8 8 Binary Search for Linked Lists Sequential search is slow, O(n), but can be done on linked lists. Binary search is faster, O(log(n)), but cannot be done on linked lists. It can only be done on contiguous lists. Linked lists are preferred over contiguous lists for applications that require many insertions and deletions of data in the list. Question: Is there a linked data structure that we can search quickly (as for contiguous lists) and in which we can make insertions and deletions quickly (as for linked lists)?

9 9 Binary Search Trees Comparison tree for Binary search: Note that inorder traversal gives the list in alphabetical order. All items in left subtree are before root item alphabetically. All items in right subtree are after root item alphabetically.

10 10 Definition of a Binary Search Tree A binary search tree is either empty, or every node has a key for which the following are true: 1) The Key of the root node is greater than any key in the left subtree. 2) The key of the root node is less than any key in the right subtree. 3) The left and right subtrees are themselves binary search trees. Assumption: No two entries in a binary search tree may have equal keys.

11 11 Specification of a Binary Search Tree template class Search_tree: public Binary_tree { public: Error_code insert(const Record &new data); Error_code remove(const Record &old data); Error_code tree_search(Record &target) const; private: // Add auxiliary function prototypes here. };

12 12 Searching a binary search tree 1. Compare target with root. If they match, we're done. 2. If target < root key, search left subtree. 3. If target > root key, search right subtree.

13 13 Implementation of tree search template Binary_node *Search_tree :: search_for_node( Binary_node * sub_root, const Record &target) const { //We will fill this in in class. }

14 14 The tree_search( ) method template Error_code Search_tree :: tree_search(Record &target) const { Error_code result = success; Binary_node *found = search_for_node(root, target); if (found == NULL) result = not_present; else target = found->data; return result; }

15 15 Examples of binary search trees

16 16 Inserting into a Binary Search Tree To insert an item into a binary search tree, we must make sure that the tree maintains the binary search tree property. We determine where an item may be inserted in a subtree by comparison with the item at the root of the subtree If the item to be inserted has a value less than the value at the subroot, it must be inserted in the left subtree. If the item to be inserted has a value greater than the value of the subroot, it must be inserted into the right subtree. Example: Insert the following characters in order: e, b, d, f, a, g, c

17 Implementing insert( ) template Error_code Search_tree :: insert(const Record &new_data) { return search_and_insert(root, new_data); } template Error_code Search_tree :: search_and_insert( Binary_node * &sub_root, const Record &new_data) { if (sub_root == NULL) { sub_root = new Binary_node (new_data); return success; } else if (new_data data) return search_and_insert(sub_root->left, new_data); else if (new_data > sub_root->data) return search_and_insert(sub_root->right, new_data); else return duplicate_error; }

18 18 tree_sort( ) 1. Build a binary search tree by using n calls to insert( ). 2. Print items out in order using inorder traversal. How long does it take? It depends on the tree: Short sort time: Long sort time:


Download ppt "1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees."

Similar presentations


Ads by Google