Binary Search Trees Chapter 7 Objectives

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Binary Search Trees Chapter 7 Objectives
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.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Lecture 17 Non-Linear data structures Richard Gesick.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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 Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
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)
Data Structures: A Pseudocode Approach with C, Second Edition1 Objectives Upon completion you will be able to: Explain the differences between a BST and.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
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.
Heap Chapter 9 Objectives Define and implement heap structures
Trees Chapter 15.
AA Trees.
Chapter 25 Binary Search Trees
Binary search tree. Removing a node
Binary Search Trees Chapter 7 Objectives
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Binary Search Tree (BST)
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Chapter 16 Tree Implementations
Binary Trees, Binary Search Trees
Chapter 7 TREES.
Heap Chapter 9 Objectives Upon completion you will be able to:
Chapter 16 Tree Implementations
Binary Search Trees.
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 7 Objectives
Chapter 20: Binary Trees.
AVL Tree Chapter 6 (cont’).
Non-Linear data structures
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Splay Trees Binary search trees.
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

A Binary Search Tree 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 or equal to the root. Each subtree is itself a binary search tree. Data Structures: A Pseudocode Approach with C, Second Edition

Basic Property In a binary search tree, the left subtree contains key values less than the root the right subtree contains key values greater than or equal to the root. 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

Data Structures: A Pseudocode Approach with C, Second Edition

(a), (b) - complete and balanced trees; (d) – nearly complete and balanced tree; (c), (e) – neither complete nor balanced trees Data Structures: A Pseudocode Approach with C, Second Edition

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

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Preorder Traversal 23 18 12 20 44 35 52 Data Structures: A Pseudocode Approach with C, Second Edition

Postorder Traversal 12 20 18 35 52 44 23 Data Structures: A Pseudocode Approach with C, Second Edition

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

Right-Node-Left Traversal 52 44 35 23 20 18 12 Right-node-left traversal of a binary search tree produces a descending sequence Data Structures: A Pseudocode Approach with C, Second Edition

Three BST search algorithms: Find the smallest node Find the largest node Find a requested node 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

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

BST Insertion To insert data all we need to do is follow the branches to an empty subtree and then insert the new node. In other words, all inserts take place at a leaf or at a leaflike node – a node that has only one null subtree. 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

30 30 30 30 Data Structures: A Pseudocode Approach with C, Second Edition

Deletion There are the following possible cases when we delete a node: The node to be deleted has no children. In this case, all we need to do is delete the node. The node to be deleted has only a right subtree. We delete the node and attach the right subtree to the deleted node’s parent. The node to be deleted has only a left subtree. We delete the node and attach the left subtree to the deleted node’s parent. The node to be deleted has two subtrees. It is possible to delete a node from the middle of a tree, but the result tends to create very unbalanced trees. Data Structures: A Pseudocode Approach with C, Second Edition

Deletion from the middle of a tree Rather than simply delete the node, we try to maintain the existing structure as much as possible by finding data to take the place of the deleted data. This can be done in one of two ways. Data Structures: A Pseudocode Approach with C, Second Edition

Deletion from the middle of a tree We can find the largest node in the deleted node’s left subtree and move its data to replace the deleted node’s data. We can find the smallest node on the deleted node’s right subtree and move its data to replace the deleted node’s data. Either of these moves preserves the integrity of the binary search tree. Data Structures: A Pseudocode Approach with C, Second Edition

Node to be removed has no children. Data Structures: A Pseudocode Approach with C, Second Edition

Node to be removed has one child. Data Structures: A Pseudocode Approach with C, Second Edition

Node to be removed has two children Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

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

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

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Homework: Preparation for the final test Chapter 6 (pp. 265-282) Chapter 7 (Sections 7.1; 7.2) p. 292: ex. 1; p. 293: ex. 6, ex. 12 P. 337: ex. 3, ex. 4, ex.6, ex. 7 P. 338: ex. 13, ex. 14 Data Structures: A Pseudocode Approach with C, Second Edition