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

Binary Trees-Unit 4

Uploaded by

FRIENDS STUDIO
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)
116 views

Binary Trees-Unit 4

Uploaded by

FRIENDS STUDIO
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/ 33

UNIT4-BINARY TREES

Data Structure
A data structure is a technique of storing and organizing the data in such a way that the data
can be utilized in an efficient manner.
Based on the organizing method of data structure, data structures are divided into two types.

• Linear Data Structures


• Non - Linear Data Structures

Linear data structure:A linear data structure is a structure in which the elements
are stored sequentially, and the elements are connected to the previous and the next
element. As the elements are stored sequentially, so they can be traversed or accessed
in a single run. The implementation of linear data structures is easier as the elements
are sequentially organized in memory. The data elements in an array are traversed one
after another and can access only one element at a time.

Examples are array, stack, queue, linked list, etc.

Non Linear Data Structures: The data structure where data elements are not
organized sequentially is called non linear data structure. In other words, A data elements
of the non linear data structure could be connected to more than one elements to reflect a
special relationship among them. All the data elements in non linear data structure can
not be traversed in single run.

Examples of non linear data structures are Trees and Graphs.

A tree is collection of nodes where these nodes are arranged hierarchically and form a
parent child relationships. A Graph is a collection of a finite number of vertices and an
edges that connect these vertices. Edges represent relationships among vertices that
stores data elements.
Binary Trees:-Binary Tree is a special data structure used for data storage
purposes. A binary tree has a special condition that each node can have a maximum of
two children. A binary tree has the benefits of both an ordered array and a linked list
as search is as quick as in a sorted array and insertion or deletion operation are as fast
as in linked list.

Basic Definition of Binary Trees :-

Path − Path refers to the sequence of nodes along the edges of a tree.
• Root − The node at the top of the tree is called root. There is only one root per tree and one
path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node called parent.
• Child − The node below a given node connected by its edge downward is called its child
node.
• Leaf/External node − The node which does not have any child node is called the leaf node.
• Subtree − Subtree represents the descendants of a node.
• Visiting − Visiting refers to checking the value of a node when control is on the node.
• Traversing − Traversing means passing through nodes in a specific order.
• Levels − Level of a node represents the generation of a node. If the root node is at level 0,
then its next child node is at level 1, its grandchild is at level 2, and so on.
• keys − Key represents a value of a node based on which a search operation is to be carried
out for a node.
• Internal node: Node with atleast one children.

• Depth of a node: Number of edges from root to the node.

• Height of a node: Number of edges from the node to the deepest leaf. Height of the tree is
the height of the root.

• Sibling: The nodes that have the same parent are known as siblings.

• Ancestor node:- An ancestor of a node is any predecessor node on a path from the root to
that node. The root node doesn't have any ancestors. In the tree shown in the above image,
nodes B, and D are the ancestors of node I.

• Descendant: The immediate successor of the given node is known as a descendant of a


node. In the above figure, I is the descendant of node D.

• Strictly Binary Tree:-When every non leaf node in a binary tree is filled with left and right
subtrees, the tree is called a strictly binary tree.

Types of Trees in Data Structure


A tree is a hierarchical data structure which can represent
relationships between different nodes.

Properties of a Tree

• A tree can contain no nodes or it can contain one special node called
the root with zero or more subtrees.

• Every edge of the tree is directly or indirectly originated from the root.
• Every child has only one parent, but one parent can have many children.
Types of Trees

1. General tree
2. Binary tree
3. Binary search tree
4. Extended Binary Tree (OR) 2-Tree

5. Threaded Binary Tree

6. AVL tree
7. 2-3 Tree
8. Heap Tree
9. Forest Tree
10. Red-black tree
11.B-tree

