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

Lab 10

The document outlines a lab manual for implementing Binary Search Tree (BST) traversals in Java, focusing on insertion and three types of traversals: In-order, Pre-order, and Post-order. It provides a theoretical background on BSTs, detailed algorithms for insertion and traversal methods, and includes Java code for a BST implementation. The lab task involves creating a BST of student marks and performing the specified traversals.

Uploaded by

ulfat0372
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)
2 views

Lab 10

The document outlines a lab manual for implementing Binary Search Tree (BST) traversals in Java, focusing on insertion and three types of traversals: In-order, Pre-order, and Post-order. It provides a theoretical background on BSTs, detailed algorithms for insertion and traversal methods, and includes Java code for a BST implementation. The lab task involves creating a BST of student marks and performing the specified traversals.

Uploaded by

ulfat0372
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/ 3

Lab Manual 10

Title: Implementation of Binary Search Tree Traversals in Java

Objective:

• To understand the concept of a Binary Search Tree (BST).


• To implement BST operations (insertion, traversal) in Java.
• To learn and perform In-order, Pre-order, and Post-order tree traversals.

Theory:

A Binary Search Tree (BST) is a binary tree in which each node has a key greater than all the
keys in its left subtree and smaller than all the keys in its right subtree.
Tree traversal refers to visiting all the nodes in a tree in a specific order:

• In-order (Left, Root, Right): Visits nodes in ascending order.


• Pre-order (Root, Left, Right): Used to create a copy of the tree.
• Post-order (Lef t, Right, Root): Used to delete the tree.

Tasks:

1. Create a Binary Search Tree (BST).


2. Perform Insertion into BST.
3. Implement and demonstrate In-order, Pre-order, and Post-order traversals.

Algorithm:

1. Insert into BST:

1. Start at the root node.


2. If the tree is empty, insert the new value as the root node.
3. If the value is smaller than the current node, move to the left child.
4. If the value is larger than the current node, move to the right child.
5. Repeat until the correct position is found.

2. Traversals:

• In-order Traversal:
1. Traverse the left subtree.
2. Visit the root node.
3. Traverse the right subtree.
• Pre-order Traversal:
1. Visit the root node.
2. Traverse the left subtree.
3. Traverse the right subtree.
• Post-order Traversal:
1. Traverse the left subtree.
2. Traverse the right subtree.
3. Visit the root node.

class Node {
int key;
Node left, right;

public Node(int item) {


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

class BinarySearchTree {
Node root;

BinarySearchTree() {
root = null;
}

// Insert method
void insert(int key) {
root = insertRec(root, key);
}

Node insertRec(Node root, int key) {


if (root == null) {
root = new Node(key);
return root;
}
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
return root;
}

// In-order traversal
void inorder() {
inorderRec(root);
}

void inorderRec(Node root) {


if (root != null) {
inorderRec(root.left);
System.out.print(root.key + " ");
inorderRec(root.right);
}
}

// Pre-order traversal
void preorder() {
preorderRec(root);
}

void preorderRec(Node root) {


if (root != null) {
System.out.print(root.key + " ");
preorderRec(root.left);
preorderRec(root.right);
}
}

// Post-order traversal
void postorder() {
postorderRec(root);
}

void postorderRec(Node root) {


if (root != null) {
postorderRec(root.left);
postorderRec(root.right);
System.out.print(root.key + " ");
}
}

public static void main(String[] args) {


BinarySearchTree tree = new BinarySearchTree();

/* Insert elements into the BST */


tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);

System.out.println("In-order Traversal:");
tree.inorder();

System.out.println("\nPre-order Traversal:");
tree.preorder();

System.out.println("\nPost-order Traversal:");
tree.postorder();
}
}
Lab Task:

1. Create a binary search tree of student marks and perform above types of traversals on it.

You might also like