Use changing variable name

Hello,

I am trying to program a binary tree with some nodes. First, I determine the number of nodes (which is 15 if you have 3 objects). For the nodes, I have made a class "node" with parameters like the level and the parent.

I have some problems to determine the parent. I determine the integer value of the parent (let's say that for node 3 the parent is node 1). Then I want to set for Node_i his parent.

Node_i->setParent(Node_parent);

However, the compiler says that Node_parent is undefined. But how can I set the parent for a node?

Thanks!


Complete code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int numbernodes = (int) pow(2.0,n+1) - 1;

	for(int i = 0; i != numbernodes; i++){
		node* Node_i = new node();
		int parent;
		int level;

		//determine parent
		if(i == 0) parent = 0;
		else parent = (int) (i-0.1)/2;
		Node_i->setParent(Node_parent);
		printf("Parent van %d is %d\n", i, parent);
		
		//determine level
		if(i == 0) level = 0;
		else level = Node_i->getLevel() + 1;
		
		
		
	}
Last edited on
Are the nodes all identified by number?

Node_i->setParent(parent);
Node_0, Node_1...

The reason I want to have a node as a parameter instead of an integer for setParent is to be able to use things like parent->getLevel();

EDIT: I think I have the same problem with my definition for the node:
node* Node_i = new node();

The subscript i is also fixed at i and does not change when I change my i in the for-loop.
Last edited on
Solved, I have made an array so I can use the array-number to identify a parent-node.
Topic archived. No new replies allowed.