1.General tree:-
• A general tree is defined as a non-empty finite set T of elements called nodes.
• the tree contains a root element or a root node.
• the remaining elements of tree from an order collection of zero or disjoint trees
T1,T2,T3 ......Tm.
• The trees T1,T2,T3 ......Tm are called subtrees of the root element T.
• And the roots of T1,T2,T3.......Tm are called successors of
the roots.
As show the figuer ,A is root node or parent node which has B,C,D are the chiled
nodes or subtrees and E,F,G,H,I,J,K,M,N are know as the successors of the root A.
Examples Family tree, Folder Structure.

2.Binary tree:- Binary tree is the type of tree in which each parent can have at
most two children. The children are referred to as left child or right child. This is one
of the most commonly used trees. When certain constraints and properties are
imposed on Binary tree it results in a number of other widely used trees like BST
(Binary Search Tree), AVL tree, RBT tree etc.

3. Binary search tree:- Binary Search Tree (BST) is an extension of Binary tree with
some added constraints. In BST, the value of the left child of a node must be smaller
than or equal to the value of its parent and the value of the right child is always larger
than or equal to the value of its parent. This property of Binary Search Tree makes it
suitable for searching operations as at each node we can decide accurately whether the
value will be in left subtree or right subtree. Therefore, it is called a Search Tree.

This structure enables one to search for and find on element with an average running
time=o(log2n).
Fig 3: Binary Search Tree.

4.Extended Binary Tree (OR) 2-Tree:-

• A binary tree can be converted to an extended binary tree by adding new nodes to
its leaf nodes or to the nodes which have only one child.

• these new nodes are added in such a way that all the nodes in the resultant tree
have either zero or two children.

• The extended tree is also know as a 2-Tree.

• The nodes of the original tree are called internal nodes.

• The new nodes of the extended tree are external nodes.

few points to be remembered about extended tree are:

i) If a tree has n nodes than the number of branches it has (n-1).

ii) Every node in a tree has exactly one parent except the root node.

iii) A single path connects any nodes of tree.

iv) For a binary tree of height 'h' the maximum number of nodes can be (2h+1 -
1).

v) Any binary tree with n


terminal nodes has (n+1)
external nodes.
Here ,the nodes with circle shape are internal nodes and with square shape are
External nodes.

• number of internal nodes (n)=4

• number of external nodes(n+1)=4+1=5

• Hight of the external tree(h)=3

• the maximum number of nodes=(2h+1 - 1) =23+1 -1=24-1=16-1=15

5.Threaded Binary Tree:-


• we know that the binary tree nodes may have at most two childern. But if they have
only one children or no children, the link part in the linked list representation remains
NULL.

• Using threaded binary tree representation, we can reuse that empty links by making
some threads.

• if one node has some vacant left or right child area that will be used as thread.

• In left threaded node if some node has no left child, then the left pointer will point
to its inorder predecessor.

• In Right threaded node if some node has no Right child, then the Right pointer will
point to its inorder Successor.

• In both cases, if no Successor or predecessor in present, then it will point to the


header node.
• Inorder of the above binary tree is DGBAEHIFC .
• The node H doesn's have left child . Hence the pointer should point to its inorder
predecessor,which is G.
• the node I doesn't have right child hence the pointer should point to
NULL.since it is the right pointer of the node.
• The node G didn't have left child hance the pointer should point to its inorder
prdecessor,which is F

6.AVL tree:- AVL tree is a self-balancing binary search tree. The name AVL is
given on the name of its inventors Adelson-Velshi and Landis. This was the first
dynamically balancing tree. In AVL tree, each node is assigned a balancing factor
based on which it is calculated whether the tree is balanced or not. In AVL tree, the
heights of children of a node differ by at most 1. The valid balancing factor in AVL tree
are 1, 0 and -1. When a new node is added to the AVL tree and tree becomes
unbalanced then rotation is done to make sure that the tree remains balanced. The
common operations like lookup, insertion and deletion takes O(log n) time in AVL
tree. It is widely used for Lookup operations.

