typedef struct _Tree{
int val;
struct _Tree *left;
struct _Tree *right;
}Tree;
这里Tree
有指针吗?它是否指向 val 的地址?
如果我定义这个怎么样Tree *node
?是node
指针指向的地址Tree
吗?
如果我们想插入val
,我们应该使用insert(&node)
orinsert(node)
吗?
void insert_Tree(Tree **root, int key){
if((*root) == NULL){
(*root) = (Tree *)malloc(sizeof(Tree));
(*root)->val = key;
(*root)->left = NULL;
(*root)->right = NULL;
cout<<"insert data "<<key<<endl;
}else if(key< (*root)->val){
insert_Tree(&(*root)->left, key);
cout<<"go left"<<endl;
}else{
insert_Tree(&(*root)->right, key);
cout<<"go right"<<endl;
}
}
int main(){
Tree *root = NULL;
insert_Tree(&root, 10);
insert_Tree(&root, 20);
insert_Tree(&root, 5);
insert_Tree(&root, 100);
}