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

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

Embed Size (px)

Citation preview

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

1

Data Structures

CSCI 132, Spring 2014Lecture 36

Binary Search Trees

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

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.

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

3

Binary_node struct

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

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

4

Binary_tree class

template <class Entry>class Binary_tree {public:

// Add methods here.protected:

// Add auxiliary function prototypes here.Binary_node<Entry> *root;

};

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

5

Implementation of constructor and empty( ) functions

template <class Entry>Binary_tree<Entry> :: Binary_tree( ){

root = NULL;}

template <class Entry>bool Binary_tree<Entry> :: empty( ) const{

return root == NULL;}

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

6

In order traversal

template <class Entry>void Binary_tree<Entry> :: inorder(void (*visit)(Entry &)) {

recursive_inorder(root, visit);}

template <class Entry>void Binary_tree<Entry> :: recursive_inorder(Binary_node<Entry> *sub_root,

void (*visit)(Entry &)) {

//We'll work this out in class}

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

7

Binary_tree specificationtemplate <class Entry>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<Entry> &original);Binary_tree & operator = (const Binary_tree<Entry> &original);~Binary_tree( );

protected:// Add auxiliary function prototypes here.Binary_node<Entry> *root;

};

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

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)?

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

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.

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

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.

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

11

Specification of a Binary Search Tree

template <class Record>class Search_tree: public Binary_tree<Record> {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.

};

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

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.

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

13

Implementation of tree search

template <class Record>Binary_node<Record> *Search_tree<Record> :: search_for_node(

Binary_node<Record>* sub_root, const Record &target) const{

//We will fill this in in class.

}

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

14

The tree_search( ) method

template <class Record>Error_code Search_tree<Record> :: tree_search(Record &target) const{

Error_code result = success;Binary_node<Record> *found = search_for_node(root, target);if (found == NULL)

result = not_present;else

target = found->data;return result;

}

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

15

Examples of binary search trees

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

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

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

Implementing insert( )template <class Record>Error_code Search_tree<Record> :: insert(const Record &new_data){

return search_and_insert(root, new_data);}template <class Record>Error_code Search_tree<Record> :: search_and_insert(

Binary_node<Record> * &sub_root, const Record &new_data){

if (sub_root == NULL) {sub_root = new Binary_node<Record>(new_data);return success;

} else if (new_data < sub_root->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;

}

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

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: