Binary Search Trees Chapter 7 Objectives

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
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.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Binary Search Trees Chapter 7 Objectives
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
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.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
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/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
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.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary Search Trees Chapter 7 Objectives
Heap Chapter 9 Objectives Define and implement heap structures
Chapter 12 – Data Structures
Chapter 25 Binary Search Trees
Trees Chapter 11 (continued)
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 7 TREES.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 7 Objectives
CSC 143 Java Trees.
Chapter 20: Binary Trees.
AVL Tree Chapter 6 (cont’).
Trees.
Tree traversals BST properties Search Insertion
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Binary Search Trees Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree ADT Write application programs using the binary search tree ADT Design and implement a list using a BST Design and implement threaded trees Data Structures: A Pseudocode Approach with C, Second Edition

7-1 Basic Concepts Binary search trees provide an excellent structure for searching a list and at the same time for inserting and deleting data into the list. Data Structures: A Pseudocode Approach with C, Second Edition

Basic Concepts A binary search tree (BST) is a binary tree with the following properties: All items in the left subtree are less than the root. All items in the right subtree are greater than or equal to the root. Each subtree is itself a binary search tree. Data Structures: A Pseudocode Approach with C, Second Edition

Binary search tree Data Structures: A Pseudocode Approach with C, Second Edition

Valid binary search tree Data Structures: A Pseudocode Approach with C, Second Edition

Invalid binary search tree Data Structures: A Pseudocode Approach with C, Second Edition

7-2 BST Operations Traversals Searches Insertion Deletion We discuss four basic BST operations: traversal, search, insert, and delete; and develop algorithms for searches, insertion, and deletion. Traversals Searches Insertion Deletion Data Structures: A Pseudocode Approach with C, Second Edition

Example of a binary search tree Data Structures: A Pseudocode Approach with C, Second Edition

Traversals Preorder traversal Postorder traversal Inorder traversal 23 18 12 20 44 35 52 Postorder traversal 12 20 18 35 52 44 23 Inorder traversal 12 18 20 23 35 44 52 The inder traversal of a binary search tree produces a sequenced list Data Structures: A Pseudocode Approach with C, Second Edition

Traversals What happens if you traverse the tree using a right-node-left sequence? 52 44 35 23 20 18 12 Data Structures: A Pseudocode Approach with C, Second Edition

Searches Three search algorithms: Find the smallest node Find the largest node Find a requested node(BST search) Data Structures: A Pseudocode Approach with C, Second Edition

Find the smallest node Data Structures: A Pseudocode Approach with C, Second Edition

Find the smallest node Data Structures: A Pseudocode Approach with C, Second Edition

Find the largest node right subtree not empty right subtree not empty right subtree empty return Data Structures: A Pseudocode Approach with C, Second Edition

Find the largest node Data Structures: A Pseudocode Approach with C, Second Edition

BST and the binary serch Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Insertion All BST insertions take place at a leaf or a leaflike node. A node that has only one null subtree. Data Structures: A Pseudocode Approach with C, Second Edition

BST Insertion Data Structures: A Pseudocode Approach with C, Second Edition

BST Insertion Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Trace of recursive BST insert Data Structures: A Pseudocode Approach with C, Second Edition

Deletion To delete a node from a binary search tree, we must first locate it. There are four possible cases when we delete a node. The node has no children. has only a right subtree. has only a left subtree. has two subtrees. Data Structures: A Pseudocode Approach with C, Second Edition

Four cases when we delete a node The node has no children. Just delete the node The node has only a right subtree. delete the node attach the right subtree to the deleted node’s parent. The node has only a left subtree. attach the left subtree to the deleted node’s parent. Data Structures: A Pseudocode Approach with C, Second Edition

Four cases when we delete a node The node has two subtrees. Find the largest node in the deleted node’s left subtree and move its data to replace the deleted node’s data or Find the smallest node in the deleted node’s right subtree and move its data to replace the deleted node’s data. Data Structures: A Pseudocode Approach with C, Second Edition

/* dltKey = root */ Data Structures: A Pseudocode Approach with C, Second Edition

(continued) Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

7-3 Binary Search Tree ADT We begin this section with a discussion of the BST data structure and write the header file for the ADT. We then develop 14 programs that we include in the ADT. Data Structure Algorithms Data Structures: A Pseudocode Approach with C, Second Edition

BST ADT design Data Structures: A Pseudocode Approach with C, Second Edition

BST tree data structure Data Structures: A Pseudocode Approach with C, Second Edition

BST tree operations BST_Create BST_Insert BST_Delete BST_Retrieve BST_Traverse BST_Count BST_Full BST_Empty BST_Destroy Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

BST tree ADT operations BST_TREE BST_Create(int (*compare) (void* argu1 void* argu2)) Allocates dynamic memory for an BST tree head node and returns its address to caller. compare is address of compare function used when two node need to be compared. Return a head node pointer; null if overflow BST_TREE* BST_Destroy( BST_TREE* tree ) Delete all data in tree and recycles memory. tree is pointer to BST tree structure Returns null head pointer. Data Structures: A Pseudocode Approach with C, Second Edition

BST tree ADT operations bool BST_Insert( BST_TREE* tree, void* dataPtr ) Inserts new data into the tree. tree is pointer to BST tree structure Return success (true) or overflow (false) bool BST_Delete( BST_TREE* tree, void* dataPtr ) Deletes a node from the tree and rebalances it if necessary. Return success (true) or Not found (false) Data Structures: A Pseudocode Approach with C, Second Edition

BST tree ADT operations void* BST_Retrieve( BST_TREE* tree, void* dataPtr ) Retrieve node searches tree for the node containing the requested key and returns pointer to its data. tree is pointer to BST tree structure Return the address of matching node. If not found, return NULL. void BST_Traverse( BST_TREE* tree, void (*process) (void* dataPtr) ) Process tree using inorder traversal process is address of process function used to visit a node during. Data Structures: A Pseudocode Approach with C, Second Edition

BST tree ADT operations void* BST_Empty( BST_TREE* tree ) Returns true if tree is empty; false if any data. tree is pointer to BST tree structure Returns true if there empty, false if any data. bool BST_Full( BST_TREE* tree ) If there is no room for another node, returns true. Returns true if no room for another insert; false if room. int BST_Count( BST_TREE* tree ) Returns number of nodes in tree. Returns tree count. Data Structures: A Pseudocode Approach with C, Second Edition

7-4 BST Applications Integer Application Student List Application This section develops two applications that use the BST ADT. We begin the discussion of BST Applications with a simple application that manipulates integers. The second application, student list, requires a structure to hold the student's data. Integer Application Student List Application Data Structures: A Pseudocode Approach with C, Second Edition

Integer Application The BST tree integer application reads integers from the keyboard and inserts them into the BST. Data Structures: A Pseudocode Approach with C, Second Edition

Insertions into a BST 18, 33 Data Structures: A Pseudocode Approach with C, Second Edition

compare process Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition