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

WEEK-10

The document contains multiple implementations of binary search tree (BST) operations in C, including insertion, deletion, searching, and validation of BST properties. It also includes functions for converting a sorted array to a BST, finding the k-th smallest element, and calculating the range sum of values within a specified range. Additionally, it provides methods for determining the minimum difference between values in the BST.

Uploaded by

pullareddyk621
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)
4 views

WEEK-10

The document contains multiple implementations of binary search tree (BST) operations in C, including insertion, deletion, searching, and validation of BST properties. It also includes functions for converting a sorted array to a BST, finding the k-th smallest element, and calculating the range sum of values within a specified range. Additionally, it provides methods for determining the minimum difference between values in the BST.

Uploaded by

pullareddyk621
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/ 7

WEEK-10

InLab-1
struct TreeNode* insertIntoBST(struct TreeNode* root, int val) {
if(!root) {
struct TreeNode* newNode=malloc(sizeof(struct TreeNode));
newNode->val=val;
newNode->left=NULL;
newNode->right=NULL;
return newNode;
}
if(val>root->val) {
root->right=insertIntoBST(root->right,val);
}
else if(val<root->val) {
root->left=insertIntoBST(root->left,val);
}
return root;
}

InLab-2
struct TreeNode* replace(struct TreeNode* root) {
if (root->right == NULL && root->left == NULL)
return NULL;
if (root->right == NULL)
return root->left;
if (root->left == NULL)
return root->right;
struct TreeNode* temp1 = root->right;
if (temp1->left == NULL) {
temp1->left = root->left;
} else {
struct TreeNode* temp2 = temp1;
while (temp2->left != NULL) {
temp2 = temp2->left;
}
temp2->left = root->left;
}
return temp1;
}
struct TreeNode* deleteNode(struct TreeNode* root, int key) {
if (root == NULL)
return NULL;
if (root->val == key)
return replace(root);
if (root->val < key)
root->right = deleteNode(root->right, key);
if (root->val > key)
root->left = deleteNode(root->left, key);
return root;
}

InLab-3
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int key) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->value = key;
newNode->left = newNode->right = NULL;
return newNode;
}
struct Node* insert(struct Node* root, int key) {
if (root == NULL) {
return createNode(key);
}
if (key < root->value) {
root->left = insert(root->left, key);
} else {
root->right = insert(root->right, key);
}
return root;
}
void inorder(struct Node* root, int* result, int* index) {
if (root == NULL) {
return;
}
inorder(root->left, result, index);
result[(*index)++] = root->value;
inorder(root->right, result, index);
}
void findKthElement(struct Node* root, int k) {
int result[100];
int index = 0;
inorder(root, result, &index);
if (k <= index) {
printf("YES\n");
} else {
printf("NO\n");
}
}
int main() {
int N, k;
scanf("%d", &N);
scanf("%d", &k);
struct Node* root = NULL;
for (int i = 0; i < N; i++) {
int val;
scanf("%d", &val);
root = insert(root, val);
}
findKthElement(root, k);
return 0;
}

Skill-1
struct TreeNode* ConvToBST(int *nums, int beg, int end){
if(end < beg)
return NULL ;
int mid = (beg + end)/2 ;
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val = nums[mid];
root->left = ConvToBST(nums, beg, mid-1);
root->right = ConvToBST(nums, mid+1, end);
return root;
}
struct TreeNode* sortedArrayToBST(int* nums, int numsSize){
if(numsSize <= 0)
return NULL;
else
return ConvToBST(nums, 0, numsSize-1);
}

Skill-2
struct TreeNode* searchBST(struct TreeNode* root, int val) {
struct TreeNode* x = NULL;
if (root == NULL) {
return NULL;
}
if (root->val == val) {
x = root;
}
else {
if (root->val < val) {
x = searchBST(root->right, val);
}
else {
x = searchBST(root->left, val);
}
}
return x;
}

Skill-3
void inorder(struct TreeNode* root, struct TreeNode** prev, int* ans){
if(!root)
return;
inorder(root->left, prev, ans);
if(*(prev))
*ans = fmin(*ans , fabs(root->val - (*prev)->val));
*prev = root;
inorder(root->right, prev, ans);
return;

}
int getMinimumDifference(struct TreeNode* root) {
struct TreeNode* prev = NULL;
int ans = INT_MAX;
inorder(root, &prev, &ans);
return ans;
}

Skill-4
void helper(struct TreeNode* root, struct TreeNode** prev, bool* result){
if(root == NULL) return;
helper(root->left, prev, result);
if(*prev != NULL && root->val <= (*prev)->val) {
*result = false;
return;
}
*prev = root;
helper(root->right, prev, result);
}
bool isValidBST(struct TreeNode* root) {
struct TreeNode* prev = NULL;
bool result = true;
helper(root, &prev, &result);
return result;
}

Skill-5
int rangeSumBST(struct TreeNode* root, int low, int high) {
int sum=0;
if (root == NULL)
return 0;
if(root->val >= low && root->val <= high){
sum=sum+root->val;
}
sum=sum+rangeSumBST(root->left, low, high);
sum=sum+rangeSumBST(root->right, low, high);
return sum;
}

You might also like