4 Replies - 676 Views - Last Post: 22 January 2013 - 08:29 PM Rate Topic: -----

#1 asdk77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-November 12

How to search a tree(postorder) in C

Posted 22 January 2013 - 02:07 PM

I write a function to search a tree, and find out the node suitable and store them in an array. It is in postorder.
void search_for_node(node *root, node *list[])
{
	int j;
	if (root == NULL)
		return;
	
	
	for(j=0; j<root->num_children; j++)
		search_for_node(root->children[j], list);
	if (root->type == ID)
	list[num_for++] = root;

	
	
}



But it can't work. Is there anything wrong in my code?
Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: How to search a tree(postorder) in C

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: How to search a tree(postorder) in C

Posted 22 January 2013 - 02:41 PM

Why doesn't it work?

Also posted here.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 337
  • View blog
  • Posts: 728
  • Joined: 27-June 09

Re: How to search a tree(postorder) in C

Posted 22 January 2013 - 03:31 PM

What is "num_for" and "ID"?

This post has been edited by mojo666: 22 January 2013 - 03:32 PM

Was This Post Helpful? 0
  • +
  • -

#4 asdk77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-November 12

Re: How to search a tree(postorder) in C

Posted 22 January 2013 - 07:15 PM

View Postmojo666, on 22 January 2013 - 03:31 PM, said:

What is "num_for" and "ID"?

num_for is a global variable counting the number of nodes suitable.
ID is a enum type, and the node structure has a member called type, some of them equals ID.
I think the logic of the function is correct, but when I run the whole program, it won't search the leaf, but just search the children of top.
Now, I am confused.
Was This Post Helpful? 0
  • +
  • -

#5 asdk77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-November 12

Re: How to search a tree(postorder) in C

Posted 22 January 2013 - 08:29 PM

It can work!
Thank you for attention!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1