二叉搜索树

来源:互联网 发布:sql 查找not exist 编辑:程序博客网 时间:2024/06/15 09:19

http://paste.ubuntu.com/    一个粘代码的好地方


#include <iostream>#include <algorithm>#include <cstdio>#include <cstdlib>#include <cstring>#include <stack>#include <queue>#include <vector>using namespace std;struct node {    int num;    struct node *left;    struct node *right;};struct node *CreatNode(int n) {    struct node *tmp = (struct node*)malloc(sizeof(struct node));    tmp->num = n;    tmp->left = tmp->right = NULL;    return tmp;};struct node *Insert(struct node *root, int n) {    if(root == NULL)        root = CreatNode(n);    else if(n < root->num)        root->left = Insert(root->left, n);    else if(n > root->num)        root->right = Insert(root->right, n);    return root;}void FirVisited(struct node *root) {    if(root) {        printf("%d ", root->num);        FirVisited(root->left);        FirVisited(root->right);    }}void bfs(struct node *root) {    if(!root) {        printf("The tree is empty\n");        return ;    }    queue<struct node*> q;    q.push(root);    while(!q.empty()) {        struct node *t = q.front();        printf("%d ", t->num);        q.pop();        if(t->left) q.push(t->left);        if(t->right) q.push(t->right);    }    printf("\n");}void PrintLeaf(struct node *root) {    if(!root) {        printf("The tree is empty\n");        return ;    }    queue<struct node*> q;    q.push(root);    while(!q.empty()) {        struct node *t = q.front();        q.pop();        if(t->left) q.push(t->left);        if(t->right) q.push(t->right);        if(t->left == NULL && t->right == NULL)            printf("%d ", t->num);    }    printf("\n");}int GetHeight(struct node *root) {    if(root == NULL) return 0;    int hl = GetHeight(root->left);    int hr = GetHeight(root->right);    return 1 + max(hl, hr);}struct node *FindMin(struct node *root) {    if(!root) return root;    struct node *tmp = root; //不能直接改root    while(tmp->left) {        tmp = tmp->left;    }    return tmp;}struct node *Delete(struct node *root, int x) { //※    if(!root) return NULL;    if(x < root->num)        root->left = Delete(root->left, x);    else if(x > root->num)        root->right = Delete(root->right, x);    else {        struct node *tmp = root;        //初始化tmp只是为了避免在free时编译器报错好烦        if(root->left && root->right) {            tmp = FindMin(root->right);            root->num = tmp->num;            root->right = Delete(root->right, tmp->num);        }        else {            if(root->left == NULL) root = root->right;            else if(root->right == NULL) root = root->left;            else free(tmp);        }    }    return root;};int main() {    struct node *root = NULL;    int n, t;    scanf("%d", &n);    for(int i = 0; i < n; i++) {        scanf("%d", &t);        root = Insert(root, t);    }    printf("firvis: "); FirVisited(root);    printf("\n");    printf("bfs:"); bfs(root);    printf("Height = %d\n", GetHeight(root));    struct node *Min = FindMin(root);    if(Min) printf("Min = %d\n", Min->num);    else printf("The tree is empty\n");    printf("Leaf: "); PrintLeaf(root);    printf("Input the number you want to delete\n");    scanf("%d", &t);    Delete(root, t);    bfs(root);    return 0;}


0 0
原创粉丝点击