Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to convert from binnary search tree to linked list, each number of tree to put in a row to a linked list ? any Idea ?
Posted
Comments
CHill60 13-May-14 16:22pm    
That rather depends on how you have implemented your binary search tree. Post your code
Getoar 13-May-14 17:03pm    
for more details look at this picture http://d2o58evtke57tz.cloudfront.net/wp-content/uploads/LinkedListToBST.png
Matt T Heffron 13-May-14 18:13pm    
This is a breadth-first traversal of the binary tree.
http://en.wikipedia.org/wiki/Tree_traversal#Breadth-first_2
Nelek 13-May-14 18:20pm    
If you get asked for it and you do it, you should post your code into the question using the widget "improve question" and then the needed code language. It will increase the readability and the easier people can read your code, the more probability for you to get help

1 solution

Matt is right. assuming that you have Tree, LinkedList and Queue structures, you can do something like this:

C#
Tree* tree;
LinkedList* list;

Queue* helper;

void convert() {
 Tree* t; // tmp pointer for current tree node
 helper->enqueue(tree);

 // while helper queue has an item
 while(helper->count>0) {
  // get a node from helper queue
  t=helper->dequeue();
  // add it to list
  list->add(t->node);
  // enqueue left branch
  if(t->left!=NULL)
   helper->enqueue(t->left);
  // enqueue right branch
  if(t->right!=NULL)
   helper->enqueue(t->right);
 }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900