算法模板梳理2--全国大学生算法设计与编程挑战赛(秋季赛)

8.先序输出叶节点

//先序输出叶子节点
void PreorderPrintLeaves(BinTree BT) {
    if(BT == NULL) {
        printf("");
    }
    else {
        if(BT->Left == NULL && BT->Right == NULL) {
            printf(" %c",BT->Data);
        }
        PreorderPrintLeaves(BT->Left);
        PreorderPrintLeaves(BT->Right);
    }
}

9.二叉搜索树的五种常用操作

//二叉搜索树的操作集
//二叉搜索树的插入
BinTree Insert( BinTree BST, ElementType X )  // 插入创建二叉搜索树
{
    if(!BST)
    {
        BST = (BinTree)malloc(sizeof(struct TNode));
        BST->Data = X;
        BST->Left = NULL;
        BST->Right= NULL;
    }
    else
    {
        if(X < BST->Data) BST->Left = Insert(BST->Left,X);
        if(X > BST->Data) BST->Right= Insert(BST->Right,X);
    }
    return BST;
}
//二叉搜索树的删除
BinTree Delete( BinTree BST, ElementType X )
{
    BinTree t;
    if(!BST) printf("Not Found\n");
    else
    {
        if( X < BST->Data) BST->Left = Delete(BST->Left,X);    //当前值小于结点值,在左子树下
        else if(X > BST->Data) BST->Right = Delete(BST->Right,X);  //当前值大于结点值,在右子树下
        else  //已找到要删除的结点
        {
            if(BST->Left && BST->Right)   //删除的结点有左右子结点
            {
                t = FindMin(BST->Right);  //从右子树中找出最小的元素填充要删除的结点
                BST->Data = t->Data;
                BST->Right = Delete(BST->Right,BST->Data);  //递归从右子树中删除最小元素
            }
            else   // 删除的结点有1个或0个子节点
            {
                t = BST; 
                if(!BST->Left) BST = BST->Right;  //只有右子结点或无子结点
                else    BST = BST->Left;  //只有左子结点或无子结点
                free(t);
            }
        }
    }
    return BST;
}
//二叉搜索树的查询
Position Find( BinTree BST, ElementType X )
{
    while(BST)
    {
        if(X > BST->Data) BST = BST->Right;
        else if(X < BST->Data) BST = BST->Left;
        else break;
    }
    return BST;
}
//二叉搜索树查询最小值
Position FindMin( BinTree BST )
{
    if( !BST ) return NULL;
    else if(!BST->Left) return BST;
    else return FindMin(BST->Left);
}
//二叉搜索树查询最大值
Position FindMax( BinTree BST )
{
   if(BST)
   {
     while(BST->Right) BST = BST->Right;
   }
   return BST;
}

10.判断是否为二叉搜索树

//判断是否为二叉搜索树
bool IsBST(BinTree T) {
    if((!T)||((!T->Left)&&(!T->Right))) return true;
    BinTree p = T->Left;
    if(p) {
        while(p->Right) p = p->Right;
        if(p->Data > T->Data) return false;
    }
    if(T->Right) return IsBST(T->Right);
    else return true;
}

11.二叉树求深度和叶子数

//求解二叉树的深度和叶子数
//求解二叉树的深度
int GetDepthOfBiTree(BiTree T) {
    //边界情况的讨论
    if(T == NULL) {
        return 0;
    }
    int sum1 = GetDepthOfBiTree(T->lchild);
    int sum2 =  GetDepthOfBiTree(T->rchild);
    if(sum1 > sum2) {
        return sum1 + 1;
    }
    else return sum2 + 1;
}
//求解二叉树的叶子数
int LeafCount(BiTree T) {
    //边界情况的讨论
    if(T == NULL) {
        return 0;
    }
    else if(T->lchild == NULL&&T->rchild == NULL) {
        return 1;
    }
    else return(LeafCount(T->lchild) + LeafCount(T->rchild));
}

12.二叉树的非递归遍历

