代码是根据算法导论来写的。
binary_tree.h:
#ifndef BINARY_TREE_H #define BINARY_TREE_H struct node { int key; struct node *left; struct node *right; struct node *parent; }; struct node * minimum(struct node *x); struct node * maximum(struct node *x); struct node * successor(struct node *x); struct node * predecessor(struct node *x); void delete(struct node **root, struct node *z); void delete_by_key(struct node **root, int key); void insert(struct node ** root, int key); void inorder_traverse(struct node *x); struct node * find(struct node *x, int key); #endifbinary_tree.c:
#include "binary_tree.h" #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> struct node *minimum(struct node *x) { while(x->left != NULL) { x = x->left; } return x; } struct node *maximum(struct node *x) { while(x->right != NULL) { x= x->right; } return x; } struct node *successor(struct node *x) { if(x->right != NULL) { return minimum(x->right); } struct node *y = x->parent; while(y != NULL && x == y->right) { x = y; y = y->parent; } return y; } struct node *predecessor(struct node *x) { if(x->left != NULL) { return maximum(x->left); } struct node *y = x->parent; while(y != NULL && x == y->left) { x = y; y = y->parent; } return y; } void insert(struct node ** root, int key) { struct node *y = NULL; struct node *x = *root; struct node *z = malloc(sizeof(struct node)); memset(z, 0, sizeof(struct node)); z->key = key; while (x != NULL) { y = x; if(z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if( y == NULL) { *root = z; } else if(z->key < y->key) { y->left = z; } else { y->right = z; } } static void transplant(struct node **root, struct node *u, struct node *v) { if(u->parent == NULL) { *root = v; } else if(u->parent->left == u) { u->parent->left = v; } else //if(u->parent->right == u) { u->parent->right = v; } if(v != NULL) { v->parent = u->parent; } } void delete(struct node **root, struct node *z) { if(z->left == NULL) { transplant(root, z, z->right); } else if(z->right == NULL) { transplant(root, z, z->left); } else { struct node *y = minimum(z->right); if(y->parent != z) { transplant(root, y, y->right); y->right = z->right; y->right->parent = y; } transplant(root, z, y); y->left = z->left; y->left->parent = y; } free(z); } void delete_by_key(struct node **root, int key) { struct node *z = find(*root, key); if(z != NULL) { delete(root, z); } } struct node *find(struct node * x, int key) { if(x == NULL || x->key == key) { return x; } if(key < x->key) { return find(x->left, key); } else { return find(x->right, key); } } void inorder_traverse(struct node *x) { if(x != NULL) { inorder_traverse(x->left); printf("key: %d\n", x->key); inorder_traverse(x->right); } } void postorder_destruct(struct node **x) { if(*x != NULL) { postorder_destruct(&(*x)->left); postorder_destruct(&(*x)->right); free(*x); *x = NULL; } }测试代码:
#include "binary_tree.h" #include <stdlib.h> #include <string.h> #include <assert.h> #include <stdio.h> int main(void) { struct node *root = NULL; int i; int max = 0; int keys[15]; for(i = 0; i < 15; i++) { //struct node *x = (struct node *)malloc(sizeof(struct node)); // memset(x, 0, sizeof(struct node)); // x->key = rand() % 100; int key = rand()%100; if(key > max) { max = key; } keys[i] = key; insert(&root, key); } printf("saved max = %d, found max = %d\n", max, maximum(root)->key); if(max != maximum(root)->key) { assert(0); } //printf("saved max = %d, found max = %d\n", max, maximum(root)->key); inorder_traverse(root); printf("\n\nAfter delete %d\n\n", keys[5]); delete_by_key(&root, keys[5]); inorder_traverse(root); postorder_destruct(&root); //insert(&root, return 0; }