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

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Chapter 10 Binary Trees.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Trees, Binary Trees, and Binary Search Trees COMP171.
BST Data Structure A BST node contains: A BST contains
Trees and Red-Black Trees Gordon College Prof. Brinton.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching.
Binary Search Trees Chapter 7 Objectives
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Data Structures Chapter 10 Binary Trees Andreas Savva.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Kruse/Ryba ch101 Object Oriented Data Structures Binary Trees Binary Search Trees Height Balance:AVL Trees Splay Trees.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.
1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees.
Binary Search Tree Qamar Abbas.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Introduction to C Programming CE Lecture 24 Insertion and Deletion with Binary Search Trees.
CS 367 – Introduction to Data Structures
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
1 Data Structures CSCI 132, Spring 2014 Lecture 34 Analyzing Hash Tables.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Binary Search Trees ©Robert E. Tarjan Dictionary: contains a set S of items, each with associated information. Operations: Access(x): Determine.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Binary Search Trees Chapter 7 Objectives
BST Trees
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Trees 3: The Binary Search Tree
Chapter 21: Binary Trees.
Data Structures CSCI 132, Spring 2014 Lecture 35 Binary Trees
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

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

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

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 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 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 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 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 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 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 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 Examples of removing a node Remove node 5. What replaces it? 2. Remove node 15. What replaces it?

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