排序二叉树

排序二叉树

性质

  1. 左子树 < 根节点
  2. 右子树 > 根节点
  3. 中序遍历的结果,是一个有序序列
  4. 只需要前序遍历或者后续遍历结果就能还原

插入操作

  1. 插入的新节点,一定会做为叶子结点

删除操作

  1. 删除度为0的节点,直接删除
  2. 删除度为1的节点,把『孤儿子树』挂到其父节点上面去
  3. 删除度为2的节点,可以转化成删除度为1或度为0的节点

对于度为2的节点:

  1. 前驱:左子树最大值
  2. 后继:右子树最小值

二叉排序树代码

注释部分为如何查找排名第K大的元素

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

typedef struct Node {
    int key, size; //size用来存储节点数量
    struct Node *lchild, *rchild;
} Node; 
/*
	小技巧一个,用NIL这个实在的节点,来替换NULL空地址,换就完事。在计算插入操作等涉及计数的操作可以不用考虑空地址,毕竟换成NIL后key,size都是0随便加不影响结果
*/
Node __NIL;
#define NIL (&__NIL)  //(&__NIL)是地址
__attribute__((constructor)) 
void init_NIL() {
    NIL->key = 0;
    NIL->size = 0;
    NIL->lchild = NIL->rchild = NIL;
    return ;
}

Node *getNewNode(int key) {
    Node *p = (Node *)malloc(sizeof(Node));
    p->key = key;
    p->size = 1;
    p->lchild = p->rchild = NIL;
    return p;
}

void update_size(Node *root) {
    root->size = root->lchild->size + root->rchild->size + 1;
    return ;
}

Node *insert(Node *root, int key) {
    if (root == NIL) return getNewNode(key);
    if (root->key == key) return root;
    if (root->key > key) root->lchild = insert(root->lchild, key);
    else root->rchild = insert(root->rchild, key);
    update_size(root);
    return root;
}

Node *predecessor(Node *root) {
    Node *temp = root->lchild;
    while (temp->rchild != NIL) temp = temp->rchild;
    return temp;
}

Node *erase(Node *root, int key) {
    if (root == NIL) return root;
    if (root->key > key) {
        root->lchild = erase(root->lchild, key);
    } else if (root->key < key) {
        root->rchild = erase(root->rchild, key);
    } else {
        if (root->lchild == NIL || root->rchild == NIL) {
            Node *temp = root->lchild ? root->lchild : root->rchild;
            free(root);
            return temp;
        } else {
            Node *temp = predecessor(root);
            root->key = temp->key;
            root->lchild = erase(root->lchild, temp->key);
        }
    }
    update_size(root);
    return root;
}

void clear(Node *root) {
    if (root == NIL) return ;
    clear(root->lchild);
    clear(root->rchild);
    free(root);
    return ;
}

Node *rand_insert(Node *root) {
    root = insert(root, rand() % 30);
    return root;
}
/*
这些遍历就是递归,在很多涉及遍历解题的时候,大多数都是用递归
*/
void pre_order(Node *root) {
    if (root == NIL) return ;
    printf("%d ", root->key);
    pre_order(root->lchild);
    pre_order(root->rchild);
    return ;
}

void in_order(Node *root) {
    if (root == NIL) return ;
    in_order(root->lchild);
    printf("%d ", root->key);
    in_order(root->rchild);
    return ;
}

void post_order(Node *root) {
    if (root == NIL) return ;
    post_order(root->lchild);
    post_order(root->rchild);
    printf("%d ", root->key);
    return ;
}

/*int find_k(Node *root, int k) {  //输出第K大的值
    if (root->rchild->size >= k) 	 //k->r->k
   		 return find_k(root->rchild, k);      
    if (root->rchild->size + 1 == k) //k->根
    	return root->key;            
    return find_k(root->lchild, k - root->rchild->size - 1);
}*/

int main() {
    srand(time(0));
    int n;
    scanf("%d", &n);
    Node *root = NIL;
    for (int i = 0; i < n; i++) {
        root = rand_insert(root);
    }
    pre_order(root); printf("\n");  //前
    in_order(root); printf("\n"); //中
    post_order(root); printf("\n"); //后
    /*int k;
    while (~scanf("%d", &k)) {
        printf("find %dth element = %d\n", k, find_k(root, k));
    }*/
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值