二叉树的基本算法实现

代码是根据算法导论来写的。

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);

#endif
binary_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;
}

测试结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值