0% found this document useful (0 votes)
1 views10 pages

Tree Adv.algorithm

A tree is a non-linear data structure consisting of nodes connected by edges, with one root node and no cycles. General trees allow any number of children per node, while binary trees restrict each node to at most two children. Key operations for binary trees include insertion, traversal (inorder, preorder, postorder), and deletion, along with definitions for concepts like depth, height, weight, forest, degree, root node, and leaf node.

Uploaded by

vrajpatelmyindia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views10 pages

Tree Adv.algorithm

A tree is a non-linear data structure consisting of nodes connected by edges, with one root node and no cycles. General trees allow any number of children per node, while binary trees restrict each node to at most two children. Key operations for binary trees include insertion, traversal (inorder, preorder, postorder), and deletion, along with definitions for concepts like depth, height, weight, forest, degree, root node, and leaf node.

Uploaded by

vrajpatelmyindia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1. what is tree ? explain general tree and binary tree with example.

Ans:

In Advanced Algorithms, a tree is a fundamental non-linear data structure that simulates a hierarchical tree
structure with a set of connected nodes. Each tree structure is made up of nodes connected by edges.

🔷 What is a Tree?

A Tree is a non-linear data structure where:

 One node is designated as the root.


 Each node (except the root) has exactly one parent.
 Nodes may have zero or more children.
 It has no cycles (i.e., it’s a connected acyclic graph).

🔷 Types of Trees

1.

General Tree

A general tree is a tree data structure where:

 Each node can have any number of children.


 There is no fixed limit on the number of child nodes.

🌳 Example:

/ | \

B C D

/\ |

E F G

 Here, node A has 3 children: B, C, and D.


 Node B has 2 children: E and F.
 Node D has 1 child: G.
 It’s a general tree because there’s no restriction on how many children a node can have.

2.

Binary Tree

A binary tree is a special kind of tree in which:

 Each node has at most 2 children.


 These children are referred to as the left child and right child.

🌲 Example:
1
/\
2 3
/\ \
4 5 6

 Node 1 has two children: 2 (left) and 3 (right).


 Node 2 has two children: 4 (left) and 5 (right).
 Node 3 has one right child: 6.
 Each node has ≤ 2 children, so this is a binary tree.

2 what is binary tree ? explain various oprations of binary tree with suitable example .

Ans:

A Binary Tree is a hierarchical data structure in which each node has at most two children, referred to
as the left child and right child.

✅ Key Features:

 Each node contains:


o Data
o Pointer to left child
o Pointer to right child
 The structure is recursive in nature.
 Root node is the topmost node.
 A binary tree is often used as the basis for more advanced trees like:
o Binary Search Trees (BST)
o AVL Trees
o Heaps

🧩 Binary Tree Node Structure (C-style)

struct Node {
int data;
struct Node* left;
struct Node* right;
};

Binary Tree Operations (with examples)


1. Insertion

Purpose: Add a node to the tree (usually in level-order if not a BST).


Example:

// Create a new node


struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
}

Tree before Insertion:

10
/ \
20 30

Inserting 40 (as left of 20):

10
/ \
20 30
/
40

2. Traversal

Traversal is visiting every node in a specific order.

a. Inorder (LNR)

: Left → Node → Right

void inorder(struct Node* root) {


if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

b. Preorder (NLR)

: Node → Left → Right

void preorder(struct Node* root) {


if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

c. Postorder (LRN)

: Left → Right → Node

void postorder(struct Node* root) {

if (root != NULL) {

postorder(root->left);

postorder(root->right);

printf("%d ", root->data);

/\

2 3

 Inorder: 2 1 3
 Preorder: 1 2 3
 Postorder: 2 3 1

3. Deletion

Note: Deleting a node in a general binary tree is complex. It usually involves:


 Finding the deepest rightmost node.
 Replacing the node to be deleted with that node.
 Then deleting the deepest rightmost node.

In Binary Search Trees, deletion has specific rules based on:

 Node with 0, 1, or 2 children.

3 write an algorithm for tree travalsal oprations .

Ans:

// Define structure of a tree node

struct Node {

int data;

struct Node* left;

struct Node* right;

};

// Create a new node with given data

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

INORDER(node)

if node is not NULL then

INORDER(node->left)

visit(node)

INORDER(node->right)

PREORDER(node)
if node is not NULL then
visit(node)
PREORDER(node->left)
PREORDER(node->right)
POSTORDER(node)
if node is not NULL then
POSTORDER(node->left)
POSTORDER(node->right)
visit(node)
example
int main() {
// Creating the tree manually
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);

printf("Inorder Traversal: ");


inorderTraversal(root);
printf("\n");

printf("Preorder Traversal: ");


preorderTraversal(root);
printf("\n");

printf("Postorder Traversal: ");


postorderTraversal(root);
printf("\n");
return 0;
}
1
/\
2 3
/\
4 5

 Inorder: 4 2 5 1 3
 Preorder: 1 2 4 5 3
 Postorder: 4 5 2 3 1

4 explain following terms depth of tree , height of tree , weight of tree , forest , degree , root node ,
leaf node in advance algorithm tree concept

Ans:

1️⃣ Depth of a Node

🔹 Definition: The depth of a node is the number of edges


from the root node to that particular node.

 Depth(root) = 0
 Depth increases by 1 for each level down the tree

🧩 Example:

A (depth 0)

/\

B C (depth 1)

D (depth 2)

2️⃣ Height of a Node / Tree

🔹 Definition: The height of a node is the number of edges on


the longest path from that node to a leaf node.
 Height of a leaf node = 0
 Height of a tree = height of the root node
🧩 Example:

/\

B C

 Height of node D = 0
 Height of node B = 1
 Height of node A (tree height) = 2

3️⃣ Weight of a Tree

 🔹 Definition: The weight of a tree is the total number of


nodes in the tree.
 🧩 Example:
A
/\
B C
/
D

 Total nodes = 4 ⇒ Weight = 4

4️⃣ Forest

🔹 Definition: A forest is a collection of disjoint trees (i.e., a


set of trees not connected together).
 If you remove the root of a tree, its subtrees form a forest.
 Used in disjoint set algorithms, graph partitioning, etc.

🧩 Example:

Tree 1: Tree 2:

A X

/\ /\

B C Y Z

5️⃣ Degree of a Node

🔹 Definition: The degree of a node is the number of children


it has.
🧩 Example:

/\

B C

 Degree of A = 2 (B, C)
 Degree of C = 1 (D)
 Degree of B = 0 (leaf)

6️⃣ Root Node

🔹 Definition: The root node is the topmost node in a tree


with no parent.
 Every tree has exactly one root
 All other nodes are descendants of the root

🧩 Example:

/\

B C. A is the root node

7️⃣ Leaf Node

🔹 Definition: A leaf node (also called external node) is a node


that has no children.
 Degree of a leaf node = 0
 They appear at the bottom of the tree

🧩 Example:

/\

B C

D. Leaf nodes: B, D

You might also like