二叉树的C++、Python实现及遍历

// 二叉树的先序中序后序实现

#include <iostream>
using namespace std;

template<class Elem>
struct BinNode{
    Elem data;
    BinNode<Elem>* left;
    BinNode<Elem>* right;
    BinNode(Elem x){
        data=x;
        left=right=NULL;
    }
};

template<class Elem>
class BinTree{
protected:
    BinNode<Elem>* root;
    void rpreprint(BinNode<Elem>* r){   // 对于传指针,一定不要掉以轻心,要首先看一下指针是不是空指针
        if(r==NULL) return;
        cout<<r->data<<" ";
        rpreprint(r->left);
        rpreprint(r->right);
    }
    void rinprint(BinNode<Elem>* r){
        if(r==NULL) return;
        rinprint(r->left);
        cout<<r->data<<" ";
        rinprint(r->right);
    }
    void rpostprint(BinNode<Elem>* r){
        if(r==NULL) return;
        rinprint(r->left);
        rinprint(r->right);
        cout<<r->data<<" ";
    }
    int cntLeaves(BinNode<Elem>* r){
        if(r==NULL) return 0;
        if(r->left==NULL && r->right==NULL) return 1;
        return cntLeaves(r->left)+cntLeaves(r->right);
    }
    BinNode<Elem>* rfindX(Elem x,BinNode<Elem>* r){
        if(!r)   return NULL;
        if(r->data==x)   return r;
        BinNode<Elem>* found;
        found=rfindX(x,r->left);
        return found?found:rfindX(x,r->right);
    }
    void rprint(BinNode<Elem>* r,int depth){
        for(int i=0;i<depth;i++)    cout<<"  ";
        if(!r){
            cout<<"[/]"<<endl;
        }
        else{
            cout<<r->data<<endl;
        rprint(r->left,depth+1);
        rprint(r->right,depth+1);
        }
    }

public:
    BinTree()   {root=NULL;}
    BinTree(Elem r){
        root=new BinNode<Elem>(r);
    }
    ~BinTree(){ };
    void preprint(){
        rpreprint(root);
        cout<<endl;
    }
    void inprint(){
        rinprint(root);
    }
    void postprint(){
        rpostprint(root);
    }
    void print(){
        rprint(root,0);
    }
    BinNode<Elem>* findX(Elem x){
        return rfindX(x,root);
    }
    bool insert(Elem p,int LorR,Elem x){
        BinNode<Elem>* found;
        found=findX(p);
        if(!found)  return false;
        if(LorR==0){
            if(found->left) return false;
            found->left=new BinNode<Elem>(x);
        }
        else{
            if(found->right)    return false;
            found->right=new BinNode<Elem>(x);
        }
        return true;
    }
    int cnt(){
        return cntLeaves(root);
    }
};

int main(){
    string name;
    cin>>name;
    BinTree<string> bt(name);
    cin>>name;
    while(name!="-"){
        string  lc,rc;
        cin>>lc>>rc;
        if(lc!="-") bt.insert(name,0,lc);
        if(rc!="-") bt.insert(name,1,rc);
        cin>>name;
    }
    cout<<"叶子数目:"<<bt.cnt()<<endl;
    return 0;
}
/*
Ann
Ann Bill Chris
Bill Daisy Ellen
Chric - Flin
Dasiy - -
Ellen Grace Henry
Flin - -
Grace - -
Henry - -
-
*/

class BinNode:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

class BinTree:
    def __init__(self, data):
        self.root = BinNode(data)
    def preprint(self, r):
        if r is None:
            return
        print(r.data, end=' ')
        self.preprint(r.left)
        self.preprint(r.right)
    def findX(self, r, x):
        if r is None:
            return
        if r.data==x:
            return r
        found = self.findX(r.left,  x)
        if found is not None:
            return found
        return self.findX(r.right, r)
    def insert(self, r, p, lorr, x):
        found = self.findX(r, p)
        if found is None:
            return
        if lorr == 0:
            if found.left is not None:
                return
            found.left = BinNode(x)
        else:
            if found.right is not None:
                return
            found.right = BinNode(x)

bt = BinTree(11)
bt.insert(bt.root, 11, 0, 22)
bt.insert(bt.root, 11, 1, 33)
bt.insert(bt.root, 22, 1, 44)
bt.insert(bt.root, 22, 0, 55)
bt.insert(bt.root, 33, 0, 66)
bt.preprint(bt.root)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吉大秦少游

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值