0% found this document useful (0 votes)
6 views6 pages

Programming Exercise # 8 - Binary Tree

The document contains a Java program that implements a binary tree with functionalities such as insertion, searching, counting nodes, and checking if the tree is empty. It defines classes for the binary tree nodes and the tree itself, along with methods for different types of tree traversals. The main method allows users to interact with the binary tree through a console interface.

Uploaded by

just Meee
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)
6 views6 pages

Programming Exercise # 8 - Binary Tree

The document contains a Java program that implements a binary tree with functionalities such as insertion, searching, counting nodes, and checking if the tree is empty. It defines classes for the binary tree nodes and the tree itself, along with methods for different types of tree traversals. The main method allows users to interact with the binary tree through a console interface.

Uploaded by

just Meee
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

Xander John C.

Salarda 11 / 30 / 2025

Programming Exercise # 8 - Binary Tree

package BinaryTreeProject;

//Java Program to Implement Binary Tree


import [Link];

/* Class BTNode */
class BTNode {
BTNode left, right;
int data;

/* Constructor */
public BTNode() {
left = null;
right = null;
data = 0;
}

/* Constructor */
public BTNode(int n) {
left = null;
right = null;
data = n;
}

/* Function to set left node */


public void setLeft(BTNode n) {
left = n;
}

/* Function to set right node */


public void setRight(BTNode n) {
right = n;
}

/* Function to get left node */


public BTNode getLeft() {
return left;
}

/* Function to get right node */


public BTNode getRight() {
return right;
}

/* Function to set data to node */


public void setData(int d) {
data = d;
}

/* Function to get data from node */


public int getData() {
return data;
}
}

/* Class BT */
class BT {
private BTNode root;

/* Constructor */
public BT() {
root = null;
}

/* Function to check if tree is empty */


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

/* Functions to insert data */


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

/* Function to insert data recursively */


private BTNode insert(BTNode node, int data) {
if (node == null)
node = new BTNode(data);
else {
if ([Link]() == null)
[Link] = insert([Link], data);
else
[Link] = insert([Link], data);
}
return node;
}

/* Function to count number of nodes */


public int countNodes() {
return countNodes(root);
}
/* Function to count number of nodes recursively */
private int countNodes(BTNode r) {
if (r == null)
return 0;
else {
int l = 1;
l += countNodes([Link]());
l += countNodes([Link]());
return l;
}
}

/* Function to search for an element */


public boolean search(int val) {
return search(root, val);
}

/* Function to search for an element recursively */


private boolean search(BTNode r, int val) {
if ([Link]() == val)
return true;
if ([Link]() != null)
if (search([Link](), val))
return true;
if ([Link]() != null)
if (search([Link](), val))
return true;
return false;
}

/* Function for inorder traversal */


public void inorder() {
inorder(root);
}

private void inorder(BTNode r) {


if (r != null) {
inorder([Link]());
[Link]([Link]() + " ");
inorder([Link]());
}
}

/* Function for preorder traversal */


public void preorder() {
preorder(root);
}
private void preorder(BTNode r) {
if (r != null) {
[Link]([Link]() + " ");
preorder([Link]());
preorder([Link]());
}
}

/* Function for postorder traversal */


public void postorder() {
postorder(root);
}

private void postorder(BTNode r) {


if (r != null) {
postorder([Link]());
postorder(r.get_Right());
[Link]([Link]() + " ");
}
}
}

/* Class BinaryTree */
public class BinaryTree {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);

/* Creating object of BT */
BT bt = new BT();

/* Perform tree operations */


[Link]("Binary Tree Test\n");
char ch;

do {
[Link]("\nBinary Tree Operations\n");
[Link]("1. insert ");
[Link]("2. search");
[Link]("3. count nodes");
[Link]("4. check empty");

int choice = [Link]();


switch (choice) {
case 1 :
[Link]("Enter integer element to insert");
[Link]([Link]());
break;
case 2 :
[Link]("Enter integer element to search");
[Link]("Search result : " +
[Link]([Link]()));
break;
case 3 :
[Link]("Nodes = " + [Link]());
break;
case 4 :
[Link]("Empty status = " + [Link]());
break;
default :
[Link]("Wrong Entry \n ");
break;
}

/* Display tree */
[Link]("\nPost order : ");
[Link]();
[Link]("\nPre order : ");
[Link]();
[Link]("\nIn order : ");
[Link]();
[Link]("\n\nDo you want to continue (Type y or n) \n");
ch = [Link]().charAt(0);
} while (ch == 'Y' || ch == 'y');
}
}
Screenshots/Outputs

You might also like