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

Tree

The document outlines the properties and algorithms related to binary search trees (BST), including searching, traversals (preorder, inorder, postorder), and the definition of binary trees. It also discusses the differences between complete and perfect binary trees, the structure of BSTs, and provides algorithms for insertion, deletion, and traversal. Additionally, it covers the array implementation of binary trees, AVL trees, and the construction of binary trees from traversal sequences.

Uploaded by

rahul banerjee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Tree

The document outlines the properties and algorithms related to binary search trees (BST), including searching, traversals (preorder, inorder, postorder), and the definition of binary trees. It also discusses the differences between complete and perfect binary trees, the structure of BSTs, and provides algorithms for insertion, deletion, and traversal. Additionally, it covers the array implementation of binary trees, AVL trees, and the construction of binary trees from traversal sequences.

Uploaded by

rahul banerjee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Tree

1.list the properties of binary search tree. Write an algorithm to


search an element from a binary search tree.
A Binary Search Tree (BST) is a type of binary tree with the following key properties:

1. Node Ordering:
For each node in the tree:
o The values of all nodes in its left subtree are less than or equal to the node's
value.
o The values of all nodes in its right subtree are greater than the node's value.
2. Binary Structure:
Each node has at most two children:
o A left child.
o A right child.
3. No Duplicate Values (Optional):
Some implementations of a BST disallow duplicate values, while others allow
duplicates to be stored in a specific manner (e.g., always in the left or right subtree).
4. Recursive Definition:
A BST is recursively defined:
o The left subtree is also a BST.
o The right subtree is also a BST.
5. Efficient Search:
The BST property allows efficient searching, insertion, and deletion operations:
o Time Complexity:
▪ Average case: O(log⁡n)O(\log n)O(logn) (for balanced trees).
▪ Worst case: O(n)O(n)O(n) (if the tree is skewed, resembling a linked
list).
6. Inorder Traversal Yields Sorted Order:
If you perform an inorder traversal of a BST (left subtree → root → right subtree),
the values of the nodes will be visited in ascending sorted order.
7. Dynamic Structure:
A BST can dynamically grow and shrink as nodes are added or removed.
8. Height of the Tree:
o The efficiency of a BST depends on its height. A balanced BST (where the
height is minimized) ensures better performance for operations.

Algorithm to Search for an Element in a Binary Search Tree

1. Input: Root of the tree (root) and the value to search (val).
2. Output: Pointer to the node containing val, or NULL if not found.

Steps:

1. Initialize a pointer (p) to the root of the tree.


2. While the pointer is not NULL:
o If the data of the current node (p->data) is equal to val, return the pointer p.
o If the data of the current node is greater than val, move the pointer to the left
child (p = p->left).
o Otherwise, move the pointer to the right child (p = p->right).
3. If the loop ends and the value is not found, return NULL.

2. Draw the binary tree whose sequential representation is given


below.

3.Write the non recursive preorder, inorder, postorder traversal


algorithm.
4.Write an iterative algorithm to perform inorder, preorder,
postorder traversal of a binary tree.
1. Postorder Traversal (Recursive)

In postorder traversal, the order is: Left → Right → Root.

Algorithm:

1. If the current node is NULL, return.


2. Recursively traverse the left subtree.
3. Recursively traverse the right subtree.
4. Process the current node (visit the node).

