看了很多二叉排序树的例子,大多数都是用数字构建,我试着写了一个用字符串构建二叉排序树......
如果有哪里不对或者是有更好的写法,欢迎各位大佬批评指正~
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
typedef struct Node
{
string data;
struct Node *lchild,*rchild;
} Node,*BiTree;
BiTree Build(BiTree T,string x)
{
if(T == NULL)
{
T = new Node();
T -> data = x;
T->lchild=NULL;
T->rchild=NULL;
return T;
}
else
{
if(x.compare(T -> data)<0)
{
T -> lchild= Build(T -> lchild,x);
}
else if(x .compare( T -> data)>0)
{
T -> rchild=Build(T -> rchild,x);
}