二叉查找树的建立,遍历,插入与删除

#include <bits/stdc++.h>
using namespace std;
int a[1005];
typedef struct node {
    int data;
    struct node *lchild, *rchild;
    node() {
        data = 0;
        lchild = rchild = NULL;
    }
}*pnode, BSTnode;
pnode InsertBST(pnode root, int data) {//插入元素
    if(root == NULL) {
        root = new node;
        root->data = data;
        root->lchild = NULL;
        root->rchild = NULL;
        return root;
    }
    if(root->data < data) {
        root->rchild = InsertBST(root->rchild, data);
    } else if(root->data > data) {
        root->lchild = InsertBST(root->lchild, data);
    }
    return root;
}
void CreatTree(pnode &root, int num, int a[]) {//建立二叉树
    for(int i = 0; i < num; i++) {
        root = InsertBST(root, a[i]);
    }
}
void SerchBST(pnode root, int data) {//查找二叉树中是否存在查找的元素
    if (root == NULL) {
        cout << "No find." << endl;
        return;
    }
    if(root->data > data) {
        SerchBST(root->lchild, data);
    } else if(root ->data < data) {
        SerchBST(root->rchild, data);
    } else {
        cout << "Find in the BSTtree." << endl;
        return;
    }
}
void InOrder(pnode T) {//中序遍历
    if(T) {
        InOrder(T->lchild);
        cout << T->data << " ";
        InOrder(T->rchild);
    }
}
pnode SerchMin(pnode root) {
    if(root == NULL) return NULL;
    if(root->lchild != NULL) return SerchMin(root->lchild);
    return root;
}
pnode SerchMax(pnode root) {
    if(root == NULL) return NULL;
    if(root->rchild != NULL) return SerchMax(root->rchild);
    return root;
}
pnode DeleteNode(pnode root, int data) {//删除值为data的节点
    if(root == NULL) return root;//
    if(root->data > data) {
        root->lchild = DeleteNode(root->lchild, data);
    } else if(root->data < data) {
        root->rchild = DeleteNode(root->rchild, data);
    } else  if(root->lchild != NULL && root->rchild != NULL) {
        root->data = SerchMax(root->rchild)->data;
        DeleteNode(root->rchild, root->data);
    } else {
        if(root->lchild != NULL) {
            pnode old=root->lchild;
            delete root;
            root = old;
        } else {
           pnode old=root->rchild;
            delete root;
            root = old;
        }
    }
    return root;
}

int main() {
    pnode root = NULL; //root 是二叉查找树的根节点,只是个指针
    cout << "Please scanf the number of the BSTtree" << endl;
    int n;
    scanf("%d", &n);
    cout << "Please scanf each of the element" << endl;
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    CreatTree(root, n, a);//建立二叉查找树
    cout << "The order of the array from small to large is " ;
    InOrder(root);//中序递归遍历,即输出的结果应该是从小到大有序的
    cout << endl;
    pnode p;
    p = SerchMin(root);//最小值
    printf("The smallest number is %d\n", p->data);
    p = SerchMax(root);//最大值
    printf("The largest number is %d\n", p->data);
    DeleteNode(root, 3);//中间的值可以修改
    InOrder(root);
    return 0;
}
/* 10 1231 324 2 3 123 13 23 1321 56 6575 */
    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/yiqzq/article/details/80780254
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