1 建立二叉查找树
typedef int ElemType;
struct node
{
ElemType data;
node* lchild;
node* rchild;
};
2 建立树
2.1 新建结点
node* newNode(int v){
node* Node = new node;
Node->data = v;
Node->lchild = Node->rchild = NULL;
return Node;
}
2.2 插入结点
void Insert(node* &root, int x){
if(root == NULL){
root = newNode(x);
return;
}
if(root->data == x){
return;
}else if(x < root->data){
Insert(root->lchild, x);
}else{
Insert(root->rchild, x);
}
}
2.3 建立树
node* Create(int data[], int n){
node* root = NULL;
for (int i = 0; i < n; ++i)
{
Insert(root, data[i]);
}
return root;
}
3 删除结点
3.1 删除前的准备
node* findMax(node* root){
while(root->rchild != NULL){
root = root->rchild;
}
return root;
}
node* findMin(node* root){
while(root->lchild != NULL){
root = root->lchild;
}
return root;
}
3.2 删除结点
void Delete(node* &root, int x){
if (root == NULL)
{
return;
}
if(root->data == x){
if(root->lchild == NULL && root->rchild == NULL){
root = NULL;
}else if(root->lchild != NULL){
node* pre = findMax(root->lchild);
root->data = pre->data;
Delete(root->lchild, pre->data);
}else{
node* next = findMin(root->rchild);
root->data = next->data;
Delete(root->rchild, next->data);
}
}else if(root->data > x){
Delete(root->lchild, x);
}else{
Delete(root->rchild, x);
}
}
4 测试实例
#include <cstdio>
#include <queue>
using std::queue;
typedef int ElemType;
struct node
{
ElemType data;
node* lchild;
node* rchild;
};
void Search(node* root, int x){
if(root == NULL){
printf("searcjh fail!\n");
return;
}
if(root->data == x){
printf("%d\n", root->data);
}else if(x < root->data){
Search(root->lchild, x);
}else{
Search(root->rchild, x);
}
}
node* newNode(int v){
node* Node = new node;
Node->data = v;
Node->lchild = Node->rchild = NULL;
return Node;
}
void Insert(node* &root, int x){
if(root == NULL){
root = newNode(x);
return;
}
if(root->data == x){
return;
}else if(x < root->data){
Insert(root->lchild, x);
}else{
Insert(root->rchild, x);
}
}
node* Create(int data[], int n){
node* root = NULL;
for (int i = 0; i < n; ++i)
{
Insert(root, data[i]);
}
return root;
}
node* findMax(node* root){
while(root->rchild != NULL){
root = root->rchild;
}
return root;
}
node* findMin(node* root){
while(root->lchild != NULL){
root = root->lchild;
}
return root;
}
void Delete(node* &root, int x){
if (root == NULL)
{
return;
}
if(root->data == x){
if(root->lchild == NULL && root->rchild == NULL){
root = NULL;
}else if(root->lchild != NULL){
node* pre = findMax(root->lchild);
root->data = pre->data;
Delete(root->lchild, pre->data);
}else{
node* next = findMin(root->rchild);
root->data = next->data;
Delete(root->rchild, next->data);
}
}else if(root->data > x){
Delete(root->lchild, x);
}else{
Delete(root->rchild, x);
}
}
const int MAXN = 110;
int pre[MAXN] = {5,1,0,3,2,4,8,6,7,9};
int in[MAXN] = {0,1,2,3,4,5,6,7,8,9};
node* Create2(int preL, int preR, int inL, int inR){
if(preL > preR){
return NULL;
}
node* root = new node;
root->data = pre[preL];
int k;
for (k = inL; k <= inR; ++k)
{
if(in[k] == root->data){
break;
}
}
int numLeft = k - inL;
root->lchild = Create2(preL + 1, preL + numLeft, inL, k - 1);
root->rchild = Create2(preL + numLeft + 1, preR, k + 1, inR);
return root;
}
void layorder(node* root){
queue<node*> q;
q.push(root);
while(!q.empty()){
node* now = q.front();
q.pop();
printf("%d\n", now->data);
if(now->lchild != NULL){
q.push(now->lchild);
}
if(now->rchild != NULL){
q.push(now->rchild);
}
}
}
int main(int argc, char const *argv[])
{
node* root;
root = Create2(0, 9, 0, 9);
Delete(root, 5);
layorder(root);
return 0;
}