第五章 数和二叉树
本部分为数据结构中较为重要部分,因此优先完成,预计分为几个部分单独完成,为常见考点+常见算法+相关算法题总结
考点: 二叉树的存储结构
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct ThreadNode {
ElemType data;
struct ThreadNode* lchild, * rchild;
int ltag, rtag;
}ThreadNode, * ThreadTree;
考点:已知中序+前序/后序序列,还原二叉树
考点:二叉树的遍历🔺
递归写法(前序、中序、后序)
void PreOrderTraverse(BiTree T){
if(T != NULL){
visit(T);
PreOrderTraverse(T->lchild)
PreOrderTraverse(T->rchild)
}
}
非递归写法
void PreOrderTraverse(BiTree T){
Stack S; InitStack(S); BiTNode *p;
if(T != NULL) Push(S, T);
while(!IsEmpty(S)){
Pop(S, p);
visit(p);
if(p->rchild != NULL){
Push(p->rchild)
}
if(p->lchild != NULL){
Push(p->lchild)
}
}
DestroyStack(S);
}
void InOrderTraverse(BiTree T){
Stack S; InitStack(S); BiTNode *p = T;
while(p != NULL || !IsEmpty(S)){
if(p != NULL){
Push(S, p);
p = p->lchild;
}else{
Pop(S, p);
visit(p);
if(p->rchild != NULL)
p = p->rchild;
else
p = NULL;
}
}
DestroyStack(S);
}
void PostOrderTraverse(BiTree T){
Stack S; InitStack(S); BiTNode *p = T, *r = NULL;
while(p != NULL || !IsEmpty(S)){
if(p != NULL){
Push(S, p);
p = p->lchild;
}else{
GteTop(S, p);
if(p->rchild != NULL && p->rchild != r){
p = p->rchild;
Push(S, p);
p = p->lchild;
}else{
Pop(S, p);
visit(p);
r = p;
p = NULL;
}
}
}
DestroyStack(S);
}
void LevelOrder(BiTree T){
Queue Q; InitQueue(Q);
BiTNode *p;
EnQueue(Q,T);
while(!IsEmpty(Q)){
DeQueue(Q, p);
visit(p);
if(p->lchild != NULL)
Enqueue(Q, p->lchild);
if(p->rchild != NULL)
Enqueue(Q, p->rchild);
}
}
考点:线索二叉树(理解)
考点:树的存储结构
typedef struct PTNode{
ElementType data;
int parent;
}PTNode;
typedef struct{
PTNode nodes[MaxSize];
int r, n;
}PTree;
typedef struct CTNode{
int child;
struct CTNode *next;
}*ChildPtr;
typedef struct{
ElementType data;
ChildPtr firstchild;
}CTBox;
typedef struct{
CTBox nodes[MaxSize];
int n, r;
}CTree;
typedef struct CSNode{
ElementType data;
struct CSNode *firstchild, *nextsibling;
}CSNode, *CSTree;
|