0

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 );
            }
        }
    }
elf01b
  • 64
  • 5
  • 2
    Please define “does not work”. – michaeldel Jun 08 '20 at 03:27
  • Where did you define `arrayString`? – trincot Jun 08 '20 at 07:18
  • Hi, welcome to SE. Please improve your question by providing a [minimal, complete, verifiable exemple](https://stackoverflow.com/help/minimal-reproducible-example) that contains input to reproduce and error output related to the problem. – PTRK Jun 08 '20 at 08:10

0 Answers0