Dec 12thACA
Dec 12thACA
4 Trees
4.1 Inorder Traversal
Algorithm (Recursive):
1. Start at the root node of the tree.
2. Traverse the left subtree in an inorder manner (recursively).
3. Visit the current node (process its value).
struct Node
{
int val ;
Node * left ;
Node * right ;
Node ( int x ) : val ( x ) , left ( NULL ) , right ( NULL ) {}
};
Node * createTree ( vector < int > & nodes , int & index , int n )
{
if ( index >= n || nodes [ index ] == -1)
return NULL ;
return root ;
}
int main ()
{
int n ;
25
cout << " Enter the number of nodes in the binary tree : " ;
cin >> n ;
int index = 0;
Node * root = createTree ( nodes , index , n ) ;
return 0;
}
Output 1:
Enter the number of nodes in the binary tree: 7
Enter the nodes of the binary tree (-1 for NULL): 1 2 3 -1 -1 4 5
Inorder Traversal of the binary tree: 2 1 4 3 5
Output 2:
Enter the number of nodes in the binary tree: 5
Enter the nodes of the binary tree (-1 for NULL): 10 5 15 -1 -1
Inorder Traversal of the binary tree: 5 10 15
struct Node
{
int val ;
Node * left ;
Node * right ;
26
Node ( int x ) : val ( x ) , left ( NULL ) , right ( NULL ) {}
};
return root ;
}
int main ()
{
int n ;
cout << " Enter the number of nodes in the binary tree : " ;
cin >> n ;
int nodes [ n ];
cout << " Enter the nodes of the binary tree ( -1 for NULL ) : " ;
for ( int i = 0; i < n ; i ++)
cin >> nodes [ i ];
int index = 0;
Node * root = createTree ( nodes , index , n ) ;
flatten ( root ) ;
return 0;
}
Output 1:
Enter the number of nodes in the binary tree: 7
27
Enter the nodes of the binary tree (-1 for NULL): 1 2 5 3 4 -1 6
Linked list representation: 1 2 3 4 5 6
Output 2:
Enter the number of nodes in the binary tree: 5
Enter the nodes of the binary tree (-1 for NULL): 10 5 15 -1 -1
Linked list representation: 10 5 15
struct Node
{
int val ;
Node * left ;
Node * right ;
Node ( int x ) : val ( x ) , left ( NULL ) , right ( NULL ) {}
};
while (! q . empty () )
{
auto front = q . front () ;
q . pop () ;
28
q . push ({ current - > left , hd - 1}) ;
if ( current - > right != NULL )
q . push ({ current - > right , hd + 1}) ;
}
return root ;
}
int main ()
{
int n ;
cout << " Enter the number of nodes in the binary tree : " ;
cin >> n ;
int nodes [ n ];
cout << " Enter the nodes of the binary tree ( -1 for NULL ) : " ;
for ( int i = 0; i < n ; i ++)
cin >> nodes [ i ];
int index = 0;
Node * root = createTree ( nodes , index , n ) ;
return 0;
}
Output 1:
Enter the number of nodes in the binary tree: 7
Enter the nodes of the binary tree (-1 for NULL): 20 8 22 5 3 -1 25
Bottom view of the binary tree: 5 3 22 25
Output 2:
Enter the number of nodes in the binary tree: 10
Enter the nodes of the binary tree (-1 for NULL): 1 2 3 4 5 6 7 -1 -1 8 9
Bottom view of the binary tree: 4 8 6 3 7
5 Operation On heaps
5.1 Heap Sort
Algorithm:
1. Build a max heap from the input array.
2. Swap the first element (largest) with the last element of the heap.
3. Reduce the heap size by 1 and heapify the root element to maintain the max heap property.
4. Repeat steps 2 and 3 until the heap size becomes 1.
Time Complexity:
29
• O(n log n): Building the heap takes O(n), and heapifying each element during the sorting process takes O(log n).
Space Complexity:
void heapify ( vector < int > & arr , int n , int i )
{
int largest = i ;
int left = 2 * i + 1;
int right = 2 * i + 2;
if ( largest != i )
{
swap ( arr [ i ] , arr [ largest ]) ;
heapify ( arr , n , largest ) ;
}
}
int main ()
{
int n ;
cout << " Enter the number of elements : " ;
cin >> n ;
heapSort ( arr ) ;
return 0;
}
Output 1:
Enter the number of elements: 5
Enter the elements: 4 10 3 5 1
Sorted array: 1 3 4 5 10
Output 2:
30
Enter the number of elements: 7
Enter the elements: 12 11 13 5 6 7 15
Sorted array: 5 6 7 11 12 13 15
vector < int > relativeSort ( vector < int > & arr1 , vector < int > & arr2 )
{
unordered_map < int , int > freq ;
vector < int > result ;
return result ;
}
int main ()
{
int n , m ;
cout << " Enter the size of the first array : " ;
cin >> n ;
vector < int > arr1 ( n ) ;
cout << " Enter the elements of the first array : " ;
for ( int i = 0; i < n ; i ++)
cin >> arr1 [ i ];
31
cout << " Enter the size of the second array : " ;
cin >> m ;
vector < int > arr2 ( m ) ;
cout << " Enter the elements of the second array : " ;
for ( int i = 0; i < m ; i ++)
cin >> arr2 [ i ];
return 0;
}
Output 1:
Enter the size of the first array: 7
Enter the elements of the first array: 2 1 2 5 7 1 9
Enter the size of the second array: 4
Enter the elements of the second array: 2 1 8 3
Resulting array after relative sorting: 2 2 1 1 5 7 9
Output 2:
Time Complexity:
• Push Operation: O(log n): Inserting an element involves heapify-up, which takes O(log n).
• Pop Operation: O(log n): Removing the root element involves heapify-down, which takes O(log n).
32
# include < iostream >
# include < vector >
using namespace std ;
class PriorityQueue
{
private :
vector < int > heap ;
if ( left < heap . size () && heap [ left ] > heap [ largest ])
largest = left ;
if ( right < heap . size () && heap [ right ] > heap [ largest ])
largest = right ;
if ( largest != i )
{
swap ( heap [ i ] , heap [ largest ]) ;
heapifyDown ( largest ) ;
}
}
public :
void push ( int x )
{
heap . push_back ( x ) ;
heapifyUp ( heap . size () - 1) ;
}
void pop ()
{
if ( heap . empty () )
{
cout << " Priority queue is empty ! " << endl ;
return ;
}
heap [0] = heap . back () ;
heap . pop_back () ;
heapifyDown (0) ;
}
int top ()
{
if ( heap . empty () )
{
cout << " Priority queue is empty ! " << endl ;
return -1;
}
return heap [0];
}
bool empty ()
{
return heap . empty () ;
}
};
int main ()
33
{
PriorityQueue pq ;
switch ( choice )
{
case 1:
cout << " Enter value to push : " ;
cin >> value ;
pq . push ( value ) ;
break ;
case 2:
pq . pop () ;
break ;
case 3:
cout << " Top element : " << pq . top () << endl ;
break ;
default :
cout << " Invalid operation ! " << endl ;
}
}
return 0;
}
Output 1:
Enter the number of operations: 6
Operations:
1. Push
2. Pop
3. Top
Enter operation: 1
Enter value to push: 10
Enter operation: 1
Enter value to push: 5
Enter operation: 1
Enter value to push: 20
Enter operation: 3
Top element: 20
Enter operation: 2
Enter operation: 3
Top element: 10
Output 2:
Enter the number of operations: 5
Operations:
1. Push
2. Pop
3. Top
Enter operation: 1
Enter value to push: 7
Enter operation: 1
Enter value to push: 15
Enter operation: 3
Top element: 15
Enter operation: 2
34
Enter operation: 3
Top element: 7
class HashTable
{
private :
vector < list < pair < int , int > > > table ;
int size ;
public :
HashTable ( int s )
{
size = s ;
table . resize ( size ) ;
}
35
}
}
table [ index ]. emplace_back ( key , value ) ;
}
void display ()
{
for ( int i = 0; i < size ; i ++)
{
cout << " Bucket " << i << " : " ;
for ( auto & pair : table [ i ])
cout << " ( " << pair . first << " , " << pair . second << " ) " ;
cout << endl ;
}
}
};
int main ()
{
int size , choice , key , value ;
cout << " Enter the size of the hash table : " ;
cin >> size ;
HashTable ht ( size ) ;
switch ( choice )
{
case 1:
cout << " Enter key and value to insert : " ;
cin >> key >> value ;
ht . insert ( key , value ) ;
break ;
case 2:
cout << " Enter key to search : " ;
cin >> key ;
value = ht . search ( key ) ;
if ( value != -1)
cout << " Value : " << value << endl ;
else
cout << " Key not found ! " << endl ;
break ;
case 3:
cout << " Enter key to remove : " ;
cin >> key ;
ht . remove ( key ) ;
36
break ;
case 4:
ht . display () ;
break ;
case 0:
cout << " Exiting . " << endl ;
break ;
default :
cout << " Invalid choice ! " << endl ;
}
} while ( choice != 0) ;
return 0;
}
Output 1:
Enter the size of the hash table: 5
Operations:
1. Insert
2. Search
3. Remove
4. Display
Enter your choice (0 to exit): 1
Enter key and value to insert: 10 100
Enter your choice (0 to exit): 1
Enter key and value to insert: 15 200
Enter your choice (0 to exit): 2
Enter key to search: 10
Value: 100
Enter your choice (0 to exit): 4
Bucket 0:
Bucket 1:
Bucket 2:
Bucket 3:
Bucket 4: (10, 100) (15, 200)
Enter your choice (0 to exit): 0
Exiting.
Output 2:
Enter the size of the hash table: 7
Operations:
1. Insert
2. Search
3. Remove
4. Display
Enter your choice (0 to exit): 1
Enter key and value to insert: 5 50
Enter your choice (0 to exit): 1
Enter key and value to insert: 12 120
Enter your choice (0 to exit): 4
Bucket 0:
Bucket 1:
Bucket 2:
Bucket 3:
Bucket 4:
Bucket 5: (5, 50)
Bucket 6: (12, 120)
Enter your choice (0 to exit): 3
Enter key to remove: 5
Enter your choice (0 to exit): 4
Bucket 0:
37
Bucket 1:
Bucket 2:
Bucket 3:
Bucket 4:
Bucket 5:
Bucket 6: (12, 120)
Enter your choice (0 to exit): 0
Exiting.
bool comparator ( const pair < int , int > &a , const pair < int , int > & b )
{
if ( a . second == b . second )
return a . first < b . first ;
return a . second > b . second ;
}
vector < pair < int , int > > freqVec ( freqMap . begin () , freqMap . end () ) ;
sort ( freqVec . begin () , freqVec . end () , comparator ) ;
arr = sortedArr ;
38
}
int main ()
{
int n ;
cout << " Enter the number of elements : " ;
cin >> n ;
s or tB yF r eq ue nc y ( arr ) ;
return 0;
}
Output 1:
Enter the number of elements: 8
Enter the elements: 4 5 6 5 4 3 3 3
Sorted array by frequency: 3 3 3 4 4 5 5 6
Output 2:
Enter the number of elements: 7
Enter the elements: 1 2 3 2 1 1 4
Sorted array by frequency: 1 1 1 2 2 3 4
39
{
unordered_set < int > elements ( arr . begin () , arr . end () ) ;
int longest = 0;
int main ()
{
int n ;
cout << " Enter the number of elements : " ;
cin >> n ;
return 0;
}
Output 1:
Enter the number of elements: 6
Enter the elements: 100 4 200 1 3 2
Length of the longest consecutive subsequence: 4
Output 2:
Enter the number of elements: 7
Enter the elements: 2 6 1 9 4 3 5
Length of the longest consecutive subsequence: 6
1. Check if the lengths of the two arrays are different. If they are, the arrays are not equal.
2. Use a hash map to count the frequency of each element in the first array.
3. Traverse the second array and decrement the frequency of each element in the hash map:
• If an element is not found or its count goes below zero, the arrays are not equal.
4. After processing the second array, check if all values in the hash map are zero. If they are, the arrays are equal.
5. Return the result.
Time Complexity:
• O(n):
– Building the frequency map takes O(n).
40
– Traversing the second array and checking the map also takes O(n).
Space Complexity:
• O(n): Space for the hash map.
Code:
# include < iostream >
# include < vector >
# include < unordered_map >
using namespace std ;
bool areArray sEqual ( vector < int > & arr1 , vector < int > & arr2 )
{
if ( arr1 . size () != arr2 . size () )
return false ;
int main ()
{
int n1 , n2 ;
cout << " Enter the size of the first array : " ;
cin >> n1 ;
vector < int > arr1 ( n1 ) ;
cout << " Enter elements of the first array : " ;
for ( int i = 0; i < n1 ; i ++)
cin >> arr1 [ i ];
cout << " Enter the size of the second array : " ;
cin >> n2 ;
vector < int > arr2 ( n2 ) ;
cout << " Enter elements of the second array : " ;
for ( int i = 0; i < n2 ; i ++)
cin >> arr2 [ i ];
return 0;
}
Output 1:
Enter the size of the first array: 5
Enter elements of the first array: 1 2 3 4 5
Enter the size of the second array: 5
Enter elements of the second array: 5 4 3 2 1
The arrays are equal.
Output 2:
Enter the size of the first array: 4
Enter elements of the first array: 1 2 3 4
Enter the size of the second array: 4
Enter elements of the second array: 1 2 2 4
The arrays are not equal.
41