Chapter 16 Tree Implementations

Slides:



Advertisements
Similar presentations
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Advertisements

© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
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.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
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.
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.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
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.
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.
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.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Binary Search Trees Chapter 7 Objectives
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Non Linear Data Structure
Trees Chapter 15.
Data Structure and Algorithms
Balanced Search Trees Modified from authors’ slides.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
A Binary Search Tree Implementation
CS 3013 Binary Search Trees.
Binary search tree. Removing a node
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
CS 302 Data Structures Trees.
Processing Data in External Storage
Binary Search Tree Chapter 10.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 16 Tree Implementations
CS 3013 Binary Search Trees.
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Chapter 20: Binary Trees.
Ch. 11 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.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 12 Sorted Lists and their Implementations
Chapter 14: Queue and Priority Queue Implementations
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Binary Search Trees.
Chapter 7 Implementations of the ADT Stack
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees Chapter 10.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Search Trees Chapter 7 Objectives
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Tree Implementation And Applications
A Binary Search Tree Implementation
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
COSC2007 Data Structures II
Presentation transcript:

Chapter 16 Tree Implementations CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Nodes in a Binary Tree Representing tree nodes Array-based Link-based Must contain both data and “pointers” to node’s children Each node will be an object Array-based Pointers will be array indices Link-based Use C++ pointers © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Array-Based Representation Class of array-based data members Variable root is index to tree’s root node within the array tree If tree is empty, root = -1 © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Array-Based Representation As tree changes (additions, removals) … Nodes may not be in contiguous array elements Thus, need list of available nodes Called a free list Node removed from tree Placed in free list for later use © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Array-Based Representation The class TreeNode for an array-based implementation of the ADT binary tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Array-Based Representation (a) A binary tree of names; (b) its implementation using the array tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Representation The header file containing the class BinaryNode for a link-based implementation of the ADT binary tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Representation The header file containing the class BinaryNode for a link-based implementation of the ADT binary tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Representation A link-based implementation of a binary tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Constructors © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Constructors © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Protected method copyTree called by copy constructor © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Copy constructor © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation destroyTree used by destructor which simply calls this method © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Protected method getHeightHelper © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Method add © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Adding nodes to an initially empty binary tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Adding nodes to an initially empty binary tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Protected method that enables recursive traversals. © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Contents of the implicit stack as treePtr progresses through a given tree during a recursive inorder traversal © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Steps during an inorder traversal of the subtrees of 20 © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Steps during an inorder traversal of the subtrees of 20 © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Avoiding returns to nodes B and C © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Nonrecursive inorder traversal © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Nonrecursive inorder traversal © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Uses same node objects as for binary-tree implementation Class BinaryNode from Listing16-2 will be used Recursive search algorithm from Section15.3.2 is basis for operations © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Adding Kody to a binary search tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Method add © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Refinement of addition algorithm © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Adding new data to a binary search tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree First draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Cases for node N containing item to be removed N is a leaf Remove leaf containing target Set pointer in parent to nullptr © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Cases for node N containing item to be removed N has only left (or right) child – cases are symmetrical After N removed, all data items rooted at L (or R) are adopted by root of N All items adopted are in correct order, binary search tree property preserved © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Cases for node N containing item to be removed N has two children Locate another node M easier to remove from tree than N Copy item that is in M to N Remove M from tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Case 2 for removeValue: The data item to remove is in a node N that has only a left child and whose parent is node P © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Case 2 for removeValue: The data item to remove is in a node N that has only a left child and whose parent is node P © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Case 2 for removeValue: The data item to remove is in a node N that has only a left child and whose parent is node P © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Case 3: The data item to remove is in a node N that has two children © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Not any node will do © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Replacing the data item in node N with its inorder successor © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Final draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Final draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Final draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Final draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Final draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Recursive removal of node N © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Recursive removal of node N © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Recursive removal of node N © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT Binary Search Tree Algorithm for findNode © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Class BinarySearchTree A header file for the link-based implementation of the class BinarySearchTree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Class BinarySearchTree A header file for the link-based implementation of the class BinarySearchTree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Class BinarySearchTree A header file for the link-based implementation of the class BinarySearchTree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Class BinarySearchTree A header file for the link-based implementation of the class BinarySearchTree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Class BinarySearchTree A header file for the link-based implementation of the class BinarySearchTree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Saving a Binary Search Tree in a File An initially empty binary search tree after the addition of 60, 20, 10, 40, 30, 50, and 70 © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Saving a Binary Search Tree in a File Use preorder traversal to save binary search tree in a file Restore to original shape by using method add Balanced binary search tree increases efficiency of ADT operations © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Saving a Binary Search Tree in a File A full tree saved in a file by using inorder traversal © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Saving a Binary Search Tree in a File A tree of minimum height that is not complete © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Saving a Binary Search Tree in a File Building a minimum-height binary search tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Saving a Binary Search Tree in a File Building a minimum-height binary search tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Tree Sort Tree sort uses a binary search tree. © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

General Trees A general tree or an n-ary tree with n = 3 © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

General Trees An implementation of a general tree and its equivalent binary tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

General Trees An implementation of the n-ary tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved