C++编程学习数据结构二叉树

// biTree.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>;
using namespace std;

typedef struct bitnode{
	int data;
    struct bitnode *lchild,*rchild;
}*bitree,tree;

bitree *t;

int number=0;

void createbitree();      //建一个树 

void preorder();          //先序遍列该树 

void createbitree(bitree &t,int c){
     bitree temp,pre;
     cout<<"please input the value:"<<endl;
     do {
        cin>>c;
        if (t==0) {
			t=new tree;
            t->lchild=t->rchild=0;
            t->data=c;
         }else{
			 temp=t;
			 while (temp!=0){
				 pre=temp;
				 if (c<(t->data))
					 temp=temp->lchild;
				 else
					 temp=temp->rchild;
			 }
			 if (c<(pre->data)){
				 bitree newdi=new tree;
				 newdi->lchild=newdi->rchild=0;
				 newdi->data=c;
				 pre->lchild=newdi;
			 }else{
				 bitree newdi=new tree;
				 newdi->lchild=newdi->rchild=0;
				 newdi->data=c;
				 pre->rchild=newdi;
			 }
			 ++number;
		}
	 }
	 while (c!=0);
	 cout<<"the bitree has "<<number<<" leave"<<endl;
}

void preorder(bitree &t){
	if (t!=0){
		cout<<t->data<<",";
		preorder(t->lchild);
		preorder(t->rchild);
	}
}

void main(){
	bitree t;
	t=0;
	createbitree(t,0);
	cout<<"the value of the preorder value is:";
	preorder(t);
}

你可能感兴趣的:(C++编程学习数据结构二叉树)