目录
- 5.3.1 二叉树的遍历
-
- 1.function.h
- 2. main.cpp
- 3. stack.cpp
- 4. queue.cpp
- 5.3.2 线索二叉树
- 5.5.1 二叉排序树
5.3.1 二叉树的遍历
1.function.h
#include <stdio.h>
#include <stdlib.h>
typedef char BiElemType;
typedef struct BiTNode {
BiElemType c;
struct BiTNode* lchild;
struct BiTNode* rchild;
}BiTNode, * BiTree;
typedef struct tag {
BiTree p;
struct tag* pnext;
}tag_t, * ptag_t;
#define MaxSize 50
typedef BiTree ElemType;
typedef struct {
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack& S);
bool StackEmpty(SqStack& S);
bool Push(SqStack& S, ElemType x);
bool Pop(SqStack& S, ElemType& x);
bool GetTop(SqStack& S, ElemType& x);
typedef struct LinkNode {
ElemType data;
struct LinkNode* next;
}LinkNode;
typedef struct {
LinkNode* front, * rear;
}LinkQueue;
void InitQueue(LinkQueue& Q);
bool IsEmpty(LinkQueue Q);
void EnQueue(LinkQueue& Q, ElemType x);
bool DeQueue(LinkQueue& Q, ElemType& x);
2. main.cpp
#include "function.h"
#define _CRT_SECURE_NO_WARNINGS
void preOrder(BiTree p)
{
if (p != NULL)
{
putchar(p->c);
preOrder(p->lchild);
preOrder(p->rchild);
}
}
void InOrder(BiTree p)
{
if (p != NULL)
{
InOrder(p->lchild);
putchar(p->c);
InOrder(p->rchild);
}
}
void PostOrder(BiTree p)
{
if (p != NULL)
{
PostOrder(p->lchild);
PostOrder(p->rchild);
putchar(p->c);
}
}
void InOrder2(BiTree T)
{
SqStack S;
InitStack(S); BiTree p = T;
while (p || !StackEmpty(S))
{
if (p)
{
Push(S, p);
p = p->lchild;
}
else {
Pop(S, p); putchar(p->c);
p = p->rchild;
}
}
}
void LevelOrder(BiTree T)
{
LinkQueue Q;
InitQueue(Q);
BiTree p = T;
EnQueue(Q, T);
while (!IsEmpty(Q))
{
DeQueue(Q, p);
putchar(p->c);
if (p->lchild != NULL)
EnQueue(Q, p->lchild);
if (p->rchild != NULL)
EnQueue(Q, p->rchild);
}
}
int main() {
BiTree tree = NULL;
BiTree tpnew;
ptag_t phead = NULL, ptail = NULL,listpnew = NULL, pcur = NULL;
char c;
while (scanf_s("%c", &c) != EOF) {
if ('\n' == c) {
break;
}
tpnew = (BiTree)calloc(1, sizeof(BiTNode));
tpnew->c = c;
listpnew = (ptag_t)calloc(1, sizeof(tag_t));
listpnew->p = tpnew;
if (NULL == tree) {
tree = tpnew;
phead = listpnew;
ptail = listpnew;
pcur = listpnew;
continue;
}
else {
ptail->pnext = listpnew;
ptail = listpnew;
}
if (NULL == pcur->p->lchild) {
pcur->p->lchild = tpnew;
}
else if (NULL == pcur->p->rchild) {
pcur->p->rchild = tpnew;
pcur = pcur->pnext;
}
}
InOrder(tree);
printf("\n");
PostOrder(tree);
printf("\n");
LevelOrder(tree);
}
3. stack.cpp
#include "fun.h"
void InitStack(SqStack& S)
{
S.top = -1;
}
bool StackEmpty(SqStack& S)
{
if (S.top == -1)
return true;
else
return false;
}
bool Push(SqStack& S, ElemType x)
{
if (S.top == MaxSize - 1)
{
return false;
}
S.data[++S.top] = x;
return true;
}
bool Pop(SqStack& S, ElemType& x)
{
if (-1 == S.top)
return false;
x = S.data[S.top--];
return true;
}
bool GetTop(SqStack& S, ElemType& x)
{
if (-1 == S.top)
return false;
x = S.data[S.top];
return true;
}
4. queue.cpp
#include "fun.h"
void InitQueue(LinkQueue& Q)
{
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
bool IsEmpty(LinkQueue Q)
{
if (Q.front == Q.rear)
return true;
else
return false;
}
void EnQueue(LinkQueue& Q, ElemType x)
{
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = x; s->next = NULL;
Q.rear->next = s;
Q.rear = s;
}
bool DeQueue(LinkQueue& Q, ElemType& x)
{
if (Q.front == Q.rear) return false;
LinkNode* p = Q.front->next;
x = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
free(p);
return true;
}
5.3.2 线索二叉树
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char ElemType;
typedef struct ThreadNode {
ElemType data;
struct ThreadNode* lchild, * rchild;
int ltag, rtag;
}ThreadNode, * ThreadTree;
void BulidThreadTree(ThreadTree& T)
{
ThreadTree arr[5];
int i;
for (i = 0; i < 5; i++)
{
arr[i] = (ThreadTree)malloc(sizeof(ThreadNode));
memset(arr[i], 0, sizeof(ThreadNode));
arr[i]->data = 'A' + i;
}
arr[0]->lchild = arr[1];
arr[0]->rchild = arr[2];
arr[1]->rchild = arr[3];
arr[2]->lchild = arr[4];
T = arr[0];
}
void InThread(ThreadTree& p, ThreadTree& pre)
{
if (p != NULL) {
InThread(p->lchild, pre);
if (p->lchild == NULL) {
p->lchild = pre;
p->ltag = 1;
}
if (pre != NULL && pre->rchild == NULL) {
pre->rchild = p;
pre->rtag = 1;
}
pre = p;
InThread(p->rchild, pre);
}
}
void CreateInThread(ThreadTree T)
{
ThreadTree pre = NULL;
if (T != NULL) {
InThread(T, pre);
pre->rchild = NULL;
pre->rtag = 1;
}
}
ThreadNode* Firstnode(ThreadNode* p)
{
while (p->ltag == 0)
p = p->lchild;
return p;
}
int main()
{
ThreadTree T;
ThreadTree p;
BulidThreadTree(T);
CreateInThread(T);
p = Firstnode(T);
printf("最左下结点值为 %c\n", p->data);
}
5.5.1 二叉排序树
#include <stdio.h>
#include <stdlib.h>
typedef int KeyType;
typedef struct BSTNode {
KeyType key;
struct BSTNode* lchild, * rchild;
}BSTNode, *BiTree;
int BST_Insert(BiTree& T, KeyType k) {
if (NULL == T) {
T = (BiTree)malloc(sizeof(BSTNode));
T->key = k;
T->lchild = T->rchild = NULL;
return 1;
}
else if (k == T->key) {
return 0;
}
else if (k < T->key) {
return BST_Insert(T->lchild, k);
}
else {
return BST_Insert(T->rchild, k);
}
}
void Creat_BST(BiTree& T, KeyType str[], int n) {
T = NULL;
int i = 0;
while (i < n) {
BST_Insert(T, str[i]);
i++;
}
}
BSTNode* BST_Search(BiTree T, KeyType k, BiTree& p) {
p == NULL;
while (T != NULL && T->key != k) {
if (k < T->key) {
T = T->lchild;
p = T;
}
else {
T = T->rchild;
p = T;
}
}
return T;
}
void DeleteNode(BiTree& root, KeyType x) {
if (NULL == root) {
return;
}
if (x < root->key) {
DeleteNode(root->lchild, x);
}
if (x > root->key) {
DeleteNode(root->rchild, x);
}
else {
if (NULL == root->lchild) {
BiTree TNode = root;
root = root->rchild;
free(TNode);
}
else if (NULL == root->rchild) {
BiTree TNode = root;
root = root->lchild;
free(TNode);
}
else {
BiTree TNode = root->lchild;
if (TNode->lchild != NULL) {
TNode = TNode->rchild;
}
root->key = TNode->key;
DeleteNode(root->lchild, TNode->key);
}
}
}
void InOrder(BiTree T) {
if (T != NULL) {
InOrder(T->lchild);
printf("%3d", T->key);
InOrder(T->rchild);
}
}
int main()
{
BiTree T;
BiTree parent;
BiTree search;
KeyType str[] = { 54,20,66,40,28,79,58 };
Creat_BST(T, str, 7);
InOrder(T);
printf("\n");
search = BST_Search(T, 40, parent);
if (search)
{
printf("找到对应结点,值=%d\n", search->key);
}
else {
printf("未找到对应结点\n");
}
DeleteNode(T, 66);
InOrder(T);
printf("\n");
}