Download presentation

Presentation is loading. Please wait.

Published bySemaj Alger Modified over 2 years ago

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

2
2 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.

3
3 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.

4
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; }

5
5 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:

6
6 Tree_sort( ) vs. quick sort In tree sort, the first item is inserted into the root of the tree. All subsequent items partitioned to the left or right depending on their relation to first item. This is analogous to quick sort, if the first item in the list is used as the pivot for partition. In tree sort, the second item becomes the root of a subtree. It becomes the pivot to partition all subsequent items in that subtree. This is analogous to quick sort for partitioning of one of the sublists.

7
7 Running time of tree_sort All comparisons for tree sort are done during the insert() calls. The insert() function does the same number of comparisons as quick sort. Therefore, tree sort has the same running time as quick sort: Worst case: Number of comparisons = O(n 2 ) Average case: Number of comparisons = O(n lg n) Approximately: 1.39 n lg(n) + O(n)

8
8 Advantage of tree sort Tree sort does not require that all the items are present in the list at the start of sorting. Items can be added gradually as they become available. Tree sort works on a linked structure that allows easier insertions and deletions than a contiguous list. Disadvantage: In the worst case, tree sort is slow.

9
9 Removing a node from a binary search tree--case 1 Case 1: The node is a leaf. Replace the pointer to the node with Null. Then delete the node. data left right sub_root

10
10 remove( ) case 2 Case 2: The node has 1 non-NULL subtree. Replace link from parent to node with a link from the parent to the non-null subtree. data left right sub_root data left right sub_root data left right... data left right... Case 2a: Case 2b:

11
11 remove( ) case 3 Case 3: The node has 2 non-NULL subtrees. Find the immediate predecessor of the node by moving 1 branch to the left and then as far right as possible. Replace the node with its immediate predecessor. data left right sub_root data leftright... data left... right

12
12 Examples of removing a node 10 5 15 2712 17 131 3 4 6 8 1. Remove node 5. What replaces it? 2. Remove node 15. What replaces it?

13
13 Implementation of remove( ) template Error_code Search_tree :: remove_root( Binary_node * &sub_root){ if (sub_root == NULL) return not_present; // Remember node to delete at end. Binary_node *to_delete = sub_root; if (sub_root->right == NULL) //cases 1 and 2a sub_root = sub_root->left; else if (sub_root->left == NULL) //case 2b sub_root = sub_root->right;

14
14 remove( ) continued else { // Neither subtree is empty. Case 3 to_delete = sub_root->left; // Move left to find predecessor. Binary_node *parent = sub_root; // parent of to_delete while (to_delete->right != NULL) { // to_delete is not the predecessor. parent = to_delete; to_delete = to_delete->right; } sub_root->data = to_delete->data; // Move from to_delete to root. if (parent == sub_root) sub_root->left = to_delete->left; else parent->right = to_delete->left; } delete to_delete; // Remove to_delete from tree. return success; }

15
15 search_and_destroy( ) template Error_code Search_tree :: remove(const Record &target) { return search_and_destroy(root, target); } template Error_code Search_tree :: search_and_destroy( Binary_node * &sub_root, const Record &target) { // find the node to remove and remove it. // we will work this out in class. }

16
16 search_and_destroy( ) template Error_code Search_tree :: remove(const Record &target) { return search_and_destroy(root, target); } template Error_code Search_tree :: search_and_destroy( Binary_node * &sub_root, const Record &target) { if (sub_root == NULL || sub_root->data == target) return remove_root(sub_root); else if (target data) return search_and_destroy(sub_root->left, target); else return search_and_destroy(sub_root->right, target); }

Similar presentations

OK

Topics Definition and Application of Binary Trees Binary Search Tree Operations.

Topics Definition and Application of Binary Trees Binary Search Tree Operations.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on online music store Ppt on amway business plan Ppt on cross docking logistics Ppt on technology in agriculture Ppt on precautions of tsunami in japan History of computers for kids ppt on batteries Ppt on bresenham's line drawing algorithm in c Renal system anatomy and physiology ppt on cells Ppt on new technology in automobile Ppt on 60 years of indian parliament structure