0% found this document useful (0 votes)
48 views20 pages

Unit

The document covers various concepts related to binary trees, including definitions, properties, and representations using arrays and linked lists. It discusses operations on binary search trees, AVL trees, and tree traversals, along with algorithms for insertion, deletion, and searching. Additionally, it explains the use of binary trees in sorting and expression evaluation, along with the heap sort algorithm.

Uploaded by

manasaveena.t
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)
48 views20 pages

Unit

The document covers various concepts related to binary trees, including definitions, properties, and representations using arrays and linked lists. It discusses operations on binary search trees, AVL trees, and tree traversals, along with algorithms for insertion, deletion, and searching. Additionally, it explains the use of binary trees in sorting and expression evaluation, along with the heap sort algorithm.

Uploaded by

manasaveena.t
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/ 20

UNIT-IV

1. Define Binary Tree Its Properties.Discuss binary tree representations


arrays and Linked List?

2. Describe the Search operations on binary search trees with the help of
algorithim?

3. Construct AVL tree with the given numbers 50,25,10,5,7,3,30,20,8,15

4. Explain Single rotation AVL trees with an example?

5. Define AVL trees ?Explain Various Rotations(LL,RR,RL,LR) of AVL tree


with Example?

6. Write in-order,Pre-order and post-order Traversal of a binary tree?

7. Explain Insertion and deletion operation on Binary search tree?

8. Explain Search strategies in Binary search tree?


9. Construct the BST for the given values 34,5,65,33,677,33,353,22,7,4,69
reconstruct the after deleting the root node?

10. Single rotation AVL tree with example?

11. Explain how an AVL tree can be used to sort a sequence of an elements
in O(log n)
12.Build the Binary search tree 10,12,13,14.15,16,17,18,19,20,11,12,13 .how to
make to the BST

Basic Terimonolgy of tree

1. Node

 The basic unit of a tree.


 Contains data and may point to other nodes.
 Example: each number in a binary tree is a node.

2. Root

 The topmost node of a tree.


 It has no parent.
 Every tree has exactly one root.

Example: In a tree with 16 at the top, 16 is the root.

3. Parent

 A node that has child nodes.


 Example: In a tree, if node 16 has children 12 and 20, then 16 is the parent of 12 and
20.

4. Child

 A node that has a parent node above it.


 Example: 14 is a child of 12.

5. Sibling

 Nodes that share the same parent.


 Example: 12 and 20 are siblings if they both come from 16.

6. Leaf Node

 A node with no children.


 Example: In this tree:
markdown
CopyEdit
16
/ \
12 20
\ / \
14 18 22

The leaf nodes are: 14, 18, 22 (no children).

7. Internal Node

 A node that has at least one child.


 Example: 16, 12, 20 are internal nodes.

8. Edge

 A link between a parent and a child node.


 One edge connects two nodes.

9. Path

 A sequence of nodes and edges connecting a node to a descendant.


 Example: Path from 16 to 14: 16 → 12 → 14

10. Level

 The distance from the root.


 Root is level 0, its children are level 1, their children are level 2, and so on.

11. Height of a Node

 The number of edges on the longest path from that node down to a leaf.
 Height of a leaf node = 0.

12. Depth of a Node

 Number of edges from the root to the node.


 Root has depth = 0.

13. Subtree

 A tree formed from a node and all its descendants.


 Every child node forms a subtree.

14. Binary Tree

 A tree where each node has at most two children: left and right.

15. Balanced Tree

 A tree where the heights of left and right subtrees of every node differ by at most 1.

Binary Tree Properties