void postorder(struct Node* root) {

if (root == NULL) return;

// Traverse left subtree

postorder(root->left);

// Traverse right subtree

postorder(root->right);

// Visit the current node

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

2. Inorder Traversal (Recursive)

In inorder traversal, the order is: Left → Root → Right.

Algorithm:

1. If the current node is NULL, return.


2. Recursively traverse the left subtree.
3. Process the current node (visit the node).
4. Recursively traverse the right subtree.

void inorder(struct Node* root) {

if (root == NULL) return;

// Traverse left subtree

inorder(root->left);

// Visit the current node

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

// Traverse right subtree

inorder(root->right);

3. Preorder Traversal (Recursive)

In preorder traversal, the order is: Root → Left → Right.

Algorithm:

1. If the current node is NULL, return.


2. Process the current node (visit the node).
3. Recursively traverse the left subtree.
4. Recursively traverse the right subtree.

void preorder(struct Node* root) {

if (root == NULL) return;

// Visit the current node

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

// Traverse left subtree

preorder(root->left);
// Traverse right subtree

preorder(root->right);

5.Write the algorithm for in-order traversal of a threaded binary tree.

Algorithm:

1. Start from the leftmost node of the tree.


2. Repeat the following steps until the current node is NULL:
o Visit the current node (process or print its data).
o If the current node has a right thread, follow the thread to the next inorder
successor.
o Otherwise, move to the leftmost node in the right subtree.

void inorderThreaded(struct Node* root) {

struct Node* curr = root;

// Go to the leftmost node

while (curr != NULL) {

// Find the leftmost node

while (curr->left != NULL)

curr = curr->left;

// Visit nodes using threads

while (curr != NULL) {

printf("%d ", curr->data); // Process the current node

// If there is a right thread, follow it

if (curr->rightThread)

curr = curr->right;

else {

// Otherwise, move to the leftmost node in the right subtree

curr = curr->right;

while (curr != NULL && curr->left != NULL)


curr = curr->left;

5.What is the output obtained after preorder, inorder and postorder


traversal of the following tree.

Pre-order-5 7 3 1 6 9 2 4 8
In-order-1 3 6 7 5 2 9 4 8
Post-order-1 6 3 7 2 8 4 9 5

6.What is a Binary Tree?


A tree is a binary tree if each node has 0child,one child or two child.

7.Define the differences between complete binary tree and perfect


binary tree with examples.
8. What is a Binary Search Tree (BST)? Show the structure of the
binary search tree after adding each of the following values in that
order: 10, 25, 2, 4, 7, 13, 11, 22. What is the height of the created
binary search tree?
A Binary Search Tree (BST) is a type of binary tree in which the nodes are arranged in a
specific order to allow for efficient searching, insertion, and deletion operations. The key
property of a BST is that for every node:

• Left Subtree: All the nodes in the left subtree of a node have values less than the
node's value.
• Right Subtree: All the nodes in the right subtree of a node have values greater than
the node's value.
• the height of the BST is 4.

9.Write a function (C/pseudocode) to insert an element, delete an element and search an


element into a BST.

Search an element

struct node* search(struct node*root, int val) {

struct node *p = root, *; while(p!=NULL)

if(p->data==val)

return P

else if(p->data > val)


p=p->left;
else
p=p->right;

return P;

}
Insert an element
struct node* insertNode(struct node*root) {

struct node* ptr = (struct node*)malloc(sizeof(struct node)); printf("Enter the value: ");

scanf("%d",&ptr->data); 12

ptr->left = NULL;

ptr->right = NULL;

if(root==NULL) root = ptr;


else

{
struct node*p = root, *q = root;
}
while(p!=NULL) {

if(p->data > ptr->data)

q=p;

p=p->left;

else if(p->data < ptr->data) {

q=p;

p=p->right;

if(q->data > ptr->data)

{
q->left = ptr;
}
else {
q->right = ptr;
}
}
return root;

}
Delete a value
10.Prove that the maximum no. of nodes in a binary tree of depth k
is 2^k-1.

Proof by Mathematical Induction

Base Case (k=1k = 1k=1):

• For a binary tree of depth k=1k = 1k=1, there is only the root node, so: N=21−1=1N = 2^1 - 1
= 1N=21−1=1
• The formula holds true for k=1k = 1k=1.

Inductive Step:
Prove that for depth k+1k + 1k+1, the maximum number of nodes is:

N=2k+1−1N = 2^{k+1} - 1N=2k+1−1

1. A binary tree of depth k+1k + 1k+1 has a root node at the top (level 1) and kkk levels below
it.
2. At depth k+1k + 1k+1, the total number of nodes is the sum of:
o The maximum number of nodes in a binary tree of depth kkk (from the inductive
hypothesis): 2k−12^k - 12k−1.
o The number of nodes in the new bottom level (level k+1k+1k+1), which is 2k2^k2k
(since each node at level kkk can have 2 children).

Thus, the total number of nodes at depth k+1k + 1k+1 is:

N=(2k−1)+2kN = (2^k - 1) + 2^kN=(2k−1)+2k

Simplify:

N=2k−1+2k=2⋅2k−1=2k+1−1N = 2^k - 1 + 2^k = 2 \cdot 2^k - 1 = 2^{k+1} -


1N=2k−1+2k=2⋅2k−1=2k+1−1

11.Explain the array implementation of a binary tree. Why is it not a


good representation for Binary Trees in general?

Array Implementation of a Binary Tree

In the array implementation of a binary tree:

1. Nodes are stored sequentially in an array or list.


2. The index of each node in the array determines its relationship with other nodes in the tree.

Node Relationships in the Array

For a node stored at index iii (1-based indexing):

1. Parent: The parent of the node at index iii is at index ⌊i/2⌋\lfloor i / 2 \rfloor⌊i/2⌋ (except for
the root, which has no parent).
2. Left Child: The left child of the node at index iii is at index 2i2i2i.
3. Right Child: The right child of the node at index iii is at index 2i+12i + 12i+1.

For 0-based indexing, the relationships are slightly modified:

1. Parent: The parent of the node at index iii is at index ⌊(i−1)/2⌋\lfloor (i - 1) / 2


\rfloor⌊(i−1)/2⌋.
2. Left Child: The left child of the node at index iii is at index 2i+12i + 12i+1.
3. Right Child: The right child of the node at index iii is at index 2i+22i + 22i+2.
Why is Array Representation Not Ideal for General Binary Trees?

• Most binary trees are not complete, and gaps in the array waste memory.
• Pointer-based (linked list) representations are more flexible, as they only allocate memory
for actual nodes without leaving unused slots.
• In a pointer-based representation, unbalanced or dynamic trees can be easily handled
without resizing or wasting space.

12.Construct a binary tree using the Inorder and Postorder traversal


of the nodes given below:
Inorder: D B F E A G C L J H K
Postorder: D F E B G L J K H C A
13. Write the difference between B-tree and B+ tree.

13.Write down the key differences between AVL trees and binary
search trees.
14.Construct an AVL tree using the list: 12, 11, 13, 10, 9, 15, 14, 18, 7, 6, 5, 4.
15.The post-order and in-order traversal sequences of nodes in a
binary tree are given below:
Postorder: D G E B H I F C A Inorder: D B G E A C H F I
The in-order & pre-order traversal sequences of nodes in a binary tree are given as follows : Draw
the binary tree. State the algorithm to construct the tree.

To construct the binary tree from the given in-order and pre-order traversal sequences,
follow these steps:

Algorithm to Construct the Tree

1. Identify the root from the pre-order sequence:


o The first element of the pre-order sequence is always the root of the tree.
2. Locate the root in the in-order sequence:
o This divides the in-order sequence into two parts:
▪ Left subtree (elements before the root in in-order).
▪ Right subtree (elements after the root in in-order).
3. Recursively construct the left and right subtrees:
o Use the corresponding portions of the pre-order and in-order sequences to build the
left and right subtrees.
4. Base case for recursion:
o If the sequence is empty, return None.

Steps to Construct the Tree

Given sequences:

• In-order: D, G, B, A, H, E, I, C, F
• Pre-order: A, B, D, G, C, E, H, I, F
Step-by-step Construction

1. Root: The first element of pre-order is A. This is the root of the tree.
o Find A in the in-order sequence. The left subtree is {D, G, B}, and the right
subtree is {H, E, I, C, F}.
2. Left Subtree of A:
o Pre-order for left subtree: {B, D, G}
o In-order for left subtree: {D, G, B}
o Root is B.
▪ Split in-order into {D, G} (left subtree) and [] (right subtree).
▪ Recursive construction gives:
▪ Root: B
▪ Left child: D
▪ Right child: G
3. Right Subtree of A:
o Pre-order for right subtree: {C, E, H, I, F}
o In-order for right subtree: {H, E, I, C, F}
o Root is C.
▪ Split in-order into {H, E, I} (left subtree) and {F} (right subtree).
▪ Recursive construction gives:
▪ Root: C
▪ Left subtree:
▪ Pre-order: {E, H, I}
▪ In-order: {H, E, I}
▪ Root: E
▪ Left child: H
▪ Right child: I
▪ Right child: F
Insert the following keys in order given below to build them into an
AVL tree : g, h, s, l, e, m, t, u

How does an AVL tree differ from a binary search tree? Insert the
following keys in the order given below to build them into an AVL
tree:
8 12 9 11 7 6 66 2 1 44
Clearly mention different rotation used and balance factor of each
node.
Write Short notes on –
1. AVL Tree
An AVL (Adelson-Velskii and Landis) tree is a self-balancing binary search tree data structure. It
ensures that the height of the tree remains relatively small by rotating nodes when the balance
factor becomes too large.
Properties of AVL Trees:

1. Balanced: AVL trees are self-balancing, meaning that the height of the tree remains relatively
small.
2. Binary Search Tree: AVL trees are a type of binary search tree, meaning that all elements to the left
of a node are smaller, and all elements to the right are larger.
3. Height Balance: AVL trees maintain a balance between the height of the left and right subtrees.

Advantages of AVL Trees:

1. Efficient Search: AVL trees allow for efficient search, with an average time complexity of O(log n).
2. Efficient Insertion and Deletion: AVL trees can insert and delete nodes efficiently, with an average
time complexity of O(log n).
3. Balanced Tree: AVL trees maintain a balanced tree, which ensures that search, insertion, and
deletion operations are efficient.
Common Operations on AVL Trees:

1. Insertion: Inserting a new node into the tree.


2. Deletion: Deleting a node from the tree.
3. Search: Searching for a specific node in the tree.

Threaded binary tree:


A threaded binary tree is a type of binary tree where some of the null child pointers are replaced
with pointers to other nodes in the tree. These pointers are called "threads".

Properties of Threaded Binary Trees:

1. Efficient Traversal: Threaded binary trees allow for efficient traversal of the tree, as the threads
provide a direct link to the next node in the traversal.
2. Reduced Memory Usage: Threaded binary trees can reduce memory usage, as the threads
replace null child pointers.
3. Faster Search: Threaded binary trees can facilitate faster search operations, as the threads
provide a direct link to the next node in the search.

Types of Threaded Binary Trees:

1. Left-Threaded Binary Tree: A left-threaded binary tree is a threaded binary tree where the left
child pointer of a node is replaced with a thread to the in-order predecessor of the node.
2. Right-Threaded Binary Tree: A right-threaded binary tree is a threaded binary tree where the
right child pointer of a node is replaced with a thread to the in-order successor of the node.

Advantages of Threaded Binary Trees:

1. Efficient Traversal: Threaded binary trees allow for efficient traversal of the tree.
2. Reduced Memory Usage: Threaded binary trees can reduce memory usage.
3. Faster Search: Threaded binary trees can facilitate faster search operations.

Disadvantages of Threaded Binary Trees:


1. Complex Implementation: Threaded binary trees can be complex to implement.
2. Difficult to Debug: Threaded binary trees can be difficult to debug due to the
complexity of the threads.

You might also like