36
A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Embed Size (px)

Citation preview

Page 1: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

A Review of Binary Search Trees

Dr. Gang Qian

Department of Computer ScienceUniversity of Central Oklahoma

Page 2: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Objectives (Sections 10.1 and 10.2) Binary tree

Definition Traversal

Binary search tree Definition Tree search Insertion Deletion

Page 3: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Binary Tree

Definition (Mathematical Structure) A binary tree is either empty, or it consists of a

node called the root together with two binary trees called the left subtree and the right subtree of the root

Note Linked implementation is natural Other implementation is also possible

Page 4: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

The concept of left and right is important for binary trees Binary trees with two nodes

Binary trees with three nodes

Not a binary tree

Page 5: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Traversal of Binary Trees

Traversal Moving through all nodes of the binary tree,

visiting each node in turn The order of traversal should be logical

At any given node in a binary tree, there are three tasks to do: Visit the node itself (V) Traverse its left subtree (L) Traverse its right subtree (R)

Page 6: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

There are six ways to arrange the three tasks: V L R; L V R; L R V; V R L; R V L; R L V

They are reduced to three if we always consider the left subtree before the right Preorder: V L R Inorder: L V R Postorder: L R V

Page 7: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Example

(Expression Trees) Preorder traversal

- a x b c Inorder traversal

a – b x c Postorder traversal

a b c x –

Page 8: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Linked Implementation of Binary Tree

Page 9: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Binary tree node classtemplate <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 10: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Binary Tree Class 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;

(continued on next slide)

Page 11: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

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 12: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Implementation of inorder traversal Use an auxiliary recursive function that applies to subtrees

template <class Entry>

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

/* Post: The tree has been traversed in inorder sequence.

Uses: The function recursive_inorder */

{

recursive_inorder(root, visit);

}

Page 13: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

template <class Entry>void Binary tree<Entry> ::

recursive_inorder(Binary_node<Entry> *sub_root, void (*visit)(Entry &))

/* Pre: sub_root is either NULL or points to a subtree of the Binary_treePost: The subtree has been traversed in inorder sequenceUses: The function recursive_inorder recursively */

{if (sub_root != NULL) {

recursive_inorder(sub_root->left, visit);(*visit)(sub_root->data);recursive_inorder(sub_root->right, visit);

}}

Page 14: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Binary Search Tree

Motivation Binary search O(log n) is much more efficient than

sequential search O(n) We can use a contiguous implementation We cannot use linked list implementation What if the data needs frequent updates

Need an implementation for ordered lists that searches quickly (as with binary search on a contiguous

list) makes insertions and deletions quickly (as with a linked

list)

Page 15: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Definition A binary search tree is a binary tree that is either empty or

in which the data entry of every node has a key and satisfies the following conditions: The key of the left child of a node is less than the key of

its parent node The key of the right child of a node is greater than the key of

its parent node The left and right subtrees of the root are also binary search

trees Additional requirements

No two entries in a binary search tree may have equal keys

Page 16: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma
Page 17: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Binary Search Tree Class

The binary search tree class is derived from the binary tree class All binary tree methods are inherited

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 18: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

The inherited methods include the constructors, the destructor, clear, empty, size, height, and the traversals preorder, inorder, and postorder

Record class Each record is associated with a Key The keys can be compared with the usual comparison

operators By casting records to their corresponding keys, the

comparison operators apply to records as well as to keys

Page 19: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Tree Search

To search for the target, first compare it with the entry at the root of the tree. If their keys match, then search finishes Otherwise, depending on whether the target is

smaller than or greater than the root, search goes to the left subtree or the right subtree as appropriate and repeat the search in that subtree

The process is implemented by calling an auxiliary recursive function

Page 20: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Recursive auxiliary function

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

search_for_node(Binary_node<Record>* sub_root, const Record &target) const

{if (sub_root == NULL || sub_root->data == target) return sub_root;else if (sub_root->data < target) return search_for_node(sub_root->right, target);else return search_for_node(sub_root->left, target);

} Tail Recursion Recursion tree will be a chain