//二叉树的非递归遍历
//中序的非递归遍历
void InorderTraversal(BinTree BT) {
    BinTree T = BT;
    Stack S = CreateStack();
    while(T||!IsEmpty(S)) {
        while(T != NULL) {
            Push(S,T);
            T = T->Left;
        } 
        T = Pop(S);
        printf(" %c",T->Data);
        T = T->Right;
    }
}
//前序的非递归遍历
void PreorderTraversal(BinTree BT) {
    BinTree T = BT;
    Stack S = CreateStack();
    while(T||!IsEmpty(S)) {
        while(T != NULL) {
            Push(S,T);
            printf(" %c",T->Data);
            T = T->Left;
        }
        T = Pop(S);
        T = T->Right;
    }
}
//后序的非递归遍历
void PostorderTraversal(BinTree BT) {
    BinTree T = BT;
    Stack S = CreateStack();
    while(T||!IsEmpty(S)) {
        while(T != NULL) {
            T->flag = 0;
            Push(S,T);
            T = T->Left;
        }
    T = Peek(S);
    if(T->flag == 0) {
        T->flag++;
        T = T->Right;
    }
    else {
        T = Pop(S);
        printf(" %c",T->Data);
        T = NULL;
        }
    }
}

13.二叉树度为1的结点记数

//二叉树度为1的结点记数
int countSCNodes(struct BinTree *bt) {
    if(bt == NULL) return 0;
    if((bt->left == NULL&&bt->right != NULL)||(bt->left != NULL&&bt->right == NULL)) {
        return 1 + countSCNodes(bt->left) + countSCNodes(bt->right);
    }
    else {
        return countSCNodes(bt->left) + countSCNodes(bt->right);
    }
}

14.二叉树度为2的结点记数

//二叉树度为2的结点记数
int countDCNodes(struct BinTree *bt) {
    if(!bt) return 0;
    else if(bt->left&&bt->right) {
        return 1 + countDCNodes(bt->left) + countDCNodes(bt->right);
    }
    else {
        return countDCNodes(bt->left) + countDCNodes(bt->right);
    }
}

15.二叉树叶节点求和

//二叉树叶结点求和
int sumLeaves(struct BinTree *bt) {
    int count = 0;
    if(bt != NULL) {
        count = count + sumLeaves(bt->left);
        count = count + sumLeaves(bt->right);
        if(bt->left == NULL && bt->right == NULL) {
            count = count + bt->data;
        }
    }
    return count;
}

16.二叉树度为1的结点求和

//二叉树度为1的结点求和
int sumSCNodes(struct BinTree *bt) {
    if(bt == NULL) return 0;
    if((bt->left == NULL&&bt->right != NULL)||(bt->left != NULL&&bt->right == NULL)) {
        return bt->data + sumSCNodes(bt->left) + sumSCNodes(bt->right);
    }
    else {
        return sumSCNodes(bt->left) + sumSCNodes(bt->right);
    }
}

17.邻接矩阵存储图的深度优先遍历

//邻接矩阵存储图的深度优先遍历
void DFS(MGraph Graph,Vertex V,void (*Visit)(Vertex)) {
    Visit(V);
    Visited[V] = true;
    for(int j = 0;j < Graph->Nv;j++) {
        if(Graph->G[V][j] == 1&&Visited[j] == false) {
            DFS(Graph,j,Visit);
        }
    }
}

18.邻接表存储图的广度优先遍历

//邻接表存储图的广度优先遍历
void BFS(LGraph Graph,Vertex S,void (*Visit)(Vertex)) {
    int queue[1010];
    int l = 0;
    int r = 0;
    queue[r++] = S;
    Visit(S);
    Visited[S] = true;
    PtrToAdjVNode tmp;
    //如果队列不是空的
    while(l != r) {
        tmp = Graph->G[queue[l++]].FirstEdge;
        while(tmp) {
            Vertex pos = tmp->AdjV;
            if(!Visited[pos]) {
                Visit(pos);
                Visited[pos] = true;
                queue[r++] = pos;
            }
            tmp = tmp->Next;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值