标签:blog http ar for sp 2014 log bs new
直接上干货:
#include "targetver.h"
using namespace std;
//定义节点
struct BiNode
{
int data;
BiNode * lchild;
BiNode * rchild;
};
//插入结点
BiNode * InsertBST(BiNode * root,int data)
{
if(root==NULL)
{
root=new BiNode;
root->data=data;
root->lchild =root->rchild =NULL;
}
if(root->data >data)
root->lchild = InsertBST(root->lchild ,data);
if(root->data <data)
root->rchild = InsertBST(root->rchild ,data);
return root;
}
//创建二叉树
BiNode * CreateBST(BiNode * root,int data[],int n)
{
int i;
for(i=0;i<n;i++)
root = InsertBST(root,data[i]);
return root;
}
//先序搜索二叉树
void PrintBST(BiNode * root)
{
if(root!=NULL)
{
cout<<root->data<<"->" ;
PrintBST(root->lchild );
PrintBST(root->rchild );
}
}
int main(int argc, char* *argv)
{
BiNode * root=NULL;
int data[10]={5,9,4,7,3,6,1,8,2,10};
root=CreateBST(root,data,10);
PrintBST(root);
cout<<endl;
return 0;
}
标签:blog http ar for sp 2014 log bs new
原文地址:http://blog.csdn.net/feeltouch/article/details/40868301