Properties:-

1. Follow properties of binary search trees.

2. Self-balancing.

3. Each node stores a value called a balance factor which is the difference in height
between its left subtree and right subtree.

4. All the nodes must have a balance factor of -1, 0 or 1.

After performing insertions or deletions, if there is at least one node that does not have a
balance factor of -1, 0 or 1 then rotations should be performed to balance the tree (self-
balancing).
7.2-3 Trees:-
• The insertion and deletion in an AVL tree involves many rotations to make it a
balanced tree .This makes the entire operation complicated.

• To eliminate this complication, a data structure called 2-3 tree can be used.

• A 2-3 tree is defined as a tree data structure where every node with children (internal
node) has either two children (2-node) as well as one data element or three children (3-
nodes) as well as two data elements.

• If the internal node has one data element and two children then itis called as 2-node.

• If the internal node has two data elements and one child then it is called as 3-nodes.
8.Heap Tree:-
• Heap is a complete binary Tree.

• there are two types of heaps.

i) if the value present at any node is greater than all its children, then the tree is
called the max-heap or descending heap.

ii)if the value present at any node is smaller than all its children, then the tree is
called the min-heap or ascending heap.
9.Forest Tree:-

• A forest is a set of several trees that are not linked to each other.

• Thus a forest denotes a set of disjoint trees.

Example:-

The figure shows a binary tree built from a forest.

10.Red-black tree:- Red-Black is another type of self-balancing tree. The name


Red-Black is given to it because each node in a Red-Black tree is either painted Red or
Black according to the properties of the Red- Black Tree. This make sure that the tree
remains balanced. Although the Red-Black tree is not a perfectly balanced tree but its
properties ensure that the searching operation takes only O(log n) time. Whenever a
new node is added to the Red-Black Tree, the nodes are rotated and painted again if
needed to maintain the properties of the Red-Black Tree .

Properties:-

1. Follow properties of binary search trees.

2. Self-balancing.

3. Each node is either red or black.

4. The root is black (sometimes omitted).

5. All leaves (denoted as NIL) are black.

6. If a node is red, then both its children are black.

7. Every path from a given node to any of its leaf nodes must go through the same
number of black nodes.
11. B-tree:- B-tree is a balanced m-way tree where m defines the order of the tree.
Till now, we read that the node contains only one key but b-tree can have more than
one key, and more than 2 children. It always maintains the sorted data. In binary tree,
it is possible that leaf nodes can be at different levels, but in b-tree, all the leaf nodes
must be at the same level.

If order is m then node has the following properties:

o Each node in a b-tree can have maximum m children


o For minimum children, a leaf node has 0 children, root node has minimum 2
children and internal node has minimum ceiling of m/2 children. For example,
the value of m is 5 which means that a node can have 5 children and internal
nodes can contain maximum 3 children.
o Each node has maximum (m-1) keys.

The root node must contain minimum 1 key and all other nodes must contain
atleast ceiling of m/2 minus 1 keys..
B-tree

Properties of Binary Tree:-

Property-01

Total Number of leaf nodes in a Binary Tree = Total Number of


nodes with 2 children + 1

Example-
Consider the following binary tree-

Here,
• Number of leaf nodes = 3
• Number of nodes with 2 children = 2
Property-02

Maximum number of nodes in a binary tree of height H = 2H+1 – 1

Example-
Maximum number of nodes in a binary tree of height 3
= 23+1 – 1
= 16 – 1
= 15 nodes

Thus, in a binary tree of height = 3, maximum number of nodes that can be inserted =
15.

Property-03
In any binary tree, the maximum number of nodes on level l is 2l where l≥0
Proof:
• In binary tree , length of the binary tree is l . The root node contains any one node
on level 0.
• Hence , the maximum number of nodes on level 0 is 1→2°=1
• The maximum number of nodes on level 1 is 2→2¹=2
• The maximum number of nodes on level 2 is 4→2²=4
• Same like these. The maximum number of nodes on level l=i is 2i⇒2l=2i
• Hence , the maximum number of nodes on level l is 2l.
Fig:

Property-04
For any non-empty binary tree, if n is the number of nodes and e is the number of
edges .
Then, n=e+1
Proof:
• For n=1, e=0 then, n=e+1=0+1 ⇒n=1
• For n=2, e=1 then , n=e+1=1+1⇒n=2
• For n=3, e=2 then, n=e+1=2+1 ⇒n=3

Property -05
For any non-empty binary tree T , if n0 is the number of leaf nodes (degree=0) and n2
is the number of internal nodes(degree=2), then n0=n2+1
Proof:
• Let n be the total number of nodes in binary tree T . ni be the number of nodes
having degree i.
Since 0≤i≤2
we have,
⇒ n=n0+n1+n2 ........ (i)
If e is the number of edges in T , then
⇒ e=n0×0+n1×1+n2×2
⇒ e=n1+2n2 ......... (ii)
Since . n=e+1 ................ (iii)
• For non-empty binary tree T, if n is number of nodes and e is number of edges.

Substitute eq. (ii) in eq(iii)


⇒n=n1+2n2+1 ...... (iv)
Since eq. (i) and eq. (iv) are equal . Then,
⇒n0+n1+n2=n1+2n2+1
⇒n0=1+2n2-n2
n0=n2+1

Binary Tree Representations:-


A binary tree data structure is represented using two methods.

1. Array Representation
2. Linked List Representation

Consider the following binary tree...

1. Array Representation of Binary Tree


A small and almost complete binary tree can be easily stored in a linear array. Small tree is
preferably stored in linear array because searching process in a linear array is expensive. Complete
means that if most of the nodes have two child nodes.

To store binary tree in a linear array, you need to consider the positional indexes of the nodes. This
indexing must be considered starting with 1 from the root node going from left to right as you go
down from one level to other
Assigning of indexes is done in this way-

Index of parent= INT[index of child node/2]


Index of Left Child = 2 * Index of parent
Index of Right Child = 2 * Index of parent+1

These rules are used to store the tree of the above example in an array

If a binary tree contains less number of elements by is deep In structure, the memory
underutilization is a major

To represent a binary tree of depth 'n' using array representation, we need one dimensional array
with a maximum size of 2n + 1.

2. Linked List Representation of Binary Tree

In linked and dynamic representation, the linked list data structure is used. Each node
constitutes of a data part and two link parts. The two link parts store address of left
and right child nodes. Data part is used to store the information about the binary tree
element. This is a better representation as nodes can be added or deleted at any
location. Memory utilization is better in this binary tree representation.
Operations on a Binary Search Tree:-
Following are the basic operations of a tree −
• Search − Searches an element in a tree.
• Insert − Inserts an element in a tree.
• Pre-order Traversal − Traverses a tree in a pre-order manner.
• In-order Traversal − Traverses a tree in an in-order manner.
• Post-order Traversal − Traverses a tree in a post-order manner.
• Deletion-Deletion an element in tree.

Node
Define a node having some data, references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};

creates an empty tree:-Initially an empty tree without any nodes is created. The
variable/identifier which must point to the root node is initialized with a NULL value.

Search Operation:-Whenever an element is to be searched, start searching from


the root node. Then if the data is less than the key value, search for the element in the
left subtree. Otherwise, search for the element in the right subtree. Follow the same
algorithm for each node
Algorithm:-
check while(root->key != key)
// Base Cases: root is null or key is present at root
if (root == NULL || root->key == key)
return root;
// Key is greater than root's key
if (root->key < key)
return search(root->right, key);

// Key is smaller than root's key


return search(root->left, key);

Insert Operation:-
The very first insertion creates the tree. Afterwards, whenever an element is to be
inserted, first locate its proper location. Start searching from the root node, then if the
data is less than the key value, search for the empty location in the left subtree and
insert the data. Otherwise, search for the empty location in the right subtree and
insert the data.

Algorithm:-
1. Create a new BST node and assign values to it.
2. insert(node, key)

* It will handle two cases,


* 1. if the tree is empty, return new node in the root
* 2. if the tree traversal reaches NULL, it will return the new node

i) If root == NULL,
return the new node to the calling function.

* if given val is greater than root->key,


* we should find the correct place in the right subtree and insert
the new node

ii) if root->data < key


call the insert function with root->right and assign the return value in root->right.
root->right = insert(root->right, key)

* if given val is smallar than root->key,


* we should find the correct place in the left subtree and insert the
new node

iii) if root->data > key


call the insert function with root->left and assign the return value in root->left.
root->left = insert(root->left ,key)

* It will handle two cases


* (Prevent the duplicate nodes in the tree)
* 1.if root->key == val it will straight away return the address of
the root node
* 2.After the insertion, it will return the original unchanged root's
address

3. Finally, return the original root pointer to the calling function.

Deletion:-
To delete the given node from the binary search tree(BST), we should follow the below rules.

1.Leaf Node

If the node is leaf (both left and right will be NULL), remove the node directly and free its
memory.

2.Node with Right Child

If the node has only right child (left will be NULL), make the node points to the right node and
free the node.

3.Node with Left Child

If the node has only left child (right will be NULL), make the node points to the left node and
free the node.

4.Node has both left and right child

If the node has both left and right child,


1.find the smallest node in the right subtree. say min
2.make node->data = min
3.Again delete the min node.

Algorithm:-
* If the node becomes NULL, it will return NULL
* Two possible ways which can trigger this case
* 1. If we send the empty tree. i.e root == NULL
* 2. If the given node is not present in the tree.

if(root == NULL)
return NULL;
* If root->key < val. val must be present in the right subtree
* So, call the above remove function with root->right

if(root->key < val)


root->right = removeNode(root->right, val);

* if root->key > val. val must be present in the left subtree


* So, call the above function with root->left
else if(root->key > val)
root->left = removeNode(root->left, val);

* This part will be executed only if the root->key == val


* The actual removal starts from here

else

* Case 1: Leaf node. Both left and right reference is NULL * replace
the node with NULL by returning NULL to the calling pointer.
* free the node

if(root->left == NULL && root->right == NULL)


free(root);
return NULL;

* Case 2: Node has right child.


* replace the root node with root->right and free the right node

else if(root->left == NULL)


struct node *temp = root->right;
free(root);
return temp;

* Case 3: Node has left child.


* replace the node with root->left and free the left node

else if(root->right == NULL)


struct node *temp = root->left;
free(root);
return temp;

* Case 4: Node has both left and right children.


* Find the min value in the right subtree
* replace node value with min.
* And again call the remove function to delete the node which has
the min value.
* Since we find the min value from the right subtree call the
remove function with root->right

else
int rightMin = getRightMin(root->right);
root->key = rightMin;
root->right = removeNode(root->right,rightMin);

Binary Tree Traversal:-


Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all
nodes are connected via edges (links) we always start from the root (head) node. That is, we cannot
randomly access a node in a tree. There are three ways which we use to traverse a tree −
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the
values it contains.

In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We
should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an ascending
order.

We start from A, and following in-order traversal, we move to its left subtree B. B is also traversed in-order.
The process goes on until all the nodes are visited. The output of inorder traversal of this tree will be −

D→B→E→A→F→C→G

Step 1 − Recursively traverse left subtree.


Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

Algorithm:-
If tree! =0 then
Inorder(tree->leftchild);
Visit(tree);
Inorder(tree->rightchild);

Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree.
We start from A, and following pre-order traversal, we first visit A itself and then move to its left
subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The
output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

Algorithm:-
If tree! =0 then

