5.trees (Finalized)
5.trees (Finalized)
Qns:
Tree Terminology:
A tree is a hierarchical data structure defined as a
collection of nodes. Nodes represent value and nodes are connected by edges.
The tree has one node called root.
Root:
The root node is the topmost node in the tree hierarchy. In other words,
the root node is the one that doesn't have any parent. In the below structure,
node numbered A is the root node of the tree. If a node is directly linked to
some other node, it would be called a parent-child relationship.
Child node:
Parent:
If the node contains any sub-node, then that node is said to be the parent
of that sub-node. Or A node that has one or more child nodes directly connected
to it. The node which is a predecessor of any node is called a Parent Node, that
is, the node which has a branch from it to any other node is called as the Parent
node.
Sibling:
The nodes that have the same parent are known as siblings. In a tree
data structure, nodes which belong to same Parent are called as SIBLINGS. In
simple words, the nodes with the same parent are called Sibling nodes.
Leaf Node:
The node of the tree, which doesn't have any child node, is called a
leaf node. A leaf node is the bottom-most node of the tree. There can be any
number of leaf nodes present in a general tree. Leaf nodes can also be called
external nodes.
Internal nodes:
Ancestor node:
Degree of a Node:
Qns:
A binary tree data structure is represented using two methods. Those methods are
as follows...
1. Array Representation
2. Linked List Representation
Binary Tree:
A Binary Tree is a type of tree data structure where each node can
have a maximum of two child nodes, a left child node and a right child node.
This restriction, that a node can have a maximum of two child nodes, gives us
many benefits:
Nodes: A binary tree consists of nodes. Each node contains three parts:
Root: The top node of the binary tree is called the root. It is the only node that
has no parent.
Leaves: Nodes that do not have any children are called leaves or leaf nodes.
Parent and Child: In a binary tree, every node (except the root) has exactly one
parent. Each node can have at most two children: a left child and a right child.
Height: The height of a binary tree is the length of the longest path from the
root to a leaf. The height of a tree with only one node (the root) is 0.
Types of Binary Trees:
Full Binary Tree: A tree in which every node has either zero children or two
children, Every node other than the leaves has two children.
Perfect Binary Tree: All internal nodes have two children, and all leaf nodes
are at the same level or a binary tree in which all interior nodes have two
children and all leaves have the same depth or same level
Complete Binary Tree:
Binary Search Trees (BST): A type of binary tree used for efficient searching,
insertion, and deletion.
Binary trees are a versatile and foundational data structure in computer science,
providing the basis for many more advanced structures and algorithms.
Example:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node // Define the structure for a node in the binary tree
{
int data;
struct Node *left, *right;
} Node;
int main()
{
Node* root = createNode (1);
root ->left = createNode (2);
root->right = createNode (3);
root ->left->left = createNode (4);
root ->left->right = createNode (5);
root ->right->left = createNode (6);
root ->right->right = createNode (7);
printf ("In-order traversal: ");
inorderTraversal(root);
printf("\n");
Output:
In-order traversal: 4 2 5 1 6 3 7
Pre-order traversal: 1 2 4 5 3 6 7
Post-order traversal: 4 5 2 6 7 3 1
Qns:
In one-way threaded binary trees, a thread will appear either in the right
or left link field of a node.
If it appears in the right link field of a node then it will point to the next
node that will appear on performing in order traversal.
Such trees are called Right threaded binary trees.
If thread appears in the left field of a node, then it will point to the nodes
in order predecessor.
Such trees are called Left threaded binary trees.
In one-way threaded binary trees, the right link field of last node and left
link field of first node contains a NULL.
In order to distinguish threads from normal links they are represented by
dotted lines.
Double threaded Binary trees / Two-way:
Space Optimization:
Time Efficiency:
{ {
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
if (!newNode)
printf("Memory error\n");
return NULL;
newNode->data = data;
newNode->leftThread = newNode->rightThread = 0;
return newNode;
root = createNode(data);
return root;
if (data < root->data) // Recursively insert data into the left or right subtree
if (root->leftThread)
newNode->left = root->left;
newNode->right = root;
newNode->rightThread = 1;
root->left = newNode;
root->leftThread = 0;
else
{
if (root->rightThread)
Node* newNode = createNode(data); // If right child is a thread, replace it with the new node
newNode->right = root->right;
newNode->left = root;
newNode->leftThread = 1;
root->right = newNode;
root->rightThread = 0;
else
return root;
}
void inorderTraversal(Node* root) // Function to perform in-order traversal
if (root != NULL)
inorderTraversal(root->left);
inorderTraversal(root->right);
int main()
inorderTraversal(root);
printf("\n");
return 0;
Output:
20 30 40 50 60 70 80
Qns:
In the above figure, the root node is 40, and all the nodes of the left subtree are
smaller than the root node, and all the nodes of the right subtree are greater
than the root node.
Implementation:
First, compare the element to be searched with the root element of the tree.
If root is matched with the target element, then return the node's location.
If it is not matched, then check whether the item is less than the root
element or smaller than the root element, then move to the left subtree.
If it is larger than the root element, then move to the right subtree.
Repeat the above procedure recursively until the match is found.
If the element is not found or not present in the tree, then return NULL.
Characteristics of Binary Search Tree:
1.Binary Tree Structure: A binary tree is a tree data structure where each node
has at most two children, referred to as the left child and the right child.
2. Node Properties:
Left Subtree:
All nodes in the left subtree of a node contain values that are less than the value
of the node.
Right Subtree: All nodes in the right subtree of a node contain values that are
greater than the value of the node.
3.Operations:
1.Insertion:
2.Deletion:
Deleting a node in a BST can be a bit more complex because there are three
cases to consider:
Node with one child: Remove the node and link its parent directly to its child.
Node with two children: Find the node's in-order predecessor (maximum value
in the left subtree) or in-order successor (minimum value in the right subtree),
replace the node's value with that, and then delete the in-order
predecessor/successor node.
3.Search:
To search for a value in a BST, start from the root and recursively
traverse the left or right subtree depending on whether the target value is
less than or greater than the current node's value.
The average time complexity for searching in a BST is O(log n) if the
tree is balanced.
Advantages:
Disadvantages:
Balancing Issues: If the BST becomes unbalanced (e.g., all elements are
inserted in increasing or decreasing order), the time complexity for operations
can degrade to O(n),which is the same as a linked list.
Implementation:
45
Step:1
Insert 15, 15 is smaller than 45, so insert it as the root node of the left subtree.
45
15
Step:2
Insert 79, 79 is greater than 45, so insert it as the root node of the right subtree.
45
Step:3
15 79
Insert 90.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
45
15 79
90
Step:4
Insert 10.
45
15 79
10 90
Step 5:
Insert 55.
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree
of 79.
45
15 79
10 55 90
Step 6:
Insert 12.
12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the right
subtree of 10.
45
15 79
10 55 90
12
Step 7:
Insert 20.
20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree
of 15.
45
15 79
10 20 55 90
12
Step :8
Insert 50.
50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a left
subtree of 55.
45
15 79
10 20 55 90
12 50
Example:
Example:
#include <stdio.h>
#include <stdlib.h>
struct Node
int data;
};
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
if (root == NULL)
{
return createNode(data);
return root;
if (root != NULL)
inorder(root->left);
inorder(root->right);
}
int main()
insert(root, 79);
insert (root,20);
insert (root,50);
inorder(root);
printf("\n");
return 0;
Output:
10 12 15 20 45 50 55 79
Deletion in Binary Search tree:
Step:1
Delete node 90, as the node to be deleted is a leaf node, so it will be replaced
with NULL, and the allocated space will free.
Step:2
Delete node 79, the node to be deleted has only one child, so it will be replaced
with its child 55.
Step: 3
Delete node 45 that is the root node, as the node to be deleted has two
children, so it will be replaced with its in-order successor. Now, node 45 will be
at the leaf of the tree so that it can be deleted easily.
Step:4
Delete node 15
Step:5
Delete Node: 50
Step:6
Delete: Node 20
Step:7
Delete Node: 12
Step:8
Delete: Node 55
55 10
10
Step:9
Delete: Node 10
10
No specific order;
Efficient lookup using the binary
full tree traversal
search property, reducing the
Node may be needed to
search space by half at each step.
Lookup/Search find a node.
Example:
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
temp->data = data;
return temp;
if (root != NULL)
inorderTraversal (root->left);
inorderTraversal (root->right);
}
if (root == NULL)
return root;
else
if (root->left == NULL)
free (root);
return temp;
}
free (root); // it will free the memory before executing the program
return temp;
temp = temp->left;
root->data = temp->data;
return root;
int main ()
inorderTraversal (root);
inorderTraversal (root);
return 0;
Output:
Example:
#include <stdio.h>
#include <stdlib.h>
struct Node
int data;
};
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct Node* insert(struct Node* root, int value)
if (root == NULL)
return createNode(value);
return root;
return root;
}
if (value < root->data)
if (root == NULL)
return root;
else
{
if (root->left == NULL)
free(root);
return temp;
free(root);
return temp;
temp = temp->left;
root->data = temp->data;
return root;
}
if (root == NULL)
return;
inorderTraversal(root->left);
inorderTraversal(root->right);
int main()
inorderTraversal(root);
if (searchResult != NULL)
{
else
inorderTraversal(root);
preorderTraversal(root);
postorderTraversal(root);
return 0;
}
Output:
20 30 40 50 60 70 80
Element found: 60
40 60 70 80
60 40 70 80
40 80 70 60
Application of Tree:
Trees find applications in various fields, including file systems,
databases, network routing, artificial intelligence, syntax parsing, and
more.
They are used to organize and optimize data storage and retrieval,
implement efficient searching algorithms, and represent relationships
between entities.
The highest node in a tree that has no parent nodes is considered the root
of the tree. Every tree has a single root node.
Parent Node: A node's parent one is the node that came before it in the
tree of nodes.
Child Node: The node that is a node's direct successor is referred to as a
node's child node.
Siblings are the children of the same parent node.
Edge: Edge serves as a connecting node between the parent and child
nodes.
Leaf: A node without children is referred to as a leaf node. It is the tree's
last node. A tree may have several leaf nodes.
A node's subtree is the tree that views that specific node as the root node.
Depth: The depth of a node is the separation between it and the root
node.
Height: The height of a node is the distance between it and the subtree's
deepest node.
The maximum height of any node is referred to as the tree's height. The
height of the root node is the same as this.
Level: In the tree, a level is the number of parents that correspond to a
particular node.
Node degree: A node's degree is determined by how many children it
has.
A binary tree has (N+1) NULL nodes, where N is the total number of
nodes in the tree.
Qns:
Trees-Evaluation of Expression:
Leaf Nodes: The leaf nodes of an expression tree are operands (e.g., constants
or variables).
Internal Nodes: The internal nodes represent operators, where each operator
node combines the results of its child nodes.
Binary Expression Tree:
This is the most common type of expression tree. Each operator node in
the tree has exactly two children.
For example, the expression a + (b * c) can be represented as…
For example:
Example:1 ( In-Order)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left;
struct node *right;
};
struct node *getNode(int val) //return a new node with the given value
{
struct node *newNode;
newNode = malloc (sizeof(struct node));
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node *insertNode(struct node *root, int val)
//inserts nodes in the binary search tree
{
if(root == NULL)
return getNode(val);
if(root->key < val)
root->right = insertNode(root->right, val);
if(root->key > val)
root->left = insertNode(root->left, val);
return root;
}
void inorder (struct node *root) //inorder traversal of the binary search tree
{
if(root == NULL)
return;
inorder(root->left); //traverse the left subtree
printf("%d ",root->key); //visit the root
inorder(root->right); //traverse the right subtree
}
int main()
{
struct node *root = NULL;
int data;
char ch;
do
{
printf ("\n Select one of the operations::");
printf ("\n 1. To insert a new node in the Binary Tree");
printf("\n2.To display the nodes of the Binary Tree(via Inorder Traversal).\n");
int choice;
scanf("%d", &choice);
switch (choice)
{
case1:
printf("\n Enter the value to be inserted\n");
scanf("%d", &data);
root = insertNode(root, data);
break;
case2:
printf("\n In order Traversal of the Binary Tree::\n");
inorder(root);
break;
default:
printf("Wrong Entry\n");
break;
}
printf("\n Do you want to continue (Type y or n)\n");
scanf(" %c", &ch);
}
while (ch == 'Y'|| ch == 'y');
return 0;
}
Output:
Select one of the operations:
1. To insert a new node in the Binary Tree
2. To display the nodes of the Binary Tree (via In order Traversal).
1
Enter the value to be inserted
10
Do you want to continue (Type y or n)
y
Select one of the operations:
1. To insert a new node in the Binary Tree
2. To display the nodes of the Binary Tree(via Inorder Traversal).
1
Enter the value to be inserted
30
Do you want to continue (Type y or n)
y
Select one of the operations::
1. To insert a new node in the Binary Tree
2. To display the nodes of the Binary Tree(via Inorder Traversal).
1
Enter the value to be inserted
50
Do you want to continue (Type y or n)
y
Select one of the operations:
1. To insert a new node in the Binary Tree
2. To display the nodes of the Binary Tree(via Inorder Traversal).
1
Enter the value to be inserted
70
Do you want to continue (Type y or n)
y
Select one of the operations:
1. To insert a new node in the Binary Tree
2. To display the nodes of the Binary Tree(via Inorder Traversal).
1
Enter the value to be inserted
90
Do you want to continue (Type y or n)
y
Select one of the operations:
1. To insert a new node in the Binary Tree
2. To display the nodes of the Binary Tree(via Inorder Traversal).
1
Enter the value to be inserted
55
Do you want to continue (Type y or n)
y
Select one of the operations:
1. To insert a new node in the Binary Tree
2. To display the nodes of the Binary Tree(via Inorder Traversal).
2
Inorder Traversal of the Binary Tree::
10 30 50 55 70 90
Do you want to continue (Type y or n)
Example:2
#include <stdio.h>
#include <stdlib.h>
// A binary tree node has data, pointer to left child
struct node // and a pointer to right child
{
int data;
struct node* left;
struct node* right;
};
// Helper function that allocates a new node with the
// given data and NULL left and right pointers.
struct node* newNode(int data)
{
struct node* node= (struct node*) malloc (sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
void printInorder(struct node* node) // Given a binary tree, print its nodes in inorder
{
if (node == NULL)
return;
printInorder(node->left); // First recur on left child
printf("%d ", node->data); // Then print the data of node
printInorder(node->right); // Now recur on right child
}
int main() // Driver code
{
struct node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("Inorder traversal of binary tree is: \n"); // Function call
printInorder(root);
getchar();
return 0;
}
Output:
In order traversal of binary tree is:
4 2 5 1 3
Example:3 (Pre-order Traversal (P-L-R):
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
struct node* newNode(int data) // given data and NULL left and right pointers.
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
void printInorder(struct node* node) // Given a binary tree, print its nodes in
inorder
if (node == NULL)
return;
printInorder(node->left); // First recur on left child
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printInorder(root);
getchar();
return 0;
Output:
4 2 5 1 3
Example:4
#include<stdio.h>
#include<stdlib.h>
struct node
int key;
};
struct node *getNode(int val) //return a new node with the given value
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node *insertNode(struct node *root, int val) //inserts nodes in the
binary search tree
if(root == NULL)
return getNode(val);
return root;
void preorder (struct node *root) //preorder traversal of the binary search tree
if(root == NULL)
return;
}
int main()
int data;
char ch;
/* Do while loop to display various options to select from to decide the input
*/
do
int choice;
scanf("%d", &choice);
switch (choice)
case1:
scanf("%d", &data);
break;
case2:
preorder(root);
break;
default:
printf("Wrong Entry\n");
break;
return 0;
Output:
10
30
40
70
60
10 30 20 40 70 60
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
if (node == NULL)
return;
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printPostorder(root);
getchar();
return 0;
Output:
Post order traversal of binary tree is
4 5 2 3 1
Example:2
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
};
struct node *getNode(int val) //return a new node with the given value
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
if(root == NULL)
return getNode(val);
if(root == NULL)
return;
int main()
int data;
char ch;
/* Do while loop to display various options to select from to decide the input */
do
int choice;
scanf("%d", &choice);
switch (choice)
case1:
scanf("%d", &data);
break;
case2:
postorder(root);
break;
default:
printf("Wrong Entry\n");
break;
}
while (ch == 'Y'|| ch == 'y');
return 0;
Output:
10
30
20
50
60
y
Select one of the operations::
55
20 55 60 50 30 10
TreeNode* insertNode(TreeNode* root, int data) //Insert a Node into the Tree
{
if (root == NULL) // If the tree is empty, return a new node
{
return createNode(data);
}
if (data < root->data) // Otherwise, recur down the tree
{
root->left = insertNode(root->left, data);
}
else
{
root->right = insertNode(root->right, data);
}
return root;
}
int main()
{
TreeNode* root = NULL;
root = insertNode(root, 50); // Insert nodes into the tree
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
printf("In-order traversal:\n "); // Print the in-order traversal of the tree
inorderTraversal(root);
printf("\n");
deleteTree(root); // Delete the entire tree
return 0;
}
Output:
In-order traversal:
20 30 40 50 60 70 80
Binary Search Tree Example:7
#include <stdio.h>
#include <stdlib.h>
typedef struct Node // Define the structure for a node in the binary search tree
{
int value;
struct Node *left;
struct Node *right;
} Node;
Node* insert(Node* root, int value) // Function to insert a value into the BST
{
if (root == NULL) // If the tree is empty, return a new node
{
return createNode(value);
}
if (value < root->value) // Otherwise, recur down the tree
{
root->left = insert(root->left, value);
}
else if (value > root->value)
{
root->right = insert(root->right, value);
}
return root; // Return the (unchanged) node pointer
}
void freeTree(Node* root) // Function to free the allocated memory of the BST
{
if (root != NULL)
{
freeTree(root->left);
freeTree(root->right);
free(root);
}
}
int main()
{
Node* root = NULL;
root = insert(root, 50); // Insert values into the BST
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
printf("In-order traversal: "); // Perform in-order traversal
inorderTraversal(root);
printf("\n");
freeTree(root); // Free allocated memory
return 0;
}
Output:
In-order traversal:
20 30 40 50 60 70 80
Example:8
Binary Search Tree (in-Order, Pre-Order and Post-Order-)
#include <stdio.h>
#include <stdlib.h>
typedef struct Node // Define the structure for a node in the BST
{
int data;
struct Node* left;
struct Node* right;
} Node;
void freeTree(Node* root) // Function to free the memory allocated for the BST
{
if (root != NULL)
{
freeTree(root->left);
freeTree(root->right);
free(root);
}
}
int main()
{
Node* root = NULL;
root = insertNode(root, 50); // Insert nodes into the BST
insertNode(root, 30);
insertNode(root, 70);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 60);
insertNode(root, 80);
printf("In-order traversal: "); // Perform in-order traversal
inOrderTraversal(root);
printf("\n");
printf("Pre-order traversal: "); // Perform pre-order traversal
preOrderTraversal(root);
printf("\n");
printf("Post-order traversal: "); // Perform post-order traversal
postOrderTraversal(root);
printf("\n");
freeTree(root); // Free allocated memory
return 0;
}
Output:
In-order traversal: 20 30 40 50 60 70 80
Pre-order traversal: 50 30 20 40 70 60 80
Post-order traversal: 20 40 30 60 80 70 50
Example:9
Binary search Tree Operations (Insertion, Deletion, Traverse and Search)
#include <stdio.h>
#include <stdlib.h>
typedef struct Node // Define the node structure
{
int data;
struct Node* left;
struct Node* right;
} Node;
Output:
Inorder traversal: 20 30 40 50 60 70 80
Deleting: 20
Inorder traversal: 30 40 50 60 70 80
Deleting: 30
Inorder traversal: 40 50 60 70 80
Deleting: 50
Inorder traversal: 40 60 70 80
Found 70 in the tree.
Example: (Practice Program) Threaded Binary Tree
#include <stdio.h>
#include <stdlib.h>
struct node *case_a(struct node *root, struct node *par,struct node *ptr);
struct node *case_c(struct node *root, struct node *par,struct node *ptr);
struct node
boolean lthread;
int info;
boolean rthread;
};
int main( )
{
int choice,num;
while(1)
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Inorder Traversal\n");
printf("4.Preorder Traversal\n");
printf("5.Quit\n");
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&num);
root = insert(root,num);
break;
case 2:
root = del(root,num);
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
exit(1);
default:
printf("\nWrong choice\n");
return 0;
int found=0;
ptr = root;
par = NULL;
while( ptr!=NULL )
found =1;
break;
par = ptr;
if(ptr->lthread == false)
ptr = ptr->left;
else
break;
else
if(ptr->rthread == false)
ptr = ptr->right;
else
break;
}
if(found)
printf("\nDuplicate key");
else
tmp->info=ikey;
tmp->lthread = true;
tmp->rthread = true;
if(par==NULL)
root=tmp;
tmp->left=NULL;
tmp->right=NULL;
tmp->left=par->left;
tmp->right=par;
par->lthread=false;
par->left=tmp;
}
else
tmp->left=par;
tmp->right=par->right;
par->rthread=false;
par->right=tmp;
return root;
int found=0;
ptr = root;
par = NULL;
while( ptr!=NULL)
found =1;
break;
par = ptr;
if(ptr->lthread == false)
ptr = ptr->left;
else
break;
else
if(ptr->rthread == false)
ptr = ptr->right;
else
break;
if(found==0)
root = case_c(root,par,ptr);
else if(ptr->lthread==false )
else if(ptr->rthread==false)
else
root = case_a(root,par,ptr);
return root;
struct node *case_a(struct node *root, struct node *par,struct node *ptr )
if(par==NULL)
root=NULL;
else if(ptr==par->left)
par->lthread=true;
par->left=ptr->left;
else
par->rthread=true;
par->right=ptr->right;
}
free(ptr);
return root;
if(ptr->lthread==false)
child=ptr->left;
else
child=ptr->right;
if(par==NULL )
root=child;
par->left=child;
else
par->right=child;
s=in_succ(ptr);
p=in_pred(ptr);
if(ptr->lthread==false)
p->right=s;
else
{
if(ptr->rthread==false)
s->left=p;
free(ptr);
return root;
struct node *case_c(struct node *root, struct node *par,struct node *ptr)
parsucc = ptr;
succ = ptr->right;
while(succ->left!=NULL)
parsucc = succ;
succ = succ->left;
ptr->info = succ->info;
else
return root;
}
if(ptr->rthread==true)
return ptr->right;
else
ptr=ptr->right;
while(ptr->lthread==false)
ptr=ptr->left;
return ptr;
if(ptr->lthread==true)
return ptr->left;
else
ptr=ptr->left;
while(ptr->rthread==false)
ptr=ptr->right;
return ptr;
if(root == NULL )
printf("Tree is empty");
return;
ptr=root;
while(ptr->lthread==false)
ptr=ptr->left;
while( ptr!=NULL )
printf("%d ",ptr->info);
ptr=in_succ(ptr);
{
struct node *ptr;
if(root==NULL)
printf("Tree is empty");
return;
ptr=root;
while(ptr!=NULL)
printf("%d ",ptr->info);
if(ptr->lthread==false)
ptr=ptr->left;
else if(ptr->rthread==false)
ptr=ptr->right;
else
ptr=ptr->right;
if(ptr!=NULL)
ptr=ptr->right;
}
}
Output:
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Quit
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Quit
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Quit
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Quit
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Quit
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Quit
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Quit
10 20 30 50 70 80
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Quit
Types of Trees
1. General tree
2. Binary tree
3. Binary Search Tree
4. AVL Tree
5. B Tree
6. Red Black Tree
7. Expression Tree
8. Tournament Tree
9. Forest Tree
10.Splay tree
General tree:
Binary Tree:
A Binary Tree is a type of tree data structure where each node can
have a maximum of two child nodes, a left child node and a right child node.
This restriction, that a node can have a maximum of two child nodes,
B Tree:
B-tree is a self-balancing tree data structure that maintains sorted data
and allows searches, sequential access, insertions, and deletions in logarithmic
time. The B-tree generalizes the binary search tree, allowing for nodes with
more than two children.
Red Black Tree:
Case 1: Check whether the tree is empty; make the current node as the root and
Case 2: But if the tree is not empty, we create a new node and colour it red.