0% found this document useful (0 votes)
2 views8 pages

lab7 tree

This document outlines a lab on tree data structures, focusing on binary trees and binary search trees (BST). It includes code examples for creating binary trees, performing various traversals (in-order, pre-order, post-order), and implementing BST operations such as insertion, searching, and deletion. The document also provides a main function to demonstrate these operations with user input.

Uploaded by

Beepen Lamsal
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 views8 pages

lab7 tree

This document outlines a lab on tree data structures, focusing on binary trees and binary search trees (BST). It includes code examples for creating binary trees, performing various traversals (in-order, pre-order, post-order), and implementing BST operations such as insertion, searching, and deletion. The document also provides a main function to demonstrate these operations with user input.

Uploaded by

Beepen Lamsal
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/ 8

LAB 7: TREE

Related theory:

 Tree terminologies
 Binary tree
 Tree Traversals
 Binary search tree operations

1. Creation and Pre- order, in-order and Post order Traversal of binary trees

#include <iostream>

using namespace std;

class Node{

public:

int data;

Node* left;

Node* right;

Node(int val){

data = val;

left = NULL;

right = NULL;

};

void in_Order(Node* root){ // In-order: Left, Root, Right

if(root == NULL) return;

in_Order(root->left);

cout<<root->data<<" ";

in_Order(root->right);

Manoj Rokaya pg. 1


LAB 7: TREE

void pre_Order(Node* root){ // Pre-order: Root, Left, Right

if(root == NULL) return;

cout<<root->data<<" ";

pre_Order(root->left);

pre_Order(root->right);

void post_Order(Node*root){ // Post-order: Left, Right, Root

if(root == NULL) return;

post_Order(root->left);

post_Order(root->right);

cout<<root->data<<" ";

int main()

Node* root = new Node(10); // 10

root->left = new Node(20); // / \

root->right = new Node(30); // 20 30


// /

root->left->left = new Node(40); // 40

cout << "In-order Traversal: ";

in_Order(root);

Manoj Rokaya pg. 2


LAB 7: TREE

cout << "\nPre-order Traversal: ";

pre_Order(root);

cout << "\nPost-order Traversal: ";

post_Order(root);

return 0;

2. Program for the following operations on Binary Search Tree (BST) of Integers
i. Creation of a BST
ii. Perform insertion operation in the BST
iii. Traverse the BST in In-order, Preorder and Post Order
iv. Search the BST for a given element
v. Perform deletion of some elements

#include <iostream>

using namespace std;

class Node {

public:

int info;

Node* left;

Node* right;

Node(int val) : info(val), left(nullptr), right(nullptr) {}

};

class BST {

private:

Node* root;

Manoj Rokaya pg. 3


LAB 7: TREE

// Recursive helpers

Node* insert(Node* node, int val) {

if (!node) return new Node(val);

if (val < node->info)

node->left = insert(node->left, val);

else

node->right = insert(node->right, val);

return node;

void inorder(Node* node) {

if (node) {

inorder(node->left);

cout << node->info << ", ";

inorder(node->right);

void preorder(Node* node) {

if (node) {

cout << node->info << ", ";

preorder(node->left);

preorder(node->right);

void postorder(Node* node) {

if (node) {

Manoj Rokaya pg. 4


LAB 7: TREE

postorder(node->left);

postorder(node->right);

cout << node->info << ", ";

Node* search(Node* node, int key) {

if (!node || node->info == key) return node;

if (key < node->info)

return search(node->left, key);

else

return search(node->right, key);

Node* findMin(Node* node) {

while (node && node->left)

node = node->left;

return node;

Node* deleteNode(Node* node, int key) {

if (!node) {

return nullptr};

if (key < node->info) {

node->left = deleteNode(node->left, key);

} else if (key > node->info) {

node->right = deleteNode(node->right, key);

} else {

Manoj Rokaya pg. 5


LAB 7: TREE

// Node with one or no child

if (!node->left) {

Node* temp = node->right;

delete node;

return temp;

if (!node->right) {

Node* temp = node->left;

delete node;

return temp;

// Node with two children

Node* successor = findMin(node->right);

node->info = successor->info;

node->right = deleteNode(node->right, successor->info);

return node;

public:

BST() : root(nullptr) {}

void create() {

int n, val;

cout << "Enter number of nodes: ";

cin >> n;

cout << "Enter " << n << " values:\n";

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

cin >> val;

Manoj Rokaya pg. 6


LAB 7: TREE

root = insert(root, val);

void insertValue(int val) {

root = insert(root, val);

cout << val << " inserted into BST.\n";

void deleteValue(int val) {

root = deleteNode(root, val);

cout << val << " deleted from BST (if it existed).\n";

void searchValue(int val) {

Node* result = search(root, val);

if (result)

cout << val << " found in BST.\n";

else

cout << val << " not found in BST.\n";

void traverseAll() {

cout << "\nInorder Traversal:\n";

inorder(root);

cout << "\nPreorder Traversal:\n";

preorder(root);

cout << "\nPostorder Traversal:\n";

postorder(root);

Manoj Rokaya pg. 7


LAB 7: TREE

cout << endl;

};

int main() {

BST tree;

tree.create();

cout << "\nInitial Traversals:";

tree.traverseAll();

int val;

cout << "\nEnter value to insert: ";

cin >> val;

tree.insertValue(val);

cout << "Enter value to search: ";

cin >> val;

tree.searchValue(val);

cout << "Enter value to delete: ";

cin >> val;

tree.deleteValue(val);

cout << "\nTraversals After Modifications:";

tree.traverseAll();

return 0;

Manoj Rokaya pg. 8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy