简体   繁体   中英

Traversal in Tree Data Structure (IN-Order) not working in java

I am new to Data Structure and currently I am writing a Tree Data Structure traversing method. Right now, when I perform in-order traversal, it should be moving from smaller to largest value. But it seems that my method is not following the sequence. May I know what is the issue here? Here is the code snippet:

    int[] intArr = { 100, 20 ,140, 55, 19, 22, 89, 200, 120 };

    for( int i = 0; i < intArr.length; i++ )
    {
        key += intArr[i];
        tree.insert(key, intArr[i]); // Key is the same as data in this case
        key = "";
    }

    String str = tree.inOrderTraverse();
    System.out.println(str);

The output supposed to look like this: 19 20 22 55 89 100 120 140 200 But this is the actual output: 100 120 140 19 20 200 22 55 89

Here is my recursive method and wrapper method:

// SUBMODULE: inOrderTraverse (Wrapper Method)
// IMPORT: none
// EXPORT: (String)
public String inOrderTraverse()
{
    return inOrderRec( root, "" );
}

// SUBMODULE: inOrderRec
// IMPORT: currNd (DSATreeNode), str (String)
// EXPORT: str (String)
private String inOrderRec( DSATreeNode currNd, String str )
{
    if( currNd != null )
    {
        str = inOrderRec( currNd.left, str );
        str += currNd.data + " ";
        str = inOrderRec( currNd.right, str );
    }
    return str;
}

This is my insert and its wrappers

// SUBMODULE: insert (Wrapper)
// IMPORT: key (String), data (Object)
// EXPORT: none
public void insert( String key, Object data )
{
    try { root = insertRec( root, key, data ); } // Now root has the address of all the trees 
    catch(IllegalArgumentException e) { System.out.println(e.getMessage()); }
} 

// SUBMODULE: insertRec
// IMPORT: currNd (DSATreeNode), key (String), data (Object)
// EXPORT: updateNd (DSATreeNode)
// ASSERTION: Create the new tree node 
private DSATreeNode insertRec( DSATreeNode currNd, String key, Object data )
{
    DSATreeNode updateNd;
    updateNd = currNd;
    if( currNd == null )
    {
        currNd = new DSATreeNode( key, data );
        updateNd = currNd;
    }
    else if( key.equals(currNd.key) )
        throw new IllegalArgumentException( "Existing key provided" );      // Key must be unique

    else if( key.compareTo(currNd.key) < 0 )
        currNd.left = insertRec( currNd.left, key, data );                  // Recursive to left

    else
        currNd.right = insertRec( currNd.right, key, data );                // Recursive to right

    return updateNd;
}

Your keys are actually strings. Therefore you will get the lexicographical order of the keys you have inserted. That is why the output is what you see. 100 120 140 19 20 200 22 55 89 is the correct lexicographical order!

Don't use string as datatypes for keys. Use integer if your elements are integers.

Or try to convert the keys to integers from strings before comparing like: Integer.valueOf(key).compareTo(Integer.valueOf(currNd.key)) in your insert method.

But this is extra work and is prone to exceptions unless you are sure that keys represent integers always.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM