Chapter16 TreeImplementation
Chapter16 TreeImplementation
Tree Implementations
CENG 214
Algorithms &
Data Structures 2
Nodes in a Binary Tree
• Representing tree nodes
– 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
Alg.-2 2/85
Array-Based Representation
• Class of array-based data members
Alg.-2 3/85
Array-Based Representation
• As tree changes (additions, removals) …
– Nodes may not be in contiguous array elements
Alg.-2 4/85
Array-Based Representation
Alg.-2 6/85
Link-Based Representation
BinaryNodeTree<string> tree1;
BinaryNodeTree<string>* tree2Ptr = new BinaryNodeTree<string>("A");
BinaryNodeTree<string>* tree3Ptr = new BinaryNodeTree<string>("B");
BinaryNodeTree<string>* tree4Ptr =
new BinaryNodeTree<string>("C", tree2Ptr, tree3Ptr);
Alg.-2 10/85
The Header File
Alg.-2 17/85
The Implementation
The constructors: The public constructors have the following
definitions in the implementation file:
Alg.-2 18/85
The Implementation
Copy constructor
Alg.-2 20/85
The Implementation
• Method add
Alg.-2 23/85
balancedAdd method
template<class ItemType>
auto BinaryNodeTree<ItemType>::balancedAdd(std::shared_ptr<BinaryNode<ItemType>>
subTreePtr, std::shared_ptr<BinaryNode<ItemType>> newNodePtr)
{
if (subTreePtr == nullptr)
return newNodePtr;
else
{
auto leftPtr = subTreePtr->getLeftChildPtr();
auto rightPtr = subTreePtr->getRightChildPtr();
return subTreePtr;
} // end if
} // end balancedAdd
Alg.-2 24/85
The Implementation
20 20 30 20 30
40
10 10
10
20 30 30
20 20 30
40 60 50
40 80
50 40 60 50
70
Alg.-2 27/85
The traversals
• Since the traversals are recursive, the public
traversal methods each call a protected
method that performs the actual recursion.
• inorder has the function visit which enables
the client not only to examine the item but
also to modify it,
• The second parameter of inorder is the pointer
treePtr which, due to the recursive calls,
eventually points to every node in the tree.
Alg.-2 28/85
A Nonrecursive traversal
• Nonrecursive traversal: Before leaving the topic of
traversals, let’s develop a nonrecursive traversal
algorithm to illustrate further the relationship between
stacks and recursion that was discussed in Stack (Ch.6).
• The definition of inorder follows;
Alg.-2 29/85
The Implementation, recursive
Alg.-2 32/85
The Implementation
Alg.-2 33/85
A Nonrecursive traversal
2 facts emerge from the recursive version of inorder when a
return is made from a recursive call:
I. The implicit recursive stack of pointers is used to find the
node p that the traversal must go back to.
II. Once the traversal backs up to node p , it either visits p or
backs farther up the tree. It visits p if p ’s left subtree has just
been traversed; it backs up if its right subtree has just been
traversed.
You could directly mimic this action by using an iterative method
and an explicit stack, as long as some bookkeeping device kept
track of which subtree of a node had just been traversed.
Good exam question
Alg.-2 34/85
A Nonrecursive traversal
This strategy of not returning to a node after its right
subtree has been traversed is simple to implement:
You place a pointer to a node in the stack only before
the node’s left subtree is traversed, but not before its
right subtree is traversed.
Alg.-2 35/85
The Implementation
Avoiding
returns to
nodes B and C
Alg.-2 36/85
The Implementation
Alg.-2 40/85
Link-Based Implementation of the ADT Binary Search Tree
Adding a new entry: Insert a new entry into a binary search tree
in the same place. Ex: Adding Kody to a binary search tree
Alg.-2 41/85
Link-Based Implementation of the ADT Binary Search Tree
• Method add
– The method creates a new node and passes pointers to the
tree and the new node to a recursive method that actually
performs the insertion.
Alg.-2 42/85
Link-Based Implementation of the ADT Binary Search Tree
Alg.-2 43/85
Link-Based Implementation of the ADT Binary Search Tree
Alg.-2 46/85
Link-Based Implementation of the ADT Binary Search Tree
• N is a leaf
• N has only one child
• N has two children
Alg.-2 47/85
Link-Based Implementation of the ADT Binary Search Tree
1. N is a leaf
– Remove leaf containing target
– Set pointer in parent to nullptr
Alg.-2 48/85
Link-Based Implementation of the ADT Binary Search Tree
Alg.-2 49/85
Link-Based Implementation of the ADT Binary Search Tree
Alg.-2 52/85
Link-Based Implementation of the ADT Binary Search Tree
Alg.-2 54/85
Link-Based Implementation of the ADT Binary Search Tree
Alg.-2 55/85
Link-Based Implementation of the ADT Binary Search Tree
You can copy into N either the item that is immediately after N
’s entry or the item that is immediately before it.
Alg.-2 56/85
Link-Based Implementation of the ADT Binary Search Tree
Inorder successor of N’s entry is in the leftmost node in N’s right subtree
Alg.-2 57/85
Link-Based Implementation of the ADT Binary Search Tree
Alg.-2 60/85
Link-Based Implementation of the ADT Binary Search Tree
Alg.-2 65/85
Link-Based Implementation of the ADT Binary Search Tree
Alg.-2 66/85
The Class BinarySearchTree (BST)
The Class BinarySearchTree
Alg.-2 74/85
Saving a Binary Search Tree in a File
• Saving a binary search tree and then restoring it to
a balanced shape: After you save a binary search
tree in a file, do you necessarily want the restored
tree to have its original shape? Maybe or not?
o Recall that you can use a given set of data items to create
several binary search trees with different shapes.
o Although the shape of a binary search tree has no effect
whatsoever on the correctness of the ADT operations, it
will affect the efficiency of those operations.
o Efficient operations are ensured if the binary search tree is
balanced. Balanced binary search tree increases efficiency
of ADT operations
Alg.-2 75/85
Saving a Binary Search Tree in a File
Building a minimum-height full binary search tree;
• A full tree with exactly n = 2 h – 1 nodes for some height h has
the exact middle of the data items in its root.
if (n > 0) {
treePtr = pointer to new node with nullptr as its child
pointers
Alg.-2 76/85
Saving a Binary Search Tree in a File
• Building a minimum-height binary search tree
Alg.-2 77/85
Saving a Binary Search Tree in a File
A full tree saved in a file by using inorder traversal,
– If you save a full tree in a file by using an inorder traversal, the
file will be in sorted order,
40
20 60
10, 20, 30, 40, 50, 60, 70
File
10 30 50 70
Alg.-2 78/85
Saving a Binary Search Tree in a File
• A tree of minimum height that is not complete
Alg.-2 79/85
Saving a Binary Search Tree in a File
Question. Consider the pseudocode operation readTree.
a. What binary search tree results when you execute readTree
with a file of the six integers 2, 4, 6, 8, 10, 12?
b. Is the resulting tree’s height a minimum? Is the tree complete?
Is it full?
Alg.-2 80/85
Tree Sort
• Tree sort uses a binary search tree (bst).
Alg.-2 81/85
General Trees
• A general tree or an n-ary tree with n = 3
Alg.-2 82/85
General Trees
(b) the
binary tree that part
a represents
Alg.-2 85/85