Experiment No 3
Experiment No 3
03
AIM: Beginning with an empty binary search tree, Construct binary search tree by inserting
the values in the order given. After constructing a binary tree –
i. Insert new node.
ii. Find number of nodes in longest path from root.
iii. Minimum data value found in the tree.
iv. Change a tree so that the roles of the left and right pointers are swapped at every node.
v. Search a value.
Pre-requisite:
• Knowledge of C++ programming
OBJECTIVE:
A binary tree in which each internal node x stores an element such that the element
stored in the left subtree of x are less than or equal to x and elements stored in the right
subtree of x are greater than orequal to x. This is called binary-search-tree property.
Searching
Searching a binary tree for a specific value can be a recursive or iterative process. This
explanation covers a recursive method.We begin by examining the root node. If the tree
is null, the value we are searching for does not exist in the tree. Otherwise, if the value
equals the root, the search is successful.If the value is less than the root, search the left
subtree. Similarly, if it is greater than the root, search the right subtree. This process is
repeated until the value is found or the indicated subtree is null. If the
searched value is not found before a null subtree is reached, then the item must not be
present in the tree.
Insertion
Insertion begins as a search would begin; if the root is not equal to the value, we search
the left or rightsubtrees as before. Eventually, we will reach an external node and add
the value as its right or left child, depending on the node's value. In other words, we
examine the root and recursively insert the new node to the left subtree if the new value
is less than the root, or the right subtree if the new value is greater than or equal to the
root.
Deletion
As with all binary trees, a node's in-order successor is the left-most child of its right
subtree, and a node's in-order predecessor is the right-most child of its left subtree. In
either case, this node will havezero or one children. Delete it according to one of the
two simpler cases above.
ALGORITHM:
Define structure for Binary Tree (Mnemonics, Left Pointer, Right Pointer)
Insert Node:
Insert (Root, Node)
Root is a variable of type structure, represent Root of the Tree. Node is
a variable oftype structure ,represent new Node to insert in a tree.
Step 1: Repeat Steps 2,3 & 4 Until Node do not insert at
appropriate position.Step 2: If Node Data is less that Root
Data & Root Left Tree is NULL
Then
insert
Node to
Left.Else
Move
Root to
Left
Step 3: Else If Node Data is Greater that equal that Root Data &
Root Right Tree isNULL
Then insert
Node to
Right.Else
Move Root
to Right.
Step 4:
Stop.
Search Node:
Search (Root, Mnemonics)
Root is a variable of type structure, represent Root of the Tree. Mnemonics is array of
character. Thisfunction search Mnemonics in a Tree.
Step 1: Repeat Steps 2,3 & 4 Until Mnemonics Not find &&
Root != NULLStep 2: If Mnemonics Equal to Root Data
Then print message Mnemonics present.
Step 3: Else If Mnemonics Greater that Equal
that Root DataThen Move Root to Right.
Step 4: Else
Move Root to
Left.Step 5:
Stop.
Delete Node:
Dsearch(Root, Mnemonics)
Root is a variable of type structure ,represent Root of the Tree. Mnemonics is array of
character. Stack is an pointer array of type structure. PTree(Parent of Searched
Node),Tree(Node to be deleted),RTree(Pointg to Right Tree),Temp are pointer
variable of type structure;
Step 1: Search Mnemonics in a
Binary Tree Step 2: If Root = =
NULL Then Tree is NULL
Step 3: Else //Delete Leaf
Node
If Tree->Left = = NULL && Tree-
>Right = = NULLThen a) If Root = =
Tree Then Root = NULL;
>Right=Temp;Else PTree-
>Left=Temp;
f) Temp-
>Left=Tree-
>Left;g)If
RTree!=Te
mp
Then Temp->Right
= Tree->Right;Step
5: Else //with Right
child
If Tree->Right!= NULL
Then a) If Root==Tree Root = Root->Right;
b) If Tree is a Right Child PTree-
>Right=Tree->Right;Else PTree-
>Left=Tree->Left;
Step 6: Else
//with Left
childIf Tree-
>Left !=
NULL
Then a) If Root==Tree Root = Root->Left;
b) If Tree is a Right Child PTree-
>Right=Tree->Left;Else PTree-
>Left=Tree->Left;
Step 7: Stop.
Mirror Image:
Root is a variable of type structure ,represent Root of the Tree. Queue is an pointer
array of typestructure. Front & Rear variable of type integer.
Mirror(Root)
Step 1: Queue[0]=Root;//Insert Root
Node in a Queue Step 2: Repeat Steps
3,4,5,6,7 & 8 Until Queue is EmptyStep
3: Root = Queue[Front++];
Step 4: Temp1 = Root->Left;
Step 5: Root->Left
= Root->Right;Step
6: Root->Right =
Temp1; Step 7: If
Root->Left !=
NULL
Then Queue[Rear++] = Tree->Left;//insert
Left SubTreeStep 8: If Root->Right !=
NULL
Then Queue[Rear++] = Root->Right;//insert
Right SubTreeStep 9: Stop.
INPUT:
Accept the nodes from the user like: add, mult, div, sub
OUTPUT:
Display result of each operation with error checking.
Questions:
1. What is Binary search tree?
2. What are the members of structure of tree & what is the size of structure?
3. What are rules to construct binary search tree?
4. How general tree is converted into binary tree?
5. What is binary threaded tree?
6. What is use of thread in traversal?
7. What is time complexity for search operation in BST?
8. What is preorder, postorder, in order traversal?
9. Traverse tree using BFS & DFS?