Trees Code HELP
Trees Code HELP
Preorder-Inorder BST
[ //Function to build a binary tree from preorder and inorder
#include <stdio.h> traversals
#include <stdlib.h> struct TreeNode* buildTree(int* preorder, int preorderSize, int*
inorder, int inorderSize){
TreeNode Structure return buildTreeHelper(preorder, 0, preorderSize - 1, inorder,
struct TreeNode { 0, inorderSize-1);
int val; }
struct TreeNode* left;
struct TreeNode* right; //Function to print the inorder traversal of a TreeNode
}; void printInorder(struct TreeNode* root){
if(!root){
//Function to create a new TreeNode return;
struct TreeNode* newTreeNode(int x){ }
struct TreeNode* node = (struct printInorder(root->left);
TreeNode*)malloc(sizeof(struct TreeNode)); printf("%d ",root->val);
node->val = x; printInorder(root->right);
node->left = NULL; }
node->right = NULL;
return node; //Function to print the given array
} void printArray(int* arr, int size){
for(int i = 0; i < size; i++){
//Function to find the index of a value in an array printf("%d ",arr[i]);
int findIndex(int * arr, int start, int end, int value){ }
for(int i = start; i <= end; i++){ printf("\n");
if(arr[i]==value){ }
return i;
} int main(){
} int inorder[] = {9,3,15,20,7};
int preorder[] = {3, 9, 20, 15, 7};
return -1;
} int inorderSize = sizeof(inorder) / sizeof(inorder[0]);
int preorderSize = sizeof(preorder) / sizeof(preorder[0]);
//Create a new TreeNode with value at the current preorder printf("Inorder of Unique Binary Tree Created: \n");
index printInorder(root);
struct TreeNode* root = newTreeNode(preorder[preStart]); printf("\n");
return 0;
//Find the index of the current root value in the inorder }
traversal ]
int inRoot = findIndex(inorder , inStart, inEnd, root->val);
// Function to search for a node in a Binary Search Tree (BST) #include <stdio.h>
struct TreeNode* searchBST(struct TreeNode* root, int val) { #include <stdlib.h>
// Loop until either the tree is exhausted (NULL)
// or the value is found. // Definition of TreeNode structure for a binary tree node
while (root != NULL && root->val != val) { struct TreeNode {
// Check if the target value is less than the current node's int val;
value. struct TreeNode* left;
// If so, move to the left subtree (values smaller than the struct TreeNode* right;
current node). };
// Otherwise, move to the right subtree (values larger than
the current node). // Function to create a new tree node
root = val < root->val ? root->left : root->right; struct TreeNode* newTreeNode(int val) {
} struct TreeNode* node = (struct
// Return the node containing the target value, if found; TreeNode*)malloc(sizeof(struct TreeNode));
otherwise, return NULL. node->val = val;
return root; node->left = NULL;
} node->right = NULL;
return node;
// Function to perform an in-order traversal of a binary tree and }
print its nodes
void printInOrder(struct TreeNode* root) { // Function to delete a node with a given key from the BST
// Check if the current node is NULL (base case for recursion) struct TreeNode* deleteNode(struct TreeNode* root, int key) {
if (root == NULL) { if (!root) return NULL;
// If NULL, return and terminate the function
return; // If the key is greater than the current node's value, go to the
} right subtree
if (root->val < key) {
// Recursively call printInOrder for the left subtree root->right = deleteNode(root->right, key);
printInOrder(root->left); }
// If the key is smaller than the current node's value, go to
// Print the value of the current node the left subtree
printf("%d ", root->val); else if (root->val > key) {
root->left = deleteNode(root->left, key);
// Recursively call printInOrder for the right subtree }
printInOrder(root->right); // If the current node is the node to be deleted
else { BST FROM PREORDER
// Case 1: The node has no children (leaf node)
if (!root->right && !root->left) { #include <stdio.h>
free(root); #include <stdlib.h>
return NULL; #include <limits.h>
}
// Case 2: The node has only a left child // Definition of the TreeNode structure for a binary tree node
else if (!root->right) { struct TreeNode {
struct TreeNode* temp = root->left; int val;
free(root); struct TreeNode* left;
return temp; struct TreeNode* right;
} };
// Case 3: The node has only a right child
else if (!root->left) { // Function to create a new tree node
struct TreeNode* temp = root->right; struct TreeNode* newTreeNode(int val) {
free(root); struct TreeNode* node = (struct
return temp; TreeNode*)malloc(sizeof(struct TreeNode));
} node->val = val;
// Case 4: The node has two children node->left = NULL;
else { node->right = NULL;
struct TreeNode* temp = root->right; return node;
// Find the smallest node in the right subtree (in-order }
successor)
while (temp->left) { // Function to solve and construct the BST from preorder
temp = temp->left; traversal
} struct TreeNode* solve(int* preorder, int* index, int size, int h) {
// Replace the current node's left subtree to the // If index is out of bounds or current value exceeds the
successor's left upper bound (h)
temp->left = root->left; if (*index == size || preorder[*index] > h) return NULL;
struct TreeNode* newRoot = root->right;
free(root); // Create a new TreeNode with the current value
return newRoot; struct TreeNode* root = newTreeNode(preorder[*index]);
} (*index)++; // Increment the index to process the next
} element