知识架构
相关代码
#include <iostream>
using namespace std;
#define MaxSize 100
struct TreeNode {
int value[MaxSize];
bool isEmpty;
};
typedef struct BiTNode{
int data;
struct BiTNode *lchild,*rchild;
}BiTNode, *BiNode;
void PreOrder(BiNode T){
if (T != NULL){
cout << T->data << endl;
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
void MidOrder(BiNode T){
if (T != NULL){
MidOrder(T->lchild);
cout << T->data << endl;
MidOrder(T->rchild);
}
}
void PostOrder(BiNode T){
if (T != NULL){
PostOrder(T->lchild);
PostOrder(T->rchild);
cout << T->data << endl;
}
}
int treeDepth(BiNode T){
if (T == NULL){
return 0;
} else {
int l = treeDepth(T->lchild);
int r = treeDepth(T->rchild);
return l>r ? l+1:r+1;
}
}
typedef struct ThreadNode{
int data;
struct ThreadNode *lchild,*rchild;
int ltag,rtag;
}ThreadNode, *ThreadTree;
ThreadNode *pre = NULL;
void visit(ThreadTree q) {
if (q->lchild == NULL){
q->lchild = pre;
q->ltag = 1;
}
if (pre != NULL && pre->rchild == NULL){
pre->rchild = q;
pre->rtag = 1;
}
pre = q;
}
void InThread(ThreadTree T){
if (T != NULL){
InThread(T->lchild);
visit(T);
InThread(T->rchild);
}
}
void PreThread(ThreadTree T){
visit(T);
if (T->ltag != 1){
PreThread(T->lchild);
}
PreThread(T->rchild);
}
|