Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.

Slides:



Advertisements
Similar presentations
CSC 205 – Java Programming II Lecture 35 April 17, 2002.
Advertisements

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 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.
Marc Smith and Jim Ten Eyck
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Advanced Implementation of Tables.
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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
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.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Discrete Mathematics Chapter 5 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.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Data Structure and Algorithms
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
CS 302 Data Structures Trees.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Section 8.1 Trees.
External Methods Chapter 15 (continued)
Chapter 16 Tree Implementations
Chapter 20: Binary Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Chapter 21: Binary Trees.
Binary Search Trees < > =
Chapter 16 Tree Implementations
Advanced Implementation of Tables
Binary Trees, Binary Search Trees
Trees Chapter 10.
Binary Search Trees Chapter 7 Objectives
Trees.
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
CSC 205 – Java Programming II
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
COSC2007 Data Structures II
Presentation transcript:

Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved

The ADT Binary Search Tree A deficiency of the ADT binary tree which is corrected by the ADT binary search tree Searching for a particular item Each node n in a binary search tree satisfies the following properties n’s value is greater than all values in its left subtree TL n’s value is less than all values in its right subtree TR Both TL and TR are binary search trees © 2011 Pearson Addison-Wesley. All rights reserved

The ADT Binary Search Tree Record A group of related items, called fields, that are not necessarily of the same data type Field A data element within a record A data item in a binary search tree has a specially designated search key A search key is the part of a record that identifies it within a collection of records © 2011 Pearson Addison-Wesley. All rights reserved

The ADT Binary Search Tree Operations of the ADT binary search tree Insert a new item into a binary search tree Delete the item with a given search key from a binary search tree Retrieve the item with a given search key from a binary search tree Traverse the items in a binary search tree in preorder, inorder, or postorder Figure 11-19 A binary search tree © 2011 Pearson Addison-Wesley. All rights reserved

Exercise Given a sequence of numbers: 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31 Draw a binary search tree by inserting the above numbers from left to right. © 2011 Pearson Addison-Wesley. All rights reserved

Exercise Given a sequence of numbers: 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31 Draw a binary search tree by inserting the above numbers from left to right. © 2011 Pearson Addison-Wesley. All rights reserved

Algorithms for the Operations of the ADT Binary Search Tree Since the binary search tree is recursive in nature, it is natural to formulate recursive algorithms for its operations A search algorithm search(bst, searchKey) Searches the binary search tree bst for the item whose search key is searchKey © 2011 Pearson Addison-Wesley. All rights reserved

Algorithms for the Operations of the ADT Binary Search Tree: Insertion insertItem(treeNode, newItem) Inserts newItem into the binary search tree of which treeNode is the root Figure 11-23a and 11-23b a) Insertion into an empty tree; b) search terminates at a leaf © 2011 Pearson Addison-Wesley. All rights reserved

Algorithms for the Operations of the ADT Binary Search Tree: Insertion Figure 11-23c c) insertion at a leaf © 2011 Pearson Addison-Wesley. All rights reserved

Algorithms for the Operations of the ADT Binary Search Tree: Deletion Steps for deletion Use the search algorithm to locate the item with the specified key If the item is found, remove the item from the tree Three possible cases for node N containing the item to be deleted N is a leaf N has only one child N has two children © 2011 Pearson Addison-Wesley. All rights reserved

Algorithms for the Operations of the ADT Binary Search Tree: Deletion Strategies for deleting node N If N is a leaf Set the reference in N’s parent to null If N has only one child Let N’s parent adopt N’s child If N has two children Locate another node M that is easier to remove from the tree than the node N Copy the item that is in M to N Remove the node M from the tree © 2011 Pearson Addison-Wesley. All rights reserved

Algorithms for the Operations of the ADT Binary Search Tree: Traversal Traversals for a binary search tree are the same as the traversals for a binary tree Theorem 11-1 The inorder traversal of a binary search tree T will visit its nodes in sorted search-key order Inorder Traversal is sorted !! © 2011 Pearson Addison-Wesley. All rights reserved

The Efficiency of Binary Search Tree Operations The maximum number of comparisons for a retrieval, insertion, or deletion is the height of the tree The maximum and minimum heights of a binary search tree n is the maximum height of a binary tree with n nodes Figure 11-30 A maximum-height binary tree with seven nodes © 2011 Pearson Addison-Wesley. All rights reserved

The Efficiency of Binary Search Tree Operations Theorem 11-2 A full binary tree of height h  0 has 2h-1 nodes Theorem 11-3 The maximum number of nodes that a binary tree of height h can have is 2h – 1 Figure 11-32 Counting the nodes in a full binary tree of height h © 2011 Pearson Addison-Wesley. All rights reserved

The Efficiency of Binary Search Tree Operations Theorem 11-4 The minimum height of a binary tree with n nodes is log2(n+1) The height of a particular binary search tree depends on the order in which insertion and deletion operations are performed Figure 11-34 The order of the retrieval, insertion, deletion, and traversal operations for the reference-based implementation of the ADT binary search tree © 2011 Pearson Addison-Wesley. All rights reserved

General Trees An n-ary tree A generalization of a binary tree whose nodes each can have no more than n children Figure 11-38 A general tree Figure 11-41 An implementation of the n-ary tree in Figure 11-38 © 2011 Pearson Addison-Wesley. All rights reserved

Summary Binary trees provide a hierarchical organization of data Implementation of binary trees The implementation of a binary tree is usually referenced-based If the binary tree is complete, an efficient array-based implementation is possible Traversing a tree is a useful operation The binary search tree allows you to use a binary search-like algorithm to search for an item with a specified value © 2011 Pearson Addison-Wesley. All rights reserved

Summary Binary search trees come in many shapes The height of a binary search tree with n nodes can range from a minimum of log2(n + 1) to a maximum of n The shape of a binary search tree determines the efficiency of its operations An inorder traversal of a binary search tree visits the tree’s nodes in sorted search-key order © 2011 Pearson Addison-Wesley. All rights reserved