How to traverse a B-Tree using Stack without recursion?

限于喜欢 提交于 2021-02-11 17:41:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!