二叉排序树

#include <stdio.h>
#include <stdlib.h>

//二叉排序树结点
typedef struct BSTreeNode {
    int val;
    struct BSTreeNode *lchild;
    struct BSTreeNode *rchild;

} BSTreeNode, *BSTree;

BSTree insert(BSTree root, int val) {
    if (root == NULL) {
        BSTree node = (BSTree) malloc(sizeof(BSTreeNode));
        node->val = val;
        node->lchild = NULL;
        node->rchild = NULL;
        root = node;
    }
    else if (val < root->val) {
        root->lchild = insert(root->lchild, val);
    }
    else {
        root->rchild = insert(root->rchild, val);
    }
    return root;
}

BSTree buildBSTree(BSTree root, int *arr, int n) {
    int i;
    for (i = 0; i < n; i++) {
        root = insert(root, arr[i]);
    }
    return root;
}

//查找元素key,找到返回key的结点指针,没找到返回NULL
BSTree search(BSTree root, int key) {
    if (root == NULL) {
        return NULL;
    }
    if (key > root->val)//查找右子树
    {
        return search(root->rchild, key);
    }
    else if (key < root->val)//查找左子树
    {
        return search(root->lchild, key);
    }
    else {
        return root;
    }
}

int delNode(BSTree *root) {
    BSTree q, s;
    //root为叶子结点
    if (!(*root)->lchild && !(*root)->rchild) { *root = NULL; } else if (!(*root)->lchild) {//左子树为空,重接右子树 q = *root; *root = (*root)->rchild;
        free(q);
    } else if (!(*root)->rchild) {//右子树为空,重接左子树 q = *root; *root = (*root)->rchild;
        free(q);
    } else {//左右子树均不为空
        q = *root;
        s = (*root)->lchild;
        while (s->rchild) {
            q = s;
            s = s->rchild;//转左,然后向右走到尽头
        }
        (*root)->val = s->val;
        if (q != *root) {//判断是否执行上述while循环
            q->rchild = s->lchild;//执行上述while循环,重接右子树
        } else {
            q->lchild = s->lchild;//未执行上述while循环,重接左子树
        }
        free(s);
    }
    return 1;
}

//若二叉排序树T中存在关键字等于key的数据元素时,则删除该数据元素结点
int delBST(BSTree *root, int key) {
    if (!(*root)) {
        return 0;
    }
    else {
        if (key == (*root)->val) { return delNode(root); } else if (key < (*root)->val) { return delBST(&(*root)->lchild, key); } else { return delBST(&(*root)->rchild, key); } } } void visit(BSTree root) { printf("%d ", root->val); } //前序遍历 void preOrder(BSTree root) { if (root != NULL) { visit(root);//访问根结点 preOrder(root->lchild); preOrder(root->rchild); } } //中序遍历 void inOrder(BSTree root) { if (root != NULL) { inOrder(root->lchild); visit(root); inOrder(root->rchild); } } //后序遍历 void postOrder(BSTree root) { if (root != NULL) { postOrder(root->lchild); postOrder(root->rchild); visit(root); } } int main() { int arr[] = {10, 6, 15, 2, 7, 12, 4, 13}; BSTree root = NULL; root = buildBSTree(root, arr, 8); printf("先序遍历二叉树: "); preOrder(root); printf("\n"); /*printf("中序遍历二叉树: "); inOrder(root); printf("\n"); printf("后序遍历二叉树: "); postOrder(root); printf("\n");*/ //查找元素7 /*BSTree node = search(root,7); if (node != NULL){ printf("二叉排序树存在结点值为%d的结点\n",node->val); } else { printf("二叉排序树不存在结点值为7的结点\n"); }*/ /*//查找元素25 BSTree node1 = search(root,25); if (node1 != NULL){ printf("二叉排序树存在结点值为%d的结点\n",node1->val); } else { printf("二叉排序树不存在结点值为25的结点\n"); }*/ if (delBST(&root,2)) { printf("删除结点元素为2的结点\n先序遍历二叉树: "); preOrder(root); } return 0; } 

你可能感兴趣的:(二叉排序树)