树与二叉树

//
//  Tree.cpp
//  data structure
//
//  Created by 胡纪钊 on 2021/11/20.
//求两结点的最近公共祖先结点
//二叉树任意两个结点必然存在最近的公共祖先结点


#include "Tree.hpp"
using namespace std;
#define maxsize 50
typedef int elemtype;
typedef struct bitnode{  //树的结构体
    elemtype data;
    struct bitnode *lc,*rc;
}bitnode,*bitree;


void InitBiTree(bitree& T){
    char ch;
    cin>>ch;
    if(ch=='#'){
        T = NULL;
    }else{
        T = (bitnode*)malloc(sizeof(bitnode));
        T->data = ch;
        InitBiTree(T->lc);
        InitBiTree(T->rc);
    }
}

int treeDepth(bitree T){
    if(T==NULL) return 0;
    int l = treeDepth(T->lc);
    int r = treeDepth(T->rc);
    return l>r?l+1:r+1;
}

void treetest(){
    bitree T;
    InitBiTree(T);
 
    cout<<treeDepth(T);
}


void visit(){
    
}

void nlr(bitree T){  //先序遍历
    if(T !=NULL){
        nlr(T);
        visit();
        nlr(T);
        
    }
}
void lnr(bitree T){  //中序遍历
    if(T !=NULL){
        lnr(T);
        visit();
        lnr(T);
        
    }
}

void lrn(bitree T){  //后序遍历
    if(T !=NULL){
        lrn(T);
        visit();
        lrn(T);
        
    }
}
//二叉排序树非递归查找法
bitnode *bst_search(bitnode *T,elemtype x){
    while (T!=NULL && x!=T->data) {
        if (x < T->data) {
            T=T->lc;  //小于,则在左子树上查找
        }
        else
            T=T->rc;  //大于,则在右子树上查找
    }
    return T;
}


//
//  Tree.cpp
//  data structure
//
//  Created by 胡纪钊 on 2021/11/20.
#include <iostream>

using namespace std;

struct ctnode{
    int child;//孩子节点在数组中的位置
    struct ctnode *next;//下一个孩子
};

typedef struct{
    elemtype data;
    struct ctnode*firstchild;//第一个孩子
    
}ctbox;

typedef struct{
    ctbox node[maxsize];
    int n,r;
}ctree;

//孩子兄弟表示法
typedef struct csnode{
    elemtype data;
    struct csnode *firstchild,*nextsibling;
    
}csnode,*cstree;



int main()
{
   cout << "Hello World" << endl; 
   
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值