Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BinarySearchTree#330

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
arastuthakur wants to merge459 commits intorevert-46-patch-5
base:revert-46-patch-5
Choose a base branch
Loading
frommain
Open

BinarySearchTree#330

arastuthakur wants to merge459 commits intorevert-46-patch-5frommain

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 rootprintf("%d -> ", root->key);// Traverse rightinorder(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 childrenstruct node *temp = minValueNode(root->right);// Place the inorder successor in position of the node to be deletedroot->key = temp->key;// Delete the inorder successorroot->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);
}

Zulfa210and others added30 commitsOctober 17, 2021 11:25
Added pancake sort algorithm in python
Spiral Order Matrix Traversal
this program is to calculate the power using recursion
Adi8080and others added30 commitsOctober 27, 2021 20:43
Added a DP program and BST separately
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 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

96 participants
@arastuthakur@Zulfa210@sahilrawat001@Adi8080@MdAnsar7@ronak007mistry@Satyam2003-dev@Manali71@Ravi-Dinkar@Somesh9618@MukulGupta2001@vraj-chovatiya@nikhil822@harsimranjit27@yrs-rosh@dpshekhawat@Ritesh-10@vandyyyy@Darkcoder-paditya@AlishaChhabra25@Pratyush-IITBHU@jeel2410@adityak3252@Kunalgirdhar3@KallaRukmini7@9759176595@us25999@vedant-9@PratigyaSharma@mayank-cse@gulshanlaskar08@alphin-roy2000@Shubhamkts14@Rohit-1706@TannuVashist@ashutoshmittal26@zestycoder@Hrit20@vishal8803@livisha@killJoy-03@Shi05@sagarmaheshwari@ankitgadge15@PrachiKapatel@Rupesh1912@Acrolyte@imutkarsh09@Qaisarali-Sulaimani@YashkShrivas4491@vigneshwar-yadav@Deepika0130@payal-gitport@ritikarora995@AayushMaurya@kamalkumar7@sphinxbard@AyushMittal10@she-hacks-2021@avi-11@ruchika-swain@brat-blip@ankushbhagat124@Kaviraghul@sushantvaidkar@Piyushwebdev@Shubhamprakash007@Taruna06@AnishaSawant04@priyanshiporwal@Dhruvil17@SakshiGupta-21@Tushar00728@RuhiRaj@Me-amarJEEt@manishsinghtomar@kriti-arora@Wizardhere@Lovecase@Saumya1507@komputarist@Aman-Ladla@aditya33agrawal@Pravi16@pkkushagra@devAyushDubey@Prateekg2050@ajay1975@abhijeetgauravm@grvkr777@sumit4code-git@adarsh1pandey@lokeshkaushik@sarthak1320@siddhant2u@gaurav79829

[8]ページ先頭

©2009-2025 Movatter.jp