Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Sacasas, Elizalde

ITC C202 - 201DA

Midterms Lab Exercise 4: Binary Search Tree Application

REQUIREMENTS: Preorder Traversal

Input/s: Any value Inserted in BinaryTree tree = new BinaryTree();

Output/s: Preorder Traversal of the BinaryTree

DESIGN: Algorithm Preorder (Pseudocode)

1. Visit the root

2. Traverse the left subtree, i.e., call Preorder (left->subtree)

3. Traverse the right subtree, i.e., call Preorder(right->subtree)

SOURCE CODE:

package MLE4;

class Node {
int key;
Node left, right;

public Node(int item)


{
key = item;
left = right = null;
}
}

class BinaryTree {

Node root;

BinaryTree() { root = null; }


void printPreorder(Node node)
{
if (node == null)
return;

System.out.print(node.key + " ");

printPreorder(node.left);

printPreorder(node.right);
}

void printPreorder() { printPreorder(root); }

public static void main(String[] args)


{
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);

System.out.println(
"Preorder traversal of binary tree is ");
tree.printPreorder();
}
}

SCREENSHOTS:
REQUIREMENTS:

Inorder Traversal

Input/s:  Any value inserted in BinaryTree tree = new BinaryTree();

Output/s: Inorder Traversal of the BinaryTree

DESIGN:

Algorithm Inorder (Pseudocode)

1. Traverse the left subtree, i.e., call Inorder(left->subtree)

2. Visit the root.

3. Traverse the right subtree, i.e., call Inorder(right->subtree)

SOURCE CODE:

package MLE4;

class Node {
int key;
Node left, right;

public Node(int item)


{
key = item;
left = right = null;
}
}

class BinaryTree {

Node root;

BinaryTree() { root = null; }


void printInorder(Node node)
{
if (node == null)
return;

printInorder(node.left);

System.out.print(node.key + " ");

printInorder(node.right);
}

void printInorder() { printInorder(root); }

public static void main(String[] args)


{
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);

System.out.println(
"Inorder traversal of binary tree is");
tree.printInorder();
}
}

SCREENSHOTS:
REQUIREMENTS:

Postorder Traversal

Input/s:  Any value Inserted in BinaryTree tree = new BinaryTree();

Output/s: Postorder Traversal of the BinaryTree

DESIGN:

Algorithm Postroder(Pseudocode)

1.Traverse the left subtree, i.e., call Postorder(left->subtree)

2.Traverse the right subtree, i.e., call Postorder(right->subtree)

3. Visit the root

SOURCE CODE:

package MLE4;

class Node {
int key;
Node left, right;

public Node(int item)


{
key = item;
left = right = null;
}
}

class BinaryTree {
Node root;

BinaryTree() { root = null; }

void printPostorder(Node node)


{
if (node == null)
return;

printPostorder(node.left);

printPostorder(node.right);

System.out.print(node.key + " ");


}

void printPostorder() { printPostorder(root); }

public static void main(String[] args)


{
BinaryTree tree = new BinaryTree();
tree.root = new Node(4);
tree.root.left = new Node(2);
tree.root.right = new Node(1);
tree.root.left.left = new Node(3);
tree.root.left.right = new Node(5);

System.out.println(
"Postorder traversal of binary tree is ");
tree.printPostorder();
}
}

SCREENSHOTS:

You might also like