5th Daa
5th Daa
5.1 WAP and algorithm for Binary search tree (Insertion ,deletion
,update,traversing).
Algorithm for Binary Search Tree Operations:
1. Insertion:
If the tree is empty, the new node becomes the root.
Otherwise, compare the value with the current node, and recursively insert it
into the left or right subtree based on comparison.
2. Deletion:
If the node to be deleted has no children (leaf node), simply remove it.
If the node has one child, replace it with its child.
If the node has two children, find the in-order successor (smallest node in the
right subtree) or in-order predecessor (largest node in the left subtree) and
replace the node with it, then delete the successor/predecessor.
3. Update:
Search for the node, and if found, simply modify its value.
4. Traversal:
In-order: Visit the left subtree, then the current node, then the right subtree.
Pre-order: Visit the current node, then the left subtree, then the right subtree.
Post-order: Visit the left subtree, then the right subtree, then the current node.
Program:-
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int value) : data(value), left(nullptr), right(nullptr) {}
};