一:概念
二叉排序树是一颗特殊的二叉树,它是一颗二叉树但同时满足如下条件:对于树上的任意一个结点,其上的数值必大于等于其左子树上任意结点数值,必小于等于其右子树上任意结点的数值。
即 左值 < 中值 < 右值。
二叉排序树进行中序遍历一定是一个递增序列
二:实现功能(函数):
1.创建插入(INSERTBST):
2.中序遍历(INORDER):
3.查找(BSTsearch):
4.查找双亲(PARENT):
5.删除节点(DELETE):
其中:1,2,5都含有递归与非递归的方法
创建二叉树图示:
递归法删除32:
非递归删除32:
三:全码:
#include<stdio.h>
#include<stdlib.h>
#define M 50
typedef struct Tree {
int data;
struct Tree* left;
struct Tree* right;
}Tree, * BitTree;
void INORDER(BitTree T)
{
/* 递归中序:
if (T == NULL) {
return;
}
INORDER(T->left);
printf("%d ", T->data);
INORDER(T->right); */
//非递归
BitTree stack[M], p = T;
int top = -1;
if (T != NULL) {
while (top != -1 || p != NULL) {
while (p != NULL) {
stack[++top] = p;
p = p->left;
}
p = stack[top--];
printf("%d ", p->data);
p = p->right;
}
}
}
//非递归
BitTree insertBST(int dt[])
{
BitTree BT = (BitTree)malloc(sizeof(Tree));
BT->data = dt[0];
BT->left = NULL;
BT->right = NULL;
*dt++;
while (*dt != '\0') {
BitTree T = BT;
BitTree pt = (BitTree)malloc(sizeof(Tree));
pt->data = *dt;
pt->left = NULL;
pt->right = NULL;
while (true) {
if (T->data > *dt) {
if (T->left != NULL) {
T = T->left;
}
else {
T->left = pt;
break;
}
}
else {
if (T->right != NULL) {
T = T->right;
}
else {
T->right = pt;
break;
}
}
}
*dt++;
}
return BT;
}
//递归
void INSERTBST(BitTree& T, int dt)
{
if (T == NULL) {
T = (BitTree)malloc(sizeof(Tree));
T->data = dt;
T->left = NULL;
T->right = NULL;
}
else if (dt < T->data) {
INSERTBST(T->left, dt);
}
else {
INSERTBST(T->right, dt);
}
}
//查找
BitTree BSTsearch(BitTree T, int n)
{
while (T != NULL) {
if (n == T->data) {
return T;
}
else if (n < T->data) {
T = T->left;
}
else {
T = T->right;
}
}
return NULL;
}
//查找双亲
BitTree PARENT(BitTree T, Tree* child)
{
if (T == NULL) {
return NULL;
}
if (T->left == child || T->right == child) {
return T;
}
else if (T->left != NULL) {
PARENT(T->left, child);
}
else if (T->right != NULL) {
PARENT(T->right, child);
}
}
//删除节点,非递归
void DELETE(BitTree* T, int key)
{
if (*T == NULL) {
return;
}
BitTree p = BSTsearch(*T, key);
BitTree q, s;
if (p == NULL) {
puts("没有该节点!");
}
else {
//有右无左
if (p->left == NULL && p->right != NULL) {
q = p->right;
p->data = q->data;
p->right = q->right;
p->left = q->left;
free(q);
}
//有左无右
else if (p->right == NULL && p->left != NULL) {
q = p->left;
p->data = q->data;
p->right = q->right;
p->left = q->left;
free(q);
}
//有左有右
else if (p->right != NULL && p->left != NULL) {
q = p;
s = p->left;
while (s->right) {
q = s;
s = s->right;
}
p->data = s->data;
if (q != p) {
q->right = s->left;
}
else {
q->left = s->left;
}
free(s);
}
//无左右(即结点)
else {
if (*T == p) //根结点
{
free(*T);
*T = NULL;
return;
}
//叶结点
BitTree parent = PARENT(*T, p);
if (parent->left == p) {
parent->left = NULL;
}
else {
parent->right = NULL;
}
free(p);
}
}
}
//递归
BitTree delNode(BitTree T, int key)
{
if (T == NULL)
return T;
if (key < T->data)
T->left = delNode(T->left, key);
else if (key > T->data)
T->right = delNode(T->right, key);
else {
if ((T->left == NULL) || (T->right == NULL)) {
BitTree temp = T->left ? T->left : T->right;
if (temp == NULL) {
temp = T;
T = NULL;
}
else
*T = *temp;
free(temp);
}
else {
BitTree temp = T->right;
while (temp->left != NULL) {
temp = temp->left;
}
T->data = temp->data;
T->right = delNode(T->right, temp->data);
}
}
return T;
}
int main()
{
puts("二叉排序树");
BitTree T1 = NULL;
BitTree T2 = NULL;
int data[8] = { 32,43,61,90,31,54,12 };
//12 31 32 43 54 61 90
puts("非递归:");
T1 = insertBST(data);
INORDER(T1);
puts("\n递归:");
for (int i = 0; i < 7; i++) {
INSERTBST(T2, data[i]);
}
INORDER(T2);
int n = 43;
BitTree T3 = BSTsearch(T2, n);
if (T3 != NULL)
printf("\n搜索到%d节点\n", T3->data);
else
puts("\n没有该值");
n = 32;
printf("删除%d节点:\n", n);
//DELETE(&T1, n);
T1=delNode(T1, n);
INORDER(T1);
return 0;
}