DSA Day 3
DSA Day 3
NEW CAMPUS
Programming
Session
Phase 1
DSA
Tree
The tree is non-linear, a hierarchical data structure consisting of a collection of nodes, and each
node in the tree contains a value that is a list of references to the node (the “child”).
Application
Traversing: The tree structure becomes a route for the HTML interpreter that can be followed
to traverse throughout the HTML document.
Decision Making: A tree data structure can provide us with an algorithm that can allow a user
to explore movie streaming applications in such a way that they can reach a movie that might be
best recommended for them.
Types
General tree
In this tree, there is no limitation to the number of nodes. It starts with a root node and the parent node's
children make another general sub-tree.
Binary tree
A binary tree node can have up to two child nodes. In a given dendrogram, nodes B, D, and F are children on
the left, and E, C, and G are children on the right.
Balance tree
A tree is said to have a balanced tree data structure if the heights of the left and right subtrees are the same or
differ by up to one.
class Tree {
public:
node* root = nullptr;
void simple_insert(int v) {
if (root == nullptr) {
root = new node(v);
}
else {
node* parent = root;
node* child = root;
while (parent != nullptr) {
child = parent;
if (v > parent->data) {
parent = parent->right;
}
else {
parent = parent->left;
}
}
if (v > child->data) {
child->right = new node(v);
}
else {
child->left = new node(v);
}
}
}
void inorder_print(node* root) {
if (root == nullptr) {
return;
}
inorder_print(root->left);
cout << root->data << " -> ";
inorder_print(root->right);
}
if (v > root->data) {
root->right = recursive_insert(root->right, v);
}
else {
root->left = recursive_insert(root->left, v);
}
return root;
}
inorder_print(root->right);
}
inorder_print(root->left);
inorder_print(root->right);
cout << root->data << " -> ";
}
}
return root;
}
};
Height and Diameter of BST
Level Order Traversal
class Node {
public:
int key;
Node* left;
Node* right;
int height;
};
// Calculate height
int height(Node* N) {
if (N == NULL)
return 0;
return N->height;
}
// Rotate right
Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left),
height(y->right)) +
1;
x->height = max(height(x->left),
height(x->right)) +
1;
return x;
}
// Rotate left
Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left),
height(x->right)) +
1;
y->height = max(height(y->left),
height(y->right)) +
1;
return y;
}
// Insert a node
Node* insertNode(Node* node, int key) {
// Find the correct postion and insert the node
if (node == NULL)
return (newNode(key));
if (key < node->key)
node->left = insertNode(node->left, key);
else if (key > node->key)
node->right = insertNode(node->right, key);
else
return node;
// Delete a node
Node* deleteNode(Node* root, int key) {
// Find the node and delete it
if (root == NULL)
return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
if ((root->left == NULL) ||
(root->right == NULL)) {
Node* temp = root->left ? root->left : root->right;
if (temp == NULL) {
temp = root;
root = NULL;
}
else
*root = *temp;
free(temp);
}
else {
Node* temp = nodeWithMimumValue(root->right);
root->key = temp->key;
root->right = deleteNode(root->right,
temp->key);
}
}
if (root == NULL)
return root;
int main() {
Node* root = NULL;
root = insertNode(root, 33);
root = insertNode(root, 13);
root = insertNode(root, 53);
root = insertNode(root, 9);
root = insertNode(root, 21);
root = insertNode(root, 61);
root = insertNode(root, 8);
root = insertNode(root, 11);
printTree(root, "", true);
root = deleteNode(root, 13);
cout << "After deleting " << endl;
printTree(root, "", true);
}
Heap
A heap is a complete binary tree, and the binary tree is a tree in which the node can have two children.
A complete binary tree is a binary tree in which all the levels except the last level, i.e., leaf node should be
completely filled, and all the nodes should be left-justified.
Min Heap: min-heap can be defined as, for every node i, the value of node i is greater than or equal to its
parent value except the root node. A[Parent(i)] <= A[i]
Max Heap: max heap can be defined as for every node i; the value of node i is less than or equal to its parent
value except the root node. A[Parent(i)] >= A[i]
Implementation:
Click here.
https://www.programiz.com/dsa/binary-search-tree
Algorithms
Merge sort.
void merge(int arr[], int p, int q, int r) {
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
Quick sort.
int partition(int array[], int low, int high) {