Page 21: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Non-recursive version

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

search_for_node(Binary_node<Record>* sub_root, const Record &target) const

{while (sub_root != NULL && sub_root->data != target) if (sub_root->data < target) sub_root = sub_root->right; else sub_root = sub_root->left;return sub_root;

}

Page 22: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Public method tree_searchtemplate <class Record>Error_code Search_tree<Record> :: tree_search(Record &target) const/* Post: If there is an entry in the tree whose key matches that in

target , the parameter target is replaced by the corresponding record from the tree and a code of success is returned. Otherwise a code of not_present is returned.Uses: function search_for_node */

{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 23: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Analysis of tree search The same keys may be built into binary search

trees of many different shapes

Page 24: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

If a binary search tree is nearly balanced (“bushy”), then search on a tree with n vertices will do O(log n) comparisons of keys

The bushier the tree, the smaller the number of comparisons The number of vertices between the root and the

target, inclusive, is the number of comparisons needed to find the target

If the tree degenerates into a chain, then tree search becomes the same as sequential search, doing O(n) comparisons on n vertices The worst case for tree search

Page 25: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Often impossible to predict the shape of the tree

If the keys are inserted in random order, then tree search usually performs almost as well as binary search

If the keys are inserted in sorted order into an empty tree, the degenerate case will occur

Page 26: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Insertion into A Binary Search Tree Find the location in the tree suitable to the

new record

Page 27: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Insertion method Call an auxiliary recursive function

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 28: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Public method: inserttemplate <class Record>Error_code Search_tree<Record> :: insert(

const Record &new_data){

return search_and_insert(root, new_data);}

The method insert can usually insert a new node into a random binary search tree with n nodes in O(log n) steps

Page 29: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Removal from A Binary Search Tree Key Issue:

The integrity of the tree has to be kept after deletion

Page 30: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma
Page 31: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Auxiliary recursive function

template <class Record>Error_code Search_tree<Record> :: search_and_delete(

Binary_node<Record>* &sub_root, const Record &target)/* Pre: sub_root is either NULL or points to a subtree Post: If the key of target is not in the subtree, a code of not present is

returned. Otherwise, a code of success is returned and the subtree node containing target has been removed in such a way that the properties of a binary search tree is preserved.

Uses: Functions search_and_delete recursively */{ if (sub_root == NULL) return not_present; else if (sub_root->data == target) { if (sub_root->right == NULL) { // No right child Binary_node<Record> *to_delete = sub_root; sub_root = sub_root->left; delete to_delete; } (continued on next slide)

Page 32: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

else if (sub_root->left == NULL) { // No left child

Binary_node<Record> *to_delete = sub_root;

sub_root = sub_root->right;

delete to_delete;

} else { // subroot has two children

// search for the immediate predecessor

Binary_node<Record> * predecessor_node = sub_root->left;

while (predecessor_node->right != NULL) {

predecessor_node = predecessor_node->right;

}

// replace the target with the immediate predecessor

sub_root->data = predecessor_node->data;

// delete the redundant immediate predecessor

search_and_delete(sub_root->left, sub_root->data);

}

}

(continued on next slide)

Page 33: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

else if (target < sub_root->data)

return search_and_delete(sub_root->left, target);

else

return search_and_delete(sub_root->right, target);

return success;

}

Page 34: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Public remove method

template <class Record>Error_code Search_tree<Record> :: remove(

const Record &target)/* Post: If a Record with a key matching that of target belongs

to the Search_tree, a code of success is returned and the corresponding node is removed from the tree. Otherwise, a code of not_present is returnedUses: Function search_and_delete */

{return search_and_delete(root, target);

} Uses an auxiliary recursive function that refers to the actual

nodes in the tree

Page 35: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Building a Binary Search Tree Build a bushy binary search tree from sorted

keys Textbook: pp. 463 -- 470

Page 36: A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma

Random Search Trees and Optimality The average binary search tree requires

approximately 1.39 times as many comparisons as a completely balanced tree.