二叉树的应用

// 计算二叉树所有结点数
int n = 0;
void count(BTNode *p){
    if(p!=NULL){
        ++n;
        count(p->lchild);
        count(p->rchild);
    }
}


// 计算叶结点数
int n = 0;
void count_0(BTNode *p){
	if(p!=NULL){
        if(p->lchild=NULL&&p->rchild=NULL)
            ++n;
        count_0(p->lchild);
        count_0(p->rchild);
    }
}

//
// 利用右孩子指针连接叶结点
// 采用先序遍历模板
void link(NTNode *p, BTNode *&head, BTNode *&tail){
    if(p!=NULL){
        if(p->lchild!=NULL&&p->rchild!=NULL){
            if(head==NULL){               // 如果head=NULL,则当前结点为第一个
                head=p;
                tail=p;
            }
            else{
                tail->rchild=p;
                tail=p;
            }
        }
        link(p->lchild,head,tail);
        link(p->rchild,head,tail);
    }
}



// 输出结点到根结点的路径
// 增加一个指向双亲的指针parent
// 定义新的数据结构
typedef struct BTNode{
    char data;
    BTNode *lchild;
    BTNode *rchild;
    BTNode *parent;
}

// 给parent赋值
void triBtree(BTNode *p, BTNode *q){        //q指向双亲
    if(p!=NULL){
        p->parent = q;
        q = p;       // p是其子树的双亲
        triBtree(lchild,q);
        triBtree(rchild,q);
    }   
}

// 打印路径
void printPath(BTNode *p){
	while(*p!=NULL){
        cout<<p->data<<'\n';
        printPath(p-parent);
    }
}

// 打印所有结点的路径, 先序遍历
void allPath(BTNode *p){
	if(p!=NULL){
        printPath(p);
        allPath(p->lchild);
        allPath(p->rchild);
    }
}

///        change(pre, L1+1, (L1+1+R1)/2, post, L2,(L2+R2-1)/2);
// 已知满二叉树的先序序列, 求后序序列
// 已知pre数组,即先序序列
void change(char pre[], int L1, int R1, char post[], int L2, int R2){
    if(L1<=R1){
        post[R2] = pre[L1];        change(pre, L1+1, (L1+1+R1)/2, post, L2,(L2+R2-1)/2);
        change(pre, L1+1, (L1+1+R1)/2, post, L2,(L2+R2-1)/2);
        change(pre, L1+1, (L1+1+R1)/2, post, L2,(L2+R2-1)/2);
    }
}

/
// 找出值为b的结点和层数
// 运用重要提示模板
int L=1;
void leno(BTNode *p){
	if(p!=0){
        if(p->data==b)
            cout<<L<<endl;
        ++L;
        
        leno(p->lchild);
        leno(p->rchild);
        --L;
    }
}
//

// 在中序线索二叉树中,寻找结点t为根的子树上的最后一个结点
// 思路: 沿右子树一直走下去,直到右指针为右线索
TBTNode* inLast(TBTNode *t){
	TBTNode *p = t;
    while(p && !p->rtag){
        p = p->rchild;
    }
    return p;
}

// 找中序前驱
TBTNode* inPrior(TBTNode *t){
    TBTNode *p=t->lchild;
    if(p && !t->ltag){       // 如果p不为空,且ltag==0
        p=inLast(p);
    }
    retunr p;
}

// 找前序后继(注意,仍然是中序线索二叉树)
// 思路列举所有情况
TBTNode* treNext(TBTNode *t){
    TBTNode *p;
    if(!t->ltag)
        p = t-lchild;
    else if(!tag->rtag)
        p = t-rchild;
    else{
        p = t;
        while(p && p-rtag)      // 如果p不为空,且rtag==1
            p=p->rchild;
        if(p)
            p=p->rchild;
    }
    return p;
    
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个二叉树应用实例代码的C语言实现,用于创建一棵二叉树并进行先序、中序和后序遍历: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点结构体 typedef struct TreeNode { int data; // 节点数据 struct TreeNode *left; // 左子树 struct TreeNode *right; // 右子树 } TreeNode; // 创建一棵二叉树 TreeNode *createBinaryTree() { TreeNode *root = NULL; int data; printf("请输入节点数据:"); scanf("%d", &data); if (data != -1) { // -1代表空节点 root = (TreeNode *)malloc(sizeof(TreeNode)); root->data = data; root->left = createBinaryTree(); root->right = createBinaryTree(); } return root; } // 先序遍历二叉树 void preOrder(TreeNode *root) { if (root) { printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } } // 中序遍历二叉树 void inOrder(TreeNode *root) { if (root) { inOrder(root->left); printf("%d ", root->data); inOrder(root->right); } } // 后序遍历二叉树 void postOrder(TreeNode *root) { if (root) { postOrder(root->left); postOrder(root->right); printf("%d ", root->data); } } int main() { TreeNode *root = createBinaryTree(); printf("先序遍历结果:"); preOrder(root); printf("\n中序遍历结果:"); inOrder(root); printf("\n后序遍历结果:"); postOrder(root); printf("\n"); return 0; } ``` 在运行程序时,先输入每个节点的数据,以-1表示空节点。例如,输入以下数据表示创建一棵二叉树: ``` 请输入节点数据:1 请输入节点数据:2 请输入节点数据: -1 请输入节点数据: -1 请输入节点数据: 3 请输入节点数据: -1 请输入节点数据: -1 ``` 程序会输出先序、中序和后序遍历的结果: ``` 先序遍历结果:1 2 3 中序遍历结果:2 1 3 后序遍历结果:2 3 1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值