0% found this document useful (0 votes)
25 views9 pages

SE214 - Lab8 - Fall2024.pdf by Karam

Uploaded by

karamict8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views9 pages

SE214 - Lab8 - Fall2024.pdf by Karam

Uploaded by

karamict8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

College of Engineering

Software Engineering Department

SE 214: Data Structures and Algorithms


Fall 2024

Lab 8 – Binary Search Tree and Tree Traversals

Name: karam Date:10/27/2024


Student ID:240419 Time and day:

Objectives:

1. Review the concept of Binary Trees.


2. Implementation of a Binary Tree.
3. Convert implementation of structure into algorithm.
4. Implementation of Tree Traversals (pre-order, in-order and post-order)

For all Exercises:


1. Submit your Java files on Moodle.
2. Paste the screenshot of your output.

Exercise 1: Binary Search Tree (15 Marks)

A. Write BinarySearchTree Class which has attribute “root” and have following methods:
1. Zero argument constructors
2. Find a method that takes an integer and returns true, if the integer is found in a tree.
3. Insert method that inserts the integer into its right place.
4. isEmpty method to see if a tree is empty.
5. Display method that displays the tree. You can display the tree as left, root, right.
6. Minimum method that searches for the smallest node in tree and return its data.
7. Maximum method that searches for the biggest node in tree and return its data.
8. Delete method that deletes a node with specific value.

B. Write a main test class for testing all the methods implemented in part A.
public class Node {
int data;
Node left,right;

public Node(int data){


this.data=data;
left=right=null;
}
}

public class BinarysearchTree {


public Node root;

// Zero argument constructor


public BinarysearchTree() {
root = null;
}

// Method to check if the tree is empty


public boolean isEmpty() {
return root == null;
}

// Method to insert a value


public void insert(int value) {
root = insertRec(root, value);
}

private Node insertRec(Node node, int value) {


if (node == null) {
return new Node(value);
}
if (value < node.data) {
node.left = insertRec(node.left, value);
} else if (value > node.data) {
node.right = insertRec(node.right, value);
}
return node;
}

// Method to find a value


public boolean find(int value) {
return findRec(root, value);
}

private boolean findRec(Node node, int value) {


if (node == null) {
return false;
}
if (value == node.data) {
return true;
} else if (value < node.data) {
return findRec(node.left, value);
} else {
return findRec(node.right, value);
}
}

// Method to display the tree in order


public void display() {
displayInOrder(root);
System.out.println();
}

private void displayInOrder(Node node) {


if (node != null) {
displayInOrder(node.left);
System.out.print(node.data + " ");
displayInOrder(node.right);
}
}

// Method to find the minimum value


public int minimum() {
if (root == null) {
throw new IllegalStateException("Tree is empty");
}
return minimumRec(root);
}

private int minimumRec(Node node) {


while (node.left != null) {
node = node.left;
}
return node.data;
}

// Method to find the maximum value


public int maximum() {
if (root == null) {
throw new IllegalStateException("Tree is empty");
}
return maximumRec(root);
}

private int maximumRec(Node node) {


while (node.right != null) {
node = node.right;
}
return node.data;
}

// Method to delete a node with a specific value


public void delete(int value) {
root = deleteRec(root, value);
}

private Node deleteRec(Node node, int value) {


if (node == null) {
return node;
}
if (value < node.data) {
node.left = deleteRec(node.left, value);
} else if (value > node.data) {
node.right = deleteRec(node.right, value);
} else {
// Node with one child or no child
if (node.left == null) {
return node.right;
} else if (node.right == null) {
return node.left;
}
// Node with two children: Get the inorder successor
node.data = minimumRec(node.right);
node.right = deleteRec(node.right, node.data);
}
return node;
}

public class Lab8214 {

public static void main(String[] args) {


BinarysearchTree bst = new BinarysearchTree();

// Test isEmpty
System.out.println("Is tree empty? " + bst.isEmpty());

// Insert values
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(3);
bst.insert(7);
bst.insert(12);
bst.insert(18);

// Test display
System.out.print("Tree elements in order: ");
bst.display();
// Test find method
System.out.println("Find 7: " + bst.find(7));
System.out.println("Find 20: " + bst.find(20));

// Test minimum and maximum methods


System.out.println("Minimum value: " + bst.minimum());
System.out.println("Maximum value: " + bst.maximum());

// Test delete method


System.out.println("Deleting 10");
bst.delete(10);
System.out.print("Tree elements after deleting 10: ");
bst.display();

System.out.println("Is tree empty? " + bst.isEmpty());


}

Exercise 2 (15 Marks)

A. Write a class Tree which has attribute “root” and have following methods:

1. Zero argument constructors


2. preOrder method to traverse the tree and displays the pre-order traversal of the tree.
3. inOrder method to traverse the tree and displays the in-order traversal of the tree.
4. postOrder method to traverse the tree and displays the post-order traversal of the tree.

B. Write a main test class for testing all the methods implemented in part A using the following tree.
Inorder traversal visits the node in the order: Left -> Root -> Right
Preorder traversal visits the node in the order: Root -> Left -> Right
Postorder traversal visits the node in the order: Left -> Right -> Root

public class Node {


int data;
Node left, right;

Node(int data) {
this.data = data;
left = right = null;
}
}

public class tree {


private Node root;

// Zero argument constructor


public tree() {
root = null;
}

// Method to insert a value into the tree (for testing purposes)


public void insert(int value) {
root = insertRec(root, value);
}

private Node insertRec(Node node, int value) {


if (node == null) {
return new Node(value);
}
if (value < node.data) {
node.left = insertRec(node.left, value);
} else {
node.right = insertRec(node.right, value);
}
return node;
}

// Pre-order traversal
public void preOrder() {
System.out.print("Pre-order: ");
preOrderRec(root);
System.out.println();
}

private void preOrderRec(Node node) {


if (node != null) {
System.out.print(node.data + " ");
preOrderRec(node.left);
preOrderRec(node.right);
}
}

// In-order traversal
public void inOrder() {
System.out.print("In-order: ");
inOrderRec(root);
System.out.println();
}

private void inOrderRec(Node node) {


if (node != null) {
inOrderRec(node.left);
System.out.print(node.data + " ");
inOrderRec(node.right);
}
}

// Post-order traversal
public void postOrder() {
System.out.print("Post-order: ");
postOrderRec(root);
System.out.println();
}

private void postOrderRec(Node node) {


if (node != null) {
postOrderRec(node.left);
postOrderRec(node.right);
System.out.print(node.data + " ");
}
}

public class Ex2 {

public static void main(String[] args) {


tree myTree = new tree();

myTree.insert(1);
myTree.insert(2);
myTree.insert(3);
myTree.insert(4);
myTree.insert(5);
myTree.insert(6);
myTree.insert(7);
myTree.insert(8);
myTree.insert(9);

// Display traversals
myTree.preOrder(); // Expected: 10 5 3 7 15 12 18
myTree.inOrder(); // Expected: 3 5 7 10 12 15 18
myTree.postOrder();

}
Sample Output:

You might also like