0% found this document useful (0 votes)
3 views

Lab 9

Uploaded by

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

Lab 9

Uploaded by

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

LAB#9

Name: Muhammad Hassam


Roll no: 037
Section: Bsse3A
Q1: Write a program which should implement a binary tree using a static array of size 10.
Elements of this array of integer type, user will provide values as input for elements of this
array. Your program should allow searching of a value specified by user in tree and it will also
provide deletion of a value specified by user.
Code:
package lab9question1;

public class TreeNode {

int value;

TreeNode left;

TreeNode right;

public TreeNode(int value) {

this.value = value;

left = null;

right = null;

package lab9question1;

public class BinaryTree {

private TreeNode[] tree;

private int size;

private final int MAX_SIZE = 10;

public BinaryTree() {

tree = new TreeNode[MAX_SIZE];


size = 0;

public void insert(int value) {

if (size < MAX_SIZE) {

tree[size] = new TreeNode(value);

size++;

} else {

System.out.println("Tree is full. Cannot insert more elements.");

public void search(int value) {

for (int i = 0; i < size; i++) {

if (tree[i].value == value) {

System.out.println(value+" is Found in Tree");

return;

System.out.println(value+" not Found in Tree");

public void delete(int value) {

int indexToDelete = -1;

for (int i = 0; i < size; i++) {

if (tree[i].value == value) {

indexToDelete = i;
break;

if (indexToDelete == -1) {

System.out.println("Value not found in tree.");

return;

tree[indexToDelete] = tree[size - 1];

size--;

System.out.println("Deleted value " + value + " from the tree.");

public void displayTree() {

if (size == 0) {

System.out.println("Tree is empty.");

return;

System.out.println("Binary Tree elements:");

for (int i = 0; i < size; i++) {

System.out.print(tree[i].value + " ");

System.out.println();

//Main Class

package lab9question1;

public class Lab9Question1 {


public static void main(String[] args) {

BinaryTree binaryTree = new BinaryTree();

int[] treeArray = {4, 2, 5, 1, 6, 3, 7};

for(int i=0; i<treeArray.length; i++){

binaryTree.insert(treeArray[i]);

binaryTree.displayTree();

binaryTree.search(1);

binaryTree.search(8);

binaryTree.delete(7);

binaryTree.displayTree();

Output:

Q2: Write a program which should implement a binary tree using array. Elements of array are
objects of “student” class. “student” class contains attributes (privately defined) reg_no(int),
st_name (string) and cgpa (float). “student” class should also contain member functions
(publicly defined); constructor, input and output functions. User will insert objects of class
“student” array of binary tree, values of attributes of objects will be provided by user.
Program should insert the values using level order insertion technique.
Code:

package lab9question2;

public class BinaryTree {

private Student[] tree;

private int capacity;

private int size;

public BinaryTree(int capacity) {

this.capacity = capacity;

this.size = 0;

this.tree = new Student[capacity];

public void insertStudent(Student student) {

if (size < capacity) {

tree[size++] = student;

} else {

System.out.println("Binary Tree is full. Cannot insert more students.");

public void displayTree() {

if (size == 0) {

System.out.println("Binary tree is empty!");

return;

for (int i = 0; i < size; i++) {

tree[i].output();
}

package lab9question2;

public class Student {

private int regNo;

private String studentName;

private float cgpa;

public Student(int regNo, String stName, float cgpa) {

this.regNo = regNo;

this.studentName = stName;

this.cgpa = cgpa;

public void output() {

System.out.println("Reg No: " + regNo + ", Name: " + studentName + ", CGPA: " + cgpa);

public int getRegNo() {

return regNo;

public String getStName() {

return studentName;

public float getCgpa() {

return cgpa;

///Main class
package lab9question2;

public class Lab9Question2 {

public static void main(String[] args) {

Student[] students = {

new Student(37, "Muhammad Hassam", 3.7f),

new Student(55, "Talha Jamal", 2.9f),

new Student(41, "Sheryar", 3.3f)

};

BinaryTree tree = new BinaryTree(students.length);

for (Student s : students) {

tree.insertStudent(s);

System.out.println("\nDisplaying all students in the binary tree:");

tree.displayTree();

Output:

You might also like