Tree
Tree
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(logn)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.
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:
Algorithm:
postorder(root->left);
postorder(root->right);
Algorithm:
inorder(root->left);
inorder(root->right);
Algorithm:
preorder(root->left);
// Traverse right subtree
preorder(root->right);
Algorithm:
curr = curr->left;
if (curr->rightThread)
curr = curr->right;
else {
curr = curr->right;
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
• 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.
Search an element
if(p->data==val)
return P
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;
{
struct node*p = root, *q = root;
}
while(p!=NULL) {
q=p;
p=p->left;
q=p;
p=p->right;
{
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.
• 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:
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).
Simplify:
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.
• 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.
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:
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.
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. 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.
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.
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.