Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BinarySearchTree #330

Open
wants to merge 459 commits into
base: revert-46-patch-5
Choose a base branch
from
Open

BinarySearchTree #330

wants to merge 459 commits into from

Conversation

arastuthakur
Copy link

// Binary Search Tree operations in C

#include <stdio.h>
#include <stdlib.h>

struct node {
int key;
struct node *left, *right;
};

// Create a node
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left);

// Traverse root
printf("%d -> ", root->key);

// Traverse right
inorder(root->right);

}
}

// Insert a node
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);

// Traverse to the right place and insert the node
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);

return node;
}

// Find the inorder successor
struct node *minValueNode(struct node *node) {
struct node *current = node;

// Find the leftmost leaf
while (current && current->left != NULL)
current = current->left;

return current;
}

// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;

// Find the node to be deleted
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);

else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}

// If the node has two children
struct node *temp = minValueNode(root->right);

// Place the inorder successor in position of the node to be deleted
root->key = temp->key;

// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);

}
return root;
}

// Driver code
int main() {
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);

printf("Inorder traversal: ");
inorder(root);

printf("\nAfter deleting 10\n");
root = deleteNode(root, 10);
printf("Inorder traversal: ");
inorder(root);
}

Zulfa210 and others added 30 commits October 17, 2021 11:25
Create BalancedBrackets.java
Added pancake sort algorithm in python
Create BalancedBrackets.java
Spiral Order Matrix Traversal
this program is to calculate the power using recursion
Adi8080 and others added 30 commits October 27, 2021 20:43
Create binary search in cpp
Added a DP program and BST separately
Created tower of hanoi in js
Number f coins to be minimum for making a particular sum
Added a new C++ program to find sum of all divisors of a number As a part of hactober fest.
Added a new C++ program to find sum of all divisors of a number as part of Hactoberfest.
THIS IS A BMI CALCULATOR
added comments
added comments
Added a cpp code for performing a queue using a stack.
Added a new C++ program to find sum of all divisors of a number.
added a program to find fibonacci using recursion
Binary Search Tree from Pre and Post Order
Allocate minimum number of pages(Java)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.