Nov 5, 2001 #1 Sedai Programmer Mar 4, 2001 158 US is there a way to traverse a BST by levels? First visit the root, then all nodes at level ONE, then all nodes at level TWO, etc. thanks! Avendeval
is there a way to traverse a BST by levels? First visit the root, then all nodes at level ONE, then all nodes at level TWO, etc. thanks! Avendeval
Nov 6, 2001 1 #2 Zyrenthian Programmer Mar 30, 2001 1,440 US void printLevel(int x, node* p) { if(!p)return; if(x) { printLevel(x-1,p->Left); printLevel(x-1,p->Right); } else { cout<<p->Data; } } for(int i = 0; i<NumberOfLevels;i++) { printLevel(i,head); } Something like this would do it. A combination of recursion and itteration this code is not tested Matt Upvote 0 Downvote
void printLevel(int x, node* p) { if(!p)return; if(x) { printLevel(x-1,p->Left); printLevel(x-1,p->Right); } else { cout<<p->Data; } } for(int i = 0; i<NumberOfLevels;i++) { printLevel(i,head); } Something like this would do it. A combination of recursion and itteration this code is not tested Matt
Nov 6, 2001 Thread starter #3 Sedai Programmer Mar 4, 2001 158 US thank you! nice!! Avendeval Upvote 0 Downvote