直接上干貨:
#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 rchild = InsertBST(root->rchild ,data); return root; } //創建二叉樹 BiNode * CreateBST(BiNode * root,int data[],int n) { int i; for(i=0;idata<<"->" ; 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<
結果(VS2010):