Download presentation
Presentation is loading. Please wait.
1
Chapter 16 Tree Implementations
CS Data Structures Mehmet H Gunes Modified from authors’ slides
2
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
3
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
4
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
5
Array-Based Representation
The class TreeNode for an array-based implementation of the ADT binary tree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
6
Array-Based Representation
(a) A binary tree of names; (b) its implementation using the array tree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
7
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
8
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
9
Link-Based Representation
A link-based implementation of a binary tree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
10
The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
11
The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
12
The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
13
The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
14
The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
15
The Header File A header file for the link-based implementation of the class BinaryNodeTree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
16
The Implementation Constructors
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
17
The Implementation Constructors
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
18
The Implementation Protected method copyTree called by copy constructor © 2017 Pearson Education, Hoboken, NJ. All rights reserved
19
The Implementation Copy constructor
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
20
The Implementation destroyTree used by destructor which simply calls this method © 2017 Pearson Education, Hoboken, NJ. All rights reserved
21
The Implementation Protected method getHeightHelper
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
22
The Implementation Method add
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
23
The Implementation Adding nodes to an initially empty binary tree
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
24
The Implementation Adding nodes to an initially empty binary tree
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
25
The Implementation Protected method that enables recursive traversals.
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
26
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
27
The Implementation Steps during an inorder traversal of the subtrees of 20 © 2017 Pearson Education, Hoboken, NJ. All rights reserved
28
The Implementation Steps during an inorder traversal of the subtrees of 20 © 2017 Pearson Education, Hoboken, NJ. All rights reserved
29
The Implementation Avoiding returns to nodes B and C
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
30
The Implementation Nonrecursive inorder traversal
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
31
The Implementation Nonrecursive inorder traversal
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
32
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 Section is basis for operations © 2017 Pearson Education, Hoboken, NJ. All rights reserved
33
Link-Based Implementation of the ADT Binary Search Tree
Adding Kody to a binary search tree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
34
Link-Based Implementation of the ADT Binary Search Tree
Method add © 2017 Pearson Education, Hoboken, NJ. All rights reserved
35
Link-Based Implementation of the ADT Binary Search Tree
Refinement of addition algorithm © 2017 Pearson Education, Hoboken, NJ. All rights reserved
36
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
37
Link-Based Implementation of the ADT Binary Search Tree
First draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ. All rights reserved
38
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
39
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
40
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
41
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
42
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
43
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
44
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
45
Link-Based Implementation of the ADT Binary Search Tree
Not any node will do © 2017 Pearson Education, Hoboken, NJ. All rights reserved
46
Link-Based Implementation of the ADT Binary Search Tree
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
47
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
48
Link-Based Implementation of the ADT Binary Search Tree
Final draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ. All rights reserved
49
Link-Based Implementation of the ADT Binary Search Tree
Final draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ. All rights reserved
50
Link-Based Implementation of the ADT Binary Search Tree
Final draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ. All rights reserved
51
Link-Based Implementation of the ADT Binary Search Tree
Final draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ. All rights reserved
52
Link-Based Implementation of the ADT Binary Search Tree
Final draft of the removal algorithm © 2017 Pearson Education, Hoboken, NJ. All rights reserved
53
Link-Based Implementation of the ADT Binary Search Tree
Recursive removal of node N © 2017 Pearson Education, Hoboken, NJ. All rights reserved
54
Link-Based Implementation of the ADT Binary Search Tree
Recursive removal of node N © 2017 Pearson Education, Hoboken, NJ. All rights reserved
55
Link-Based Implementation of the ADT Binary Search Tree
Recursive removal of node N © 2017 Pearson Education, Hoboken, NJ. All rights reserved
56
Link-Based Implementation of the ADT Binary Search Tree
Algorithm for findNode © 2017 Pearson Education, Hoboken, NJ. All rights reserved
57
The Class BinarySearchTree
A header file for the link-based implementation of the class BinarySearchTree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
58
The Class BinarySearchTree
A header file for the link-based implementation of the class BinarySearchTree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
59
The Class BinarySearchTree
A header file for the link-based implementation of the class BinarySearchTree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
60
The Class BinarySearchTree
A header file for the link-based implementation of the class BinarySearchTree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
61
The Class BinarySearchTree
A header file for the link-based implementation of the class BinarySearchTree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
62
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
63
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
64
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
65
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
66
Saving a Binary Search Tree in a File
Building a minimum-height binary search tree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
67
Saving a Binary Search Tree in a File
Building a minimum-height binary search tree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
68
Tree Sort Tree sort uses a binary search tree.
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
69
General Trees A general tree or an n-ary tree with n = 3
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
70
General Trees An implementation of a general tree and its equivalent binary tree © 2017 Pearson Education, Hoboken, NJ. All rights reserved
71
General Trees An implementation of the n-ary tree
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.