BST
手写一个二叉搜索树
#include
using namespace std;struct node
{int value;node *left;node *right;
};node* insert(node *n,int value)
{if(n == NULL){node *n = new node;n->left = NULL;n->right = NULL;n->value = value;return n;}if(value < n->value){n->left = insert(n->left, value);return n;}else if(value > n->value){n->right = insert(n->right, value);return n;}}node *remove(node *n, int val)
{if(n->value == val){if(n->right == NULL){node *ptr = n->left;delete n;return ptr;}else{node *ptr = n->right;while(ptr->left){ptr = ptr->left;}n->value = ptr->value;n->right = remove(n->right, ptr->value);return n;}}if(n->left != NULL)n->left = remove(n->left, val);if(n->right != NULL)n->right = remove(n->right, val);return n;
}int main()
{node *root = NULL;root = insert(root,10);root = insert(root,5);root = insert(root,2);root = insert(root,20);remove(root,10);cout << root->left->value;system("pause");return 0;
}