Visit(tree);
Preorder(tree->leftchild);
Preorder(tree->rightchild);

Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left
subtree, then the right subtree and finally the root node.

We start from A, and following Post-order traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until all the nodes are visited. The output of post-order
traversal of this tree will be −
D→E→B→F→G→C→A
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.

Algorithm:-
If tree! =0 then

Post order(tree->left child);


Post order(tree->right child);
Visit(tree);

Level order Traversal


IN level order traversal,the nodes are visited level by level starting from the root, and going from
left to right . The level order traversal required a queue data structure. So
it is not possible to develop a recursive procedure to traversal the binary tree in level
order . This is nothing but a breadth first search technique.
Level order Traversal
A -> B -> C -> D -> E -> F -> G

Counting Number of Binary Trees:-


Every root node represents the binary search tree so in the given binary tree we can see that
there is no other binary search tree present therefore the count is 2 which is the total number of leaf
nodes in a binary tree.

Input
The tree which will be created after inputting the values is given below −

Output
Count the Number of Binary Search Trees present in a Binary Tree are: 6
Explanation
we are given with an array of integer values that is used to form a binary tree and we will check
whether there is a binary search tree present in it. Every root node represents the binary search tree
so in the given binary tree we can see that there are 4 leaf nodes and two subtrees which are
forming the BST therefore the count is 6.

Approach used in the program is as follows −


In this approach we will find the largest value of the node in the left subtree of node N and check if it
is less than N. Also, we will find the smallest value in the right subtree of node N and check if it is
more than N. If true, then it is a BST. Traverse the binary tree in bottom up manner and check above
conditions and increment count of BSTs
• The node of every node_data contains the information like number of BSTs present,
maximum value in that tree, minimum value, boolean true if that subtree is a BST.
• Function BST_present(struct tree_node* parent) finds the count of BSTs present inside the
binary tree rooted at parent.
• If the parent is NULL then return { 0, min, max, true } where min is INT-MIN and max is
INT_MAX.
• If left and right childs are null then return { 1, parent−>data, parent−>data, true }
• Set node_data Left = BST_present(parent−>left); and node_data Right =
BST_present(parent−>right);
• Take node n1 and set n1.lowest = min(parent−>data, (min(Left.lowest, Right.lowest))) as
lowest in its right subtree.
• Set n1.highest = max(parent−>data, (max(Left.highest, Right.highest))); as highest in its left
subtree.
• if(Left.check && Right.check && parent−>data > Left.highest && parent−>data <Right.lowest)
returns true then set n1.check = true as it is a BST.
• Increase count of bsts as n1.total_bst = 1 + Left.total_bst + Right.total_bst;
• Otherwise set n1.check = false and count as n1.total_bst = Left.total_bst + Right.total_bst.
• At the end return n1.

Applications of binary trees:-


• Binary Search Tree - Used in many search applications where data is constantly
entering/leaving, such as the map and set objects in many languages' libraries.
• Binary Space Partition - Used in almost every 3D video game to determine what
objects need to be rendered.
• Binary Tries - Used in almost every high-bandwidth router for storing router-
tables.
• Hash Trees - used in p2p programs and specialized image-signatures in which a
hash needs to be verified, but the whole file is not available.
• Heaps - Used in implementing efficient priority-queues, which in turn are used for
scheduling processes in many operating systems, Quality-of-Service in routers, and
A* (path-finding algorithm used in AI applications, including robotics and video
games). Also used in heap-sort.
• Huffman Coding Tree (Chip Uni) - used in compression algorithms, such as those
used by the .jpeg and .mp3 file-formats.
• GGM Trees - Used in cryptographic applications to generate a tree of pseudo-
random numbers.
• Syntax Tree - Constructed by compilers and (implicitly) calculators to parse
expressions.
• Treap - Randomized data structure used in wireless networking and memory
allocation.
• T-tree - Though most databases use some form of B-tree to store data on the drive,
databases which keep all (most) their data in memory often use T-trees to do so.

