二叉树的算法操作笔记

typedef struct BiNode{
    char ch;
    struct BiNode*left;
    struct BiNode*right;
}*BiTree;
/*
    先序建立一颗二叉树   
*/
void CreateBiTree(BiTree&T){
    char ch;
    scanf("%c", &ch);
    if (ch == Nil){
        T = NULL;
    }
    else{
        T = (BiTree)malloc(sizeof(struct BTNode));
        T->ch = ch;
        CreateBiTree(T->left);
        CreateBiTree(T->right);

/*
    表达式求值
    先求左子树表达式的值,再求右子树表达式的值,最后左右子树进行运算
    param:
        a:左子树的值
        b:右子树的值
        c:根节点的符号
    return:运算的结果
*/
int op(int a,int b,char c){
    switch(c){
    case '+':return a+b;break;
    case '-':return a-b;break;
    case '*':return a*b;break;
    case '/':return a/b;break;
    }
}
int comp(BiTree&T){
    int a,b;
    if(T){
        if(T->left&&T->right){
            a=comp(T->left);//递归计算左子树的值
            b=comp(T->right);//递归计算右子树的值
            return op(a,b,T->ch);两颗子树最后进行运算
        }else return T->ch-'0';
    }else return 0;
}
/*
    求二叉树的深度
*/
int depth(BiTree T){
    int i=0,j=0;
    if(!T) return 0;//如果树为空返回0
    i=depth(T->left);//递归计算左子树的深度
    j=depth(T->right);//递归计算右子树的深度
    return i>j?i+1:j+1;
}
/*
    普通的非二叉搜索树的搜索
*/
void search(BiTree&T,BiTree&q,char key){
    if(T){
        if(T->ch==key) {
                q=T;
            return;
        }else{
            search(T->left,q,key);
            search(T->right,q,key);
        }
    }
}
/*
    利用节点的右子树指针right将一颗二叉树的叶子节点按照从左往右的顺序串成一个单链表,head指向第一个叶子节点,tail指向最后一个叶子节点(递归版本)
*/
void make_leaftoLinkList_notRecursion(BiTree T,BiTree&head,BiTree&tail){
    if(T){
        if((T->left==NULL)&&(T->right==NULL)){
            if(head==NULL){
                head=T;
                tail=T;
            }else{
                tail->right=T;
                tail=T;
            }
        }else{
            make_leaftoLinkList(T->left,head,tail);
            make_leaftoLinkList(T->right,head,tail);
        }
    }
}
void make_leafToLinkList(BiTree&T, BiTree&head, BiTree&tail){
    SqStack S;
    init_stack(S);
    BiTree p = NULL;
    while (T || !stack_empty(S)){
        if (T){
            Push(S, T);
            if ((T->left == NULL) && (T->right == NULL)){//说明T是叶子节点
                if (head == NULL){
                    head = T;
                    tail = T;
                    p = T;
                }
                else{
                    tail->right = T;
                    tail = T;
                }

            }
            T = T->left;
        }
        else{
            Pop(S, T);
            T = T->right;
        }
    }
}
/*
    输出根节点到每个叶子节点的路径
*/
void print_root_to_leaf(BiTree T,int path[],int top){
    path[top++]=T->ch;
    if(T){
        if ((T->left == NULL) && (T->right == NULL)){
            printroot_path(path, top);
        }
        else{
            if (T->left)print_root_to_leaf(T->left, path, top);
            if (T->right) print_root_to_leaf(T->right, path, top);
        }   
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值