#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;
typedef struct BTNode *Position;
typedef Position BTree;
struct BTNode
{
char data;
Position lChild, rChild;
};
BTree CreateBTree(BTree bt, bool isRoot)
{
char ch;
if (isRoot)
printf("Root: ");
scanf_s("%c", &ch);
getchar();
if (ch != '#') //输入'#'表示该节点为空
{
isRoot = false;
bt = new BTNode;
bt->data = ch;
bt->lChild = NULL;
bt->rChild = NULL;
printf("%c's left child is: ", bt->data);
bt->lChild = CreateBTree(bt->lChild, isRoot);
printf("%c's right child is: ", bt->data);
bt->rChild = CreateBTree(bt->rChild, isRoot);
}
return bt;
}
//前序遍历打印在最前,先左子树,后右子树,每个节点经过一次即打印
void PreOrderTraverse(BTree T)
{
if (T == NULL)
return;
printf("%c", T->data);
PreOrderTraverse(T->lChild);
PreOrderTraverse(T->rChild);
}
//中序遍历打印在中间,先左子树,后右子树,每个节点经过两次才打印
void InOrderTraverse(BTree T)
{
if (T == NULL)
return;
InOrderTraverse(T->lChild);
printf("%c", T->data);
InOrderTraverse(T->rChild);
}
//前序遍历打印在最后,先左子树,后右子树,每个节点经过三次才打印
void PostOrderTraverse(BTree T)
{
if (T == NULL)
return;
PostOrderTraverse(T->lChild);
PostOrderTraverse(T->rChild);
printf("%c", T->data);
}
void LayerOrderTraverse(BTree T)
{
queue<BTree> q;
if (T != NULL)
{
q.push(T);
}
while (q.empty() == false)
{
printf("%c", q.front()->data);
if (q.front()->lChild != NULL)
{
q.push(q.front()->lChild);
}
if (q.front()->rChild != NULL)
{
q.push(q.front()->rChild);
}
q.pop();
}
}
void Insert(BTree T, char value)
{
BTree parent = NULL;
BTree head = T;
BTree p = (BTree)malloc(sizeof(BTree));
p->data = value;
p->lChild = p->rChild = NULL;
while (head)
{
parent = head;
if (value < head->data)
head = head->lChild;
else
head = head->rChild;
}
if (value < parent->data)
parent->lChild = p;
else
parent->rChild = p;
}
BTree SearchParentofSNode(BTree Pnod, BTree nod)
{
while (1)
{
if (nod->lChild != NULL)
{
Pnod = nod;
nod = nod->lChild;
}
else
{
break;
}
}
return Pnod;
}
BTree SearchSuccessorNode(BTree nod)
{
while (1)
{
if (nod->lChild != NULL)
{
nod = nod->lChild;
}
else
{
break;
}
}
return nod;
}
BTree DeleteData(BTree parent, BTree cur, char value)
{
BTree SNode = NULL;
BTree PSNode = NULL;
BTree temp = NULL;
if (cur == NULL)
{
printf("\n删除节点不存在\n");
return NULL;
}
else if (value > cur->data)
{
cur->rChild = DeleteData(cur, cur->rChild, value);
}
else if (value < cur->data)
{
cur->lChild = DeleteData(cur, cur->lChild, value);
}
else if (value == cur->data)
{
if (cur->lChild == NULL && cur->rChild == NULL)
{
free(cur);
return NULL;
}
else if (cur->lChild != NULL && cur->rChild == NULL)
{
temp = cur->lChild;
free(cur);
return temp;
}
else if (cur->lChild == NULL && cur->rChild != NULL)
{
temp = cur->rChild;
free(cur);
return cur->rChild;
}
else if (cur->lChild != NULL && cur->rChild != NULL)
{
SNode = SearchSuccessorNode(cur->rChild);
PSNode = SearchParentofSNode(cur->rChild, cur->rChild);
if (cur->rChild == SNode)
{
SNode->lChild = cur->lChild;
free(cur);
return SNode;
}
else if (cur->rChild != SNode && SNode->rChild == NULL)
{
SNode->lChild = cur->lChild;
SNode->rChild = cur->rChild;
PSNode->lChild = NULL;
free(cur);
return SNode;
}
else if (cur->rChild != SNode && SNode->rChild != NULL)
{
PSNode->lChild = SNode->rChild;
SNode->lChild = cur->lChild;
SNode->rChild = cur->rChild;
free(cur);
return SNode;
}
}
}
return cur;
}
void SearchData(BTree T, char value)
{
if (T != NULL)
{
if (T->data == value)
{
printf("\n查找值存在,值为%c\n", T->data);
}
else if (T->data > value)
{
SearchData(T->lChild, value);
}
else if (T->data < value)
{
SearchData(T->rChild, value);
}
}
else if (T == NULL)
{
printf("\n查找值不存在\n");
}
}
int main()
{
BTree nod = NULL;
BTree bt = NULL;
bt = CreateBTree(bt, true);
printf("前序遍历: \n");
PreOrderTraverse(bt);
printf("\n中序遍历: \n");
InOrderTraverse(bt);
printf("\n后序遍历: \n");
PostOrderTraverse(bt);
printf("\n层次遍历: \n");
LayerOrderTraverse(bt);
printf("\n插入值: \n");
Insert(bt, 'M');
LayerOrderTraverse(bt);
printf("\n查找值:");
SearchData(bt, 'A');
printf("删除值: \n");
nod = DeleteData(bt, bt, 'A');
LayerOrderTraverse(nod);
getchar();
return 0;
}