王道2025数据结构代码总结:第五章 树与二叉树

5.2.2

二叉树的链式存储

typedef struct BiTNode{
    ElemType data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

5.3.1

先序遍历

void PreOrder(BiTree T){
    if(T!=NULL){
        visit(T);
        PreOrder(T->lchild);
        ProOrder(t->rchild);
    }
}

中序遍历

void InOrder(BiTree T){
    if(T!=NULL){
        InOrder(T->lchild);
        visit(T);
        InOrder(T->rchild);
    }
}

后序遍历

void PostOrder(BiTree T){
    if(T!=NULL){
        PostOrder(T->lchild);
        PostOrder(T->rchild);
        visit(T);   
    }
}

先序遍历的非递归算法

void PreOrder2(BiTree T){
    InitStack(S);
    BiTree p=T;
        while(p||!IsEmpty(S)){
            if(p){
                visit(p);
                Push(S,p);
                p=p->lchild;
            }
            else{
                Pop(S,p);
                p=p->rchild;
            }
        }
}

中序遍历的非递归算法

void InOrder2(BiTree T){
    InitStack(S);
    BiTree p=T;
        while(p||!IsEmpty(S)){
            if(p){
                Push(S,p);
                p=p->lchild;
            }
            else{
                Pop(S,p);
                visit(p);
                p=p->rchild;
            }
        }
}

二叉树的层次遍历

void LevelOrder(BiTree T){
    InitQueue(Q);
    BiTree 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);
        }
}

5.3.2

线索二叉树的存储结构

typedef struct ThreadNode{
    ElemTypr data;
    struct ThreadNode *lchild,*rchild;
    int ltag,rtag;
}ThreadNode,*ThreadTree;

中序遍历对二叉树线索化

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;
}

②节点p在中序序列下的后继

ThreadNode *Nextnode(ThreadNode *p){
    if(p->rtag==0)
        return Firstnode(p->rchild);
    else
        return p->rchild;
}

③不含头结点的中序线索二叉树的中序遍历

void Inorder(ThreadNode *T){
    for(ThreadNode *p=Firstnode(T);p!=NULL;p=Nextnode(p))
        visit(p);
}

5.4.1

双亲表示法的存储结构

#define MAX_TREE_SIZE 100
typedef struct{
    ElemType data;
    int parent;
}PTNode;
typedef struct{
    PTNode nodes[MAX_TREE_SIZE];
    int n;
}PTree;

孩子兄弟表示法

typedef struct CSNode{
    ElemType data;
    struct CSNode *firstchild,*nextsibling;
}CSNode,*CSTree;

5.5.2

并查集的结构

#define SIZE 100
int UFSets[SIZE];

并查集的操作

①初始化操作

void Initial(int S[]){
    for(int i=0;i<SIZE;i++)
        S[i]=-1;
}

②find操作

//在并查集S中查找并返回包含元素x的树的根
int Find(int S[],int x){
    while(S[x]>=0)
        x=S[x];
    return x;
}

③Union操作

void Union(int S[],int Root1,int Root2){
    if(Root1==Root2)
        return;
    S[Root2]=Root1;
}

并查集实现的优化

①改进的Union操作

void Union(int S[],int Root1,int Root2){
    if(Root1==Root2}
        return;
    if(S[Root2>S[Root1]){
        S[Root1]+=S[Root2];
        S[Root2]=Root1;
    }
    else{
        S[Root2]+=S[Root1];
        S[Root1]=Root2;
    }
}

②改进的Find操作

int Find(int S[],int x){
    int root=x;
    while(s[root]>=0)
        root=s[root];
    while(x!=root){
        int t=S[x];
        S[x]=root;
        x=t;
    }
    return root;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值