今天学到了二叉树,一开始被那个malloc弄的很迷,后来发现root=(BiTreeNode*)malloc(sizeof(BiTreeNode))的那个星号是在后面的,吐血。。
代码里面有个小技巧,就是typedef struct XXX{...}XXX,这样就使用XXX代替了struct XXX,可以少打一些字了233.
#includeusing namespace std;typedef struct BiTreeNode { int data; BiTreeNode* left; BiTreeNode* right; void operator =(BiTreeNode* b) { data=b->data; left=b->left; right=b->right; };} BiTreeNode;BiTreeNode *root;void Create(BiTreeNode* root,int data) { //add a node to the tree BiTreeNode* tot; BiTreeNode* Father; BiTreeNode* current; tot=(BiTreeNode*)malloc(sizeof(BiTreeNode));//the new point tot->data=data; tot->left=NULL; tot->right=NULL; Father=current=root; while (current!=NULL) { //find the leaf if (current->data right; } else { Father=current; current=current->left; } } current=Father; if (current->data right=tot; } else { current->left=tot; }}int main(){ root=(BiTreeNode*)malloc(sizeof(BiTreeNode)); root->data=10; root->left=NULL; root->right=NULL; Create(root,25); Create(root,5); Create(root,30); Create(root,12408); Create(root,233); cout<<233; return 0;}