1. Each node has at most 2 children
o Called the left and right child.
2. Maximum nodes at level l = 2^l
o Level 0 has 1 node, level 1 has 2, and so on.
3. Maximum nodes in a tree of height h = 2^(h+1) - 1
o A complete binary tree fills all levels.
4. Minimum nodes in a binary tree of height h = h + 1
o Happens when each node has only one child (like a linked list).
5. Number of leaf nodes = Number of nodes with two children + 1
o Formula: L = N2 + 1
6. Number of NULL pointers in a binary tree with n nodes = n + 1
o Each node has two pointer fields.
7. Tree height
o Height = longest path from root to a leaf.
8. Node depth
o Depth = number of edges from root to that node.
9. Traversal types
o Inorder: Left → Node → Right
o Preorder: Node → Left → Right
o Postorder: Left → Right → Node
o Level-order: Top-down, level-by-level
10. Types of binary trees
o Full Binary Tree: Every node has 0 or 2 children.
o Perfect Binary Tree: All internal nodes have 2 children, all leaves at same
level.
o Complete Binary Tree: All levels full, last level filled left to right.
o Balanced Binary Tree: Height difference between left and right subtrees ≤ 1.

3. Binary Tree Representation


🔸 There are two common ways to represent a binary tree:

1. Using Arrays
2. Using Linked Lists (Pointer-based)

📦 1. Binary Tree Representation Using Arrays

🔹 Point-wise:

1. Each node is stored in an array index.


2. Index i stores a node's value.
3. Left child is at index 2*i + 1
4. Right child is at index 2*i + 2
5. Parent of node at index i is at (i - 1) / 2
6. Efficient for complete or perfect binary trees.
7. Wastes space for incomplete or sparse trees.

/\

2 3

/\

4 5

Index: 0 1 2 3 4

Value: [1, 2, 3, 4, 5]

Binary Tree Representation Using Linked Lists (Pointers)

🔹 Point-wise:

1. Each node is a struct/class with:


o data
o left pointer
o right pointer
2. Nodes are connected by pointers, not indices.
3. More flexible and memory-efficient.
4. Works well for all types of trees (including sparse/incomplete).
5. Dynamic — nodes can be added or deleted easily.
6. Used in most real applications and algorithms (like AVL, BST, heaps).

struct Node {

int data;

struct Node* left;

struct Node* right;

};

/\

2 3

/\

4 5

Linked List (Memory representation):

 Node 1 → left → Node 2


 Node 1 → right → Node 3
 Node 2 → left → Node 4
 Node 2 → right → Node 5

🔹 1. What is a BST?

 A binary tree with a specific ordering rule:

For every node:

o All values in the left subtree are less than the node.
o All values in the right subtree are greater than the node.

2. BST Properties

 Each node has at most two children.


 The left child < parent < right child.
 No duplicate elements (in standard BSTs).
 Inorder traversal of a BST gives a sorted list.

3. Basic Operations in BST


Operation Description Time Complexity

Insert Add a new value in correct position O(log n)*

Search Find whether a value exists O(log n)*

Delete Remove a value, adjust tree accordingly O(log n)*

Traverse Visit nodes in a specific order O(n)

BST Example

Insert in order: 40, 20, 60, 10, 30, 50, 70

40

/ \

20 60

/\ /\

10 30 50 70

 All left children < parent

 All right children > parent

5. BST Traversals

Type Order Output for above BST

Inorder Left → Node → Right 10 20 30 40 50 60 70

Preorder Node → Left → Right 40 20 10 30 60 50 70

Postorder Left → Right → Node 10 30 20 50 70 60 40

Level Order Top-down, left to right 40 20 60 10 30 50 70

6. Use Cases of BSTs

 Searching and sorting (via inorder traversal)


 Implementing maps/dictionaries
 Used in databases, file systems, and symbol tables
 Auto-complete and range queries

7. Limitations of Simple BST

 If elements are inserted in sorted order, tree becomes unbalanced (like a linked list).
Example: Inserting: 10 → 20 → 30 → 40
Tree becomes:

10

20

30

40

*.Tree trversals in trees


1. Depth-First Traversals (DFT)

a. Inorder Traversal (Left, Root, Right)

Used mostly with Binary Search Trees to get nodes in sorted order.

Example:

/\

1 3

Inorder: 1, 2, 3

b. Preorder Traversal (Root, Left, Right)

Used to create a copy of the tree or prefix expressions.

Ex: 2

/\

1 3

Preorder: 2, 1, 3

c. Postorder Traversal (Left, Right, Root)


