Download presentation
Presentation is loading. Please wait.
1
Chapter 16 Tree Implementations
CS Data Structures Mehmet H Gunes Modified from authors’ slides
2
Contents The Nodes in a Binary Tree
A Link-Based Implementation of the ADT Binary Tree A Link-Based Implementation of the ADT Binary Search Tree Saving a Binary Search Tree in a File Tree Sort General Trees Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
3
Array-Based Representation
Consider required data members View class TreeNode, Listing 16-1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
4
Array-Based Representation
(a) A binary tree of names; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
5
Array-Based Representation
(b) its implementation using the array tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
6
Link-Based Representation
Listing 16-2 shows the class BinaryNode for a link-based implementation A link-based implementation of a binary tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
7
Link-Based Implementation
View header file for the link-based implementation of the class BinaryNodeTree, Listing 16-3 Note significant portions of the implementation file, Listing 16-A Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
8
Link-Based Implementation
Adding nodes to an initially empty binary tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
9
Link-Based Implementation
Adding nodes to an initially empty binary tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
10
Link-Based Implementation
Avoiding returns to nodes B and C View pseudocode for non recursive traversal, Listing 16-B Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
11
Algorithms for the ADT Binary Search Tree Operations
(a) A binary search tree; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
12
Algorithms for the ADT Binary Search Tree Operations
(b) empty subtree where the search algorithm terminates when looking for Kody Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
13
Algorithms for the ADT Binary Search Tree Operations
(c) the tree after Kody is inserted as a new leaf Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
14
Algorithms for the ADT Binary Search Tree Operations
(a) Insertion into an empty tree; (b) search for Frank terminates at a leaf; (c) insertion at a leaf Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
15
Algorithms for the ADT Binary Search Tree Operations
(a) N with only a left child— N can be either the left child or right child of P ; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
16
Algorithms for the ADT Binary Search Tree Operations
(b) after removing node N Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
17
Algorithms for the ADT Binary Search Tree Operations
N with two children Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
18
Algorithms for the ADT Binary Search Tree Operations
(a) Not any node will do; (b) no longer a binary search tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
19
Algorithms for the ADT Binary Search Tree Operations
Search key x can be replaced by y Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
20
Algorithms for the ADT Binary Search Tree Operations
Copying the item whose search key is the inorder successor of N ’s search key View final draft of remove algorithm, Listing 16-C Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
21
Algorithms for the ADT Binary Search Tree Operations
Recursive deletion of node N Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
22
Algorithms for the ADT Binary Search Tree Operations
Recursive deletion of node N Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
23
Algorithms for the ADT Binary Search Tree Operations
Recursive deletion of node N Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
24
Algorithms for the ADT Binary Search Tree Operations
findNode as a refinement of search Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
25
The Class BinarySearchTree
View header file for the link-based implementation of the class BinarySearchTree, Listing 16-4 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013`
26
Saving Binary Search Tree in a File
An initially empty binary search tree after the insertion of 60, 20, 10, 40, 30, 50, and 70 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
27
Saving Binary Search Tree in a File
Recursive algorithm to create full binary search tree with n nodes Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
28
Saving Binary Search Tree in a File
A full tree saved in a file by using inorder traversal Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
29
Saving Binary Search Tree in a File
A tree of minimum height that is not complete Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
30
Saving Binary Search Tree in a File
Building a minimum height binary search tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
31
Tree Sort Algorithm for tree sort
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
32
General Trees A general tree
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
33
General Trees (a) A link-based implementation of the general tree in Figure ; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
34
General Trees (b) the binary tree that part a represents
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
35
General Trees An implementation of the n -ary tree in Figure 16-18
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.