问题
How to traverse a B-Tree using Stack without recursion?
This is function to traverse Btree using Stack but it does not work
void StackTraverse( BTreeNode node01 ) {
Stack< BTreeNode > stack01 = new Stack< BTreeNode >();
stack01.push( node01 ); // first node "to check"
String string01 = "";
int i = 0;
while ( stack01.size() > 0 ) {
BTreeNode current = stack01.pop();
if ( current.leaf == false ) {// if node is valid
for ( i = 0; i < current.n; i++ ) {
string01 = string01 + "Node : " + current.keys[ i ] + " ";
// string01 = string01 + current.traverse();
stack01.push( current.nodeChild[ i ] );
}
arrayString.add( string01 );
string01 = "";
}
}
}
void StackTraverse() {
String s01 = "";
if ( root != null ) {
StackTraverse( root );
System.out
.println( "\n arrayString.size() = " + arrayString.size() );
for ( int i = 0; i < arrayString.size(); i++ ) {
s01 = arrayString.get( i );
System.out.println( s01 );
}
}
}
来源:https://stackoverflow.com/questions/62254455/how-to-traverse-a-b-tree-using-stack-without-recursion