Professional Documents
Culture Documents
Lab Manual
CPCS204
Data Structures 1
1433/1434H
Lab - 7
Learning Procedure
Laboratory 7:
Statement Purpose:
This lab will give you practice with Trees.
Activity Outcomes:
This lab teaches you the following topics:
Instructor Note:
As pre-lab activity, review Ch10 and Ch11, from the book Data Structures with
Java by John R. Hubbard and also the relevant instructor’s slides.
Names I.D.
1. .……………..………………………………. ………………………………
2. ..…………………………………………….. ………………………………
3. .……………………………………………... ………………………………
4. .…………………………………………….. ..…………………………….
1) Stage J (Journey)
Trees: tree is a nonlinear data structure that models a hierarchical
organization. The characteristic features are that each element may have
several successors (called its “children”) and every element except one (called
the “root”) has a unique predecessor (called its “parent”).
(Nick P., 2001) A Bnary Tree: is made of nodes, where each node contains a
"left" pointer, a "right" pointer, and a data element. The "root" pointer points
to the topmost node in the tree. The left and right pointers recursively point to
smaller "subtrees" on either side. A null pointer represents a binary tree with
no elements -- the empty tree.
The formal recursive definition is: a binary tree is either empty (represented by
a null pointer), or is made of a single node, where the left and right pointers
(recursive definition ahead) each point to a binary tree.
Simple example:
A "binary search tree" (BST) or "ordered binary tree" is a type of binary tree
where the nodes are arranged in order: for each node, all elements in its left
subtree are less-or-equal to the node (<=), and all the elements in its right
subtree are greater than the node (>). The tree shown above is a binary search
tree -- the "root" node is a 5, and its left subtree nodes (1, 3, 4) are <= 5, and
its right subtree nodes (6, 9) are > 5. Recursively, each of the subtrees must
also obey the binary search tree constraint: in the (1, 3, 4) subtree, the 3 is the
root, the 1 <= 3 and 4 > 3. Watch out for the exact wording in the problems -- a
"binary search tree" is different from a "binary tree".
The nodes at the bottom edge of the tree have empty subtrees and are called
"leaf" nodes (1, 4, 6) while the others are "internal" nodes (3, 5, 9).
Programing Strategy:
When thinking about a binary tree problem, it's often a good idea to draw a
few little trees to think about the various cases. For any problem related to
binary trees, there are two things to understand.
▪ The node/pointer structure that makes up the tree and the code that
manipulates it
▪ The algorithm, typically recursive, that iterates over the tree
Depth-first
Breadth-first
2) Stage a1 (apply)
Activity1:
Java Ordered Binary Tree Structure
Task 1: Create a new java project and add the BinaryTree.java and
TestBinaryTree.java below then complete the following tasks.
// BinaryTree.java
public class BinaryTree {
// Root node pointer. Will be null for an empty tree.
/*
--Node--
The binary tree is built using this nested node class.
Each node stores one data element, and has left and right
sub-tree pointer which may be null.
The node is a "dumb" nested class -- we just use it for
storage; it does not have any methods.
*/
public static class Node {
Node left;
Node right;
int data;
Node(int newData) {
left = null;
right = null;
data = newData;
}
}
/**
Creates an empty binary tree -- a null root pointer.
*/
public BinaryTree() {
root = null;
}
/**
Inserts the given data into the binary tree.
Uses a recursive helper.
*/
public void insert(int data) {
root = insert(root, data);
}
/**
Recursive insert -- given a node pointer, recur down and
insert the given data into the tree. Returns the new
node pointer (the standard way to communicate
a changed pointer back to the caller).
*/
private Node insert(Node node, int data) {
if (node == null) {
node = new Node(data);
} else {
CPCS204 – The Lab Note Lab-7 6
Term I
2013 Lab-7: Trees
/**
Returns the number of nodes in the tree.
Uses a recursive helper that recurs
down the tree and counts the nodes.
*/
public int size() {
return (size(root));
}
/**
Returns true if the given target is in the binary tree.
Uses a recursive helper.
*/
public boolean lookup(int data) {
return (lookup(root, data));
}
/**
Recursive lookup -- given a node, recur
down searching for the given data.
*/
private boolean lookup(Node node, int data) {
if (node == null) {
return (false);
}
if (data == node.data) {
return (true);
} else if (data < node.data) {
return (lookup(node.left, data));
} else {
return (lookup(node.right, data));
}
}
CPCS204 – The Lab Note Lab-7 7
Term I
2013 Lab-7: Trees
/**
Prints the node values in the "inOrder" order.
Uses a recursive helper to do the traversal.
*/
public void inOrder() {
inorderTree(root);
System.out.println();
}
/**
Prints the node values in the "preOrder" order.
Uses a recursive helper to do the traversal.
*/
public void preOrder() {
preOrder(root);
System.out.println();
}
/**
Prints the node values in the "postOrder" order.
Uses a recursive helper to do the traversal.
*/
public void postOrder() {
postOrder(root);
System.out.println();
}
}
CPCS204 – The Lab Note Lab-7 8
Term I
2013 Lab-7: Trees
/**
Prints the node values in the "levelOrder" order.
Uses a helper to do the traversal.
*/
public void levelOrder() {
levelOrder(root);
System.out.println();
}
if (node != null) {
while (q.size() != 0) {
if (currentNode.left != null) {
q.add(currentNode.left);
}
if (currentNode.right != null) {
q.add(currentNode.right);
}
}
}
}
/**
Changes the tree into its mirror image.
Uses a recursive helper that recurs over the tree,
swapping the left/right pointers.
*/
public void mirror() {
deleteNode(x, root);
}
n.data = findMin(n.right).data;
n.right= removeMinRight(n.right);
}
else{
//Alternative code line
System.out.println("Deleted: "+ n.data);
n = (n.left != null) ? n.left : n.right;
// if(n.left != null)
// n = n.left;
// else
// n = n.right;
}
}
return n;
}
return min;
}
CPCS204 – The Lab Note Lab-7 10
Term I
2013 Lab-7: Trees
Task 4: Write the code of a method public boolean isEmpty() that Tests
whether the tree has any nodes or not.
Task 5: Write the code of a method public Node root() that returns the
tree's root; an error occurs if the tree is empty.
Simple Algorithm:
postorder(node)
if node = null then return
postorder(node.left)
postorder(node.right)
print node.value
3) Stage v (verify)
Task 9: Add two lines of code to the main class TestBinaryTree.java to
search for the values 4 and 8 using the lookup(int data) method.
Task 10: Write the code of a method publicvoid mirror() that Uses a
recursive helper private void mirror(Node node) that recurs over the
tree, swapping the left/right pointers. In which it changes the tree into its
mirror image.
So the tree... is changed to...
4 4
/\ /\
2 5 5 2
/\ /\
1 3 3 1
Task 13: Add testing code to the main class TestBinaryTree.java in order
to check out your new deleting method deleteNodeWay2(int x) and
reprint the tree in level-order to see the difference after the deletion.
4) Stage a2 (assess)
Lab Work:
In each laboratory you are assessed on your work within lab session based on
your participation, discussions and achievement of lab activities. Thus, each lab
has a portion of the (LAB WORK MARK). Therefore, a checklist of each lab is
used to evaluate your work. This checklist accounts the following criteria: