How come n still points to a default node after draw()
?
I'm trying to pass in a pointer to a node to draw()
, and have it point to a newly-created node within draw()
.
#include <iostream>
using namespace std;
struct node{
int key;
struct node *l, *r;
int level, place;
};
int keyCounter = 0;
void draw(struct node *t, int low, int high, int storey){
if(storey == 0) return;
t = new node();
t->key = ++keyCounter;
t->level = storey;
int mid = (low + high) / 2;
t->place = mid;
draw(t->l, low, mid, storey - 1);
draw(t->r, mid, high, storey - 1);
}
void visit(struct node *t){
if(t != NULL){
cout << t->key << ' ' << t->level << ' ' << t->place << '\n';
visit(t->l);
visit(t->r);
}
}
int main(){
struct node *n = new node();
draw(n, 0, 64, 6);
visit(n);
return 0;
}