Useful for deleting or freeing nodes (post-processing).

Example:

/\

1 3

Postorder: 1, 3, 2

2. Breadth-First Traversal (BFT) / Level Order

Visits nodes level by level using a queue.

Example:

/\

1 3

Level Order: 2, 1, 3

*.Application Expression Trees


An expression tree is a binary tree where:

Leaves represent operands (e.g., numbers or variables).

Internal nodes represent operators (e.g., +, -, *, /).

Example:
For the expression: (3 + (5 * 2))
The expression tree would look like:

/\
3 *

/\

5 2

· Expression Evaluation

Use Postorder Traversal to evaluate the expression.

Because you process operands first, then operators.

· Infix, Prefix, and Postfix Conversion

Inorder: (3 + (5 * 2)) → Infix (natural human-readable)

Preorder: + 3 * 5 2 → Prefix (Polish Notation)

Postorder: 3 5 2 * + → Postfix (Reverse Polish Notation)

· Compilers & Interpreters

They build and traverse expression trees to parse, compile, and evaluate code.

· Symbolic Computation

Tools like Mathematica or SymPy use expression trees to simplify, differentiate, or integrate
expressions.

*.Heap Sort
Heap Sort is a comparison-based sorting algorithm that uses a Binary Heap data structure
(usually a Max Heap).

It’s known for:

Time complexity: O(n log n)

Space complexity: O(1) (in-place)

