二叉查找树的生成、遍历、插入和删除

#include<stdio.h>
#include<malloc.h>

typedef struct BTREE {
	int val;
    struct BTREE* leftChild;
    struct BTREE* rightChild;
}BTREE;

typedef unsigned char boolean;
#define	TRUE			1
#define	FALSE			0

BTREE* createPoint(int value);
BTREE* findMinPoint(BTREE* head);
void insertPoint(BTREE* head, int value);
void showBtreeByLeft(BTREE* head);
void showBtreeByMid(BTREE* head);
void deleteBtreePoint(BTREE** root, int value);
void dealDeletePoint(BTREE** point);

void dealDeletePoint(BTREE** point) {
	BTREE* midPoint = NULL;
	if(*point == NULL) {
		return;
	}

	if((*point)->leftChild == NULL && (*point)->rightChild == NULL) {
		(*point)->val = NULL;
		*point = NULL;
		return;
	} else if((*point)->leftChild != NULL && (*point)->rightChild == NULL) {
		*point = (*point)->leftChild;
		return;
	} else if((*point)->rightChild != NULL && (*point)->leftChild == NULL) {
		*point = (*point)->rightChild;
		return;
	} else if((*point)->rightChild != NULL && (*point)->leftChild != NULL) {
		midPoint = findMinPoint(*point);
		(*point)->val = midPoint->val;
		(*point)->leftChild = NULL;
		midPoint->val = NULL;
	} else {
		return;
	}
}

void deleteBtreePoint(BTREE** root, int value) {
	int rootValue = (*root)->val;
	if((*root) == NULL || rootValue == NULL) {
		return;
	}

	if(rootValue == value) {
		dealDeletePoint(root);
	} else if(rootValue > value) {
		deleteBtreePoint(&((*root)->leftChild), value);
	} else if(rootValue < value) {	
		deleteBtreePoint(&((*root)->rightChild), value);
	} else {
		return;
	}
	return;
}

void showBtreeByMid(BTREE* head) {
	if(head == NULL) {
		return;
	}
	showBtreeByMid(head->leftChild);
	printf("%d ", head->val);
	showBtreeByMid(head->rightChild);
}

void showBtreeByLeft(BTREE* head) {
	if(head == NULL) {
		return;
	}

	printf("%d ", head->val);
	showBtreeByLeft(head->leftChild);
	showBtreeByLeft(head->rightChild);
}

void insertPoint(BTREE* root, int value) {
	int rootValue = root->val;
	if(root == NULL) {
		return;
	}
	
	if(root->leftChild == NULL && rootValue > value) {
		root->leftChild = createPoint(value);
		return;
	}

	if(root->rightChild == NULL && rootValue < value) {
		root->rightChild = createPoint(value);
		return;
	}

	if(value > rootValue) {
		insertPoint(root->rightChild, value);
	} else if(value < rootValue) {
		insertPoint(root->leftChild, value);
	} else {
		return;
	}
}

BTREE* findMinPoint(BTREE* head) {
	BTREE* p = NULL;
	if(head->leftChild == NULL) {
		printf("代替值的地址:%p\n", head);
		return head;
	} else if(head->leftChild != NULL) {
		p = findMinPoint(head->leftChild);
		printf("%p\n", p);
	}
	printf("%p\n", p);
	return p;
}
 
BTREE* createPoint(int value) {
	BTREE* btree = NULL;
 	if(value == NULL) {
 		return NULL;
 	}

	btree = (BTREE *)malloc(sizeof(BTREE));
	btree->val = value;
	btree->leftChild = btree->rightChild = NULL;

	return btree;
}

void main(void) {
	BTREE* root = createPoint(10);
	BTREE* point = NULL;
	insertPoint(root, 6);
	insertPoint(root, 1);
	insertPoint(root, 7);
	insertPoint(root, 14);
	insertPoint(root, 11);
	insertPoint(root, 21);
	insertPoint(root, 9);
	insertPoint(root, 24);
	printf("删除前的先序遍历\n");
	showBtreeByLeft(root);
	printf("\n");
	printf("删除前的中序遍历\n");
	showBtreeByMid(root);

	deleteBtreePoint(&root,21);
	printf("\n");
	printf("删除后的先序遍历\n");
	showBtreeByLeft(root);
	printf("\n");
	printf("删除后的中序遍历\n");
	showBtreeByMid(root);
	printf("\n");

	deleteBtreePoint(&root,7);
	printf("\n");
	printf("删除后的先序遍历\n");
	showBtreeByLeft(root);
	printf("\n");
	printf("删除后的中序遍历\n");
	showBtreeByMid(root);
	printf("\n");
}

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/sjg_sjk/article/details/80293212
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