二叉树

二叉树的输入/先序/中序/后序/层序

#include <iostream>
#include <stdlib.h>
#include <stack>
#include <queue>

using namespace std;

struct BT
{
    int data;
    BT *left;
    BT *right;
};

BT *createtree(){
    //先序的输入顺序
    int ch;
    BT *p;
    cout<<"ch=";
    cin>>ch;
    if(ch==-1) p=NULL;
    else
    {  p=(BT*)malloc(sizeof(BT));//BT *p=new BT;???
        p->data=ch;
        p->left=createtree();
        p->right=createtree();
    }
    return p;
}

//递归
void pre0(BT* root){
    if(root!=NULL) {
        cout << root->data << " ";
        pre0(root->left);
        pre0(root->right);
    }
}

void in0(BT* root){
    if(root!=NULL){
        in0(root->left);
        cout<<root->data<<" ";
        in0(root->right);
    }
}

void post0(BT* root){
    if(root!=NULL){
        post0(root->left);
        post0(root->right);
        cout<<root->data<<" ";
    }
}

//非递归
void pre(BT *root){
    stack<BT*> st;
    BT* p = root;
    while(p!=NULL||st.empty()!=1)
        if(p!=NULL){
            //向左走
            //用栈记录路径
            cout<<p->data<<" ";
            st.push(p);
            p=p->left;
        }

        else{
            //座支走到尽头时,若栈里还有结点,则退栈进入右分支
            //p->data=st.top();
            p = st.top();
            st.pop();
            p=p->right;
            //cout<<n<<setw(5);
        }
        cout<<endl;
}

void in(BT *root)
{
    stack<BT*> s;
    BT* p = root;
    while(p!=NULL||!s.empty())
    {
        if(p!=NULL)
        {
            s.push(p);
            p = p->left;
        }
        else
        {
            p=s.top();
            s.pop();
            cout<<p->data<<" ";
            p=p->right;
        }
    }
    cout<<endl;
}

void post(BT*root)
{
    BT *p = root, *r = NULL;
    stack<BT*> s;
    while (p!=NULL || !s.empty()) {
        if (p!=NULL) {//走到最左边
            s.push(p);
            p = p->left;
        }
        else {
            p = s.top();
            if (p->right!=NULL && p->right != r)//右子树存在,未被访问
                p = p->right;
            else {
                s.pop();
                cout<<p->data<<" ";
                r = p;//记录最近访问过的节点
                p = NULL;//节点访问完后,重置p指针
            }
        }//else
    }//while
    cout<<endl;
}

void cengci(BT*root)
{   //与其他遍历均不同,层序遍历使用的是队列,而非栈
    //使用栈的函数一般都可以改为递归函数
    queue<BT*>q;
    BT* p = root;
    q.push(p);
    while(!q.empty())
    {
        p=q.front();
        q.pop();
        if(p!=NULL)
        {
            cout<<p->data<<" ";
            q.push(p->left);
            q.push(p->right);
        }
    }
    cout<<endl;
}

int main()
{BT *s;
    s=createtree();
    cout<<"前序遍历递归"<<endl;
    pre0(s);
    cout<<endl;
    cout<<"前序遍历非递归"<<endl;
    pre(s);
    cout<<"中序遍历递归"<<endl;
    in0(s);
    cout<<endl;
    cout<<"中序遍历非递归"<<endl;
    in(s);
    cout<<"后序遍历递归"<<endl;
    post0(s);
    cout<<endl;
    cout<<"后序遍历非递归"<<endl;
    post(s);
    cout<<"层序遍历"<<endl;
    cengci(s);

    return 0;
}

字符串

#include<iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class student{
    int _id;

    int _age;
public:
    char _name[20];
    student(){}
    student(char* a, int i,int age){
        strcpy(_name, a);
        _id = i;
        _age = age;
    }

    void printInf(){
        cout<<"name: "<<_name<<" id: "<<_id<<" age: "<<_age<<endl;
    }
};


int main(){

    student* s = new student("jack", 10, 18);
    student* s2 = new student("tom", 12, 19);
    if(strcmp(s->_name, s2->_name)) s->printInf();
    else s->printInf();
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值