Not stable (doesn't preserve order of equal elements)

Binary Heap Refresher


A Max Heap is a complete binary tree where the parent node is always greater than
or equal to its children.


In Heap Sort, we first build a max heap, then repeatedly swap the max element
(root) with the last element, shrink the heap, and heapify.

Steps of Heap Sort

1.

Build Max Heap from the unsorted array.

2.
3.

Extract max (root of the heap) and place it at the end of the array.

4.
5.

Reduce heap size and heapify the root.

6.
7.

Repeat until the heap size is 1.

8.

#include <stdio.h>

// Function to heapify a subtree rooted at index i

void heapify(int arr[], int n, int i) {

int largest = i; // Initialize largest as root


int left = 2 * i + 1; // Left child index

int right = 2 * i + 2; // Right child index

// If left child is larger than root

if (left < n && arr[left] > arr[largest])

largest = left;

// If right child is larger than largest so far

if (right < n && arr[right] > arr[largest])

largest = right;

// If largest is not root

if (largest != i) {

int temp = arr[i];

arr[i] = arr[largest];

arr[largest] = temp;

// Recursively heapify the affected sub-tree

heapify(arr, n, largest);

// Main function to perform heap sort


void heapSort(int arr[], int n) {

// Build a maxheap (rearrange array)

for (int i = n / 2 - 1; i >= 0; i--)

heapify(arr, n, i);

// One by one extract elements from heap

for (int i = n - 1; i > 0; i--) {

// Move current root to end

int temp = arr[0];

arr[0] = arr[i];

arr[i] = temp;

// Call max heapify on the reduced heap

heapify(arr, i, 0);

// Utility function to print array

void printArray(int arr[], int n) {

for (int i = 0; i < n; ++i)

printf("%d ", arr[i]);

printf("\n");

}
// Driver code

int main() {

int arr[] = {12, 11, 13, 5, 6, 7};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array:\n");

printArray(arr, n);

heapSort(arr, n);

printf("Sorted array:\n");

printArray(arr, n);

return 0;

OUTPUT:Original array:

12 11 13 5 6 7

Sorted array:

5 6 7 11 12 13

1. Define Binary Tree Its Properties.Discuss binary tree representations


arrays and Linked List?
A binary tree is a data structure in which each node has at most two children, referred to as
the left and right child. It is a hierarchical structure where each node can store data and
links to other nodes (its children). The root node is the topmost node, and each child node
can itself be the root of a subtree.

Properties of a Binary Tree:

Maximum Nodes at Level i: At level i, the maximum number of nodes is 2^i. For
example, at level 0 (the root), there can only be one node (the root itself).

Maximum Number of Nodes in a Binary Tree of Height h: The maximum number of


nodes in a binary tree with height h is 2^(h+1) - 1.

Minimum Height for n Nodes: The minimum height of a binary tree with n nodes is
⌈log₂(n + 1)⌉ - 1.

Types of Binary Trees:

Full Binary Tree: Every node has either 0 or 2 children.

Complete Binary Tree: All levels are completely filled except possibly the last
level, which is filled from left to right.

Perfect Binary Tree: All internal nodes have two children, and all leaf nodes
are at the same level.

Skewed Binary Tree: Every node has only one child (either left or right).

Binary Tree Representations

1. Array Representation

In the array representation of a binary tree, the nodes are stored in an array in level-order
(from top to bottom, left to right). For a node at index i:

The left child is at index 2i + 1.

The right child is at index 2i + 2.

The parent is at index (i - 1) / 2.

2. Example: Consider the binary tree:

10
/ \

20 30

/ \

40 50

This tree will be stored in an array as:[10, 20, 30, 40, 50]

The root 10 is at index 0.

· The left child of 10 is at index 1 (value 20).

· The right child of 10 is at index 2 (value 30).

· The left child of 20 is at index 3 (value 40).

· The right child of 20 is at index 4 (value 50).

2. Linked List Representation

In the linked list representation of a binary tree, each node contains the data and two
pointers—one for the left child and one for the right child. This method allows each node to
be dynamically allocated, making it flexible for non-complete binary trees.

10

/ \

20 30

/ \

40 50

struct Node {

int data;

struct Node* left;

struct Node* right;

};
struct Node* root = newNode(10);

root->left = newNode(20);

root->right = newNode(30);

root->left->left = newNode(40);

root->left->right = newNode(50);

3. Describe the Search operations on binary search trees with the help of
algorithim?

In a Binary Search Tree (BST), each node contains a key, and the keys follow a specific
order:

The left child contains a key smaller than the parent node.

The right child contains a key greater than the parent node.

The search operation in a binary search tree involves finding a specific key in the tree based
on these ordering rules. The search process starts at the root and then moves to the left or
right child depending on whether the key being searched is smaller or larger than the
current node’s key.

Search Operation on Binary Search Tree

Here’s how the search operation works in a Binary Search Tree:

Start at the root node.

If the key to be searched is equal to the root node's key, the search is successful,
and the node is returned.

If the key is smaller than the root node's key, the search continues in the left
subtree.

If the key is larger than the root node's key, the search continues in the right
subtree.

Repeat this process until the key is found or the subtree becomes empty (i.e., there
is no node to check), in which case the key does not exist in the tree
struct Node {

int data;

struct Node* left;

struct Node* right;

};

// Function to search a key in a BST

struct Node* search(struct Node* root, int key) {

// Base case: root is null or key is present at the root

if (root == NULL || root->data == key)

return root;

// Key is greater than root's key, search in the right subtree

if (key > root->data)

return search(root->right, key);

// Key is smaller than root's key, search in the left subtree

return search(root->left, key);

Explanation of the Algorithm:

The function search takes the root of the binary search tree and the key to search
for as parameters.

Base Case 1: If the root is NULL, it means the tree is empty, or the key is not found. In
this case, the function returns NULL.
Base Case 2: If the key matches the value at the root, the function returns the root
node, which means the key is found.

If the key is greater than the value of the root node, the function recurses into the
right subtree.

If the key is smaller, it recurses into the left subtree.

50

/ \

30 70

/ \ / \

20 40 60 80

Searching for 40:

Start at the root (50).

Since 40 is smaller than 50, move to the left child (30).

Since 40 is greater than 30, move to the right child (40).

40 is found in the tree.

Searching for 100:

Start at the root (50).

Since 100 is greater than 50, move to the right child (70).

Since 100 is greater than 70, move to the right child (80).

100 is not found as the right child of 80 is NULL.

Time Complexity:

Best case: O(1) when the key is found at the root.

Average case: O(log n) when the tree is balanced.

Worst case: O(n) when the tree is skewed (i.e., like a linked list).

You might also like