The properties that separate a binary search tree from a regular binary .
tree is All nodes of left subtree are less than the root node
All nodes of right subtree are more than the root node.
Both subtrees of each node are also BSTs i.e. they have the above two properties

The binary tree on the right isn't a binary search tree because the right subtree of the
node "3" contains a value smaller than it.

There are three basic operations that you can perform on a binary
search tree: Search, Insert, Delete

Search Operations

If the value is below the root, we can say for sure that the value is not in the right
subtree; we need to only search in the left subtree and if the value is above the
root, we can say for sure that the value is not in the left subtree; we need to only
search in the rightsubtree.

1) If root == NULL or number == root->data return root

2) If number < root->data


return Search(root->left)
3) If number > root->data
return Search(root->right)

if root== NULL
Print number not
found Else
Print number found at root
Insertion

Insert function is used to add a new element in a binary search tree at appropriate
location. Insert function is to be designed in such a way that, it must node violate
the property of binary search tree at each value.

Allocate the memory for node.

Set the data part to the value and set the left and right pointer of node to NULL.

If the item to be inserted, will be the first element of the tree, then the left and
right of this node will point to NULL.

Else, check if the item is less than the root element of the tree, if this is true, then
recursively perform this operation with the left of the root.

If this is false, then perform this operation recursively with the right sub-tree of the
root.

1)IF NODE == NULL


SET NODE -> DATA = VALUE
SET NODE -> LEFT = NODE -> RIGHT =NULL
ELSE
IF VALUE < NODE -> DATA
Insert(NODE -> LEFT, VALUE)
ELSE
Insert(NODE -> RIGHT, VALUE)

Delete:-
Delete function is used to delete the specified node from a binary search tree.
However, we must delete a node from a binary search tree in such a way, that the
property of binary search tree doesn't violate. There are three situations of deleting a
node from binary search tree.

The node to be deleted is a leaf node

It is the simplest case, in this case, replace the leaf node with the NULL and simple
free the allocated space.

In the following image, we are deleting the node 85, since the node is a leaf node,
therefore the node will be replaced with NULL and allocated space will be freed.

The node to be deleted has only one child


In this case, replace the node with its child and delete the child node, which now
contains the value which is to be deleted. Simply replace it withthe NULL and free
the allocated space.

In the following image, the node 12 is to be deleted. It has only one child. The node
will be replaced with its child node and the replaced node 12 (which is now leaf node)
will simply be deleted.
The node to be deleted has only two child

It is a bit complexed case compare to other two cases. However, the node which is
to be deleted, is replaced with its in-order successor or predecessor recursively
until the node value (to be deleted) is placed on the leaf of the tree. After the
procedure, replace the node with NULL and free the allocatedspace.

In the following image, the node 50 is to be deleted which is the root node of
the tree. The in-order traversal of the tree given below.

6, 25, 30, 50, 52, 60, 70, 75.

replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the
tree, which will simply be deleted.

Delete (Tree,Item)
IF TREE = NULL
Write "item not found in the tree"
ELSE IF ITEM < TREE -> DATA
Delete(TREE->LEFT, ITEM)
ELSE IF ITEM > TREE -> DATA
Delete(TREE -> RIGHT, ITEM)
ELSE IF TREE -> LEFT AND TREE -> RIGHT
SET TEMP = find min value Node(TREE -> RIGHT)
SET TREE ->DATA = TEMP ->DATA
Delete(TREE ->LEFT, TEMP ->DATA)
ELSE
SET TEMP = TREE
IF TREE -> LEFT = NULL AND TREE -> RIGHT = NULL
SET TREE = NULL
ELSE IF TREE -> LEFT != NULL
SET TREE = TREE -> LEFT ELSE
SET TREE = TREE -> RIGHT.

You might also like