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

Slides:



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

Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Chapter 10 Binary Trees.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Search Trees1 Part-F1 Binary Search Trees   
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Void write_method() /* Post: A short string identifying the abstract data type used for the structure is written. */ Project 2 -- Analysis.
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.
Marc Smith and Jim Ten Eyck
Binary Search Trees Chapter 6.
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.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
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.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Kruse/Ryba ch101 Object Oriented Data Structures Binary Trees Binary Search Trees Height Balance:AVL Trees Splay Trees.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 10 Trees and Binary Trees Part 2. ?Traversal level by level.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
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.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
A Binary Search Tree Binary Search Trees.
External Searching: B-Trees Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
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.
Data Structures and Algorithms TREE. Searching Sequential Searches Time is proportional to n We call this time complexity O(n) Pronounce this “big oh”
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Chapter 11 Binary Trees Dr. Youssef Harrath
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 Lecture 33 Hash Tables.
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.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
BST Trees
Binary Search Tree Chapter 10.
Tree data structure.
Binary Tree Applications
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Chapter 20: Binary Trees.
Trees 3: The Binary Search Tree
Chapter 21: Binary Trees.
Tree data structure.
Data Structures CSCI 132, Spring 2014 Lecture 35 Binary Trees
Trees.
CMSC 341 Binary Search Trees.
Data Structures and Algorithms
CMSC 341 Binary Search Trees.
Mark Redekopp David Kempe
Data Structures and Algorithms
AVL-Trees (Part 2).
Presentation transcript:

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.

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 Binary_tree class template class Binary_tree { public: // Add methods here. protected: // Add auxiliary function prototypes here. Binary_node *root; };

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

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 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 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 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 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 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 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 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 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 Examples of 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

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