In Order Traverse

 avatar
unknown
java
2 years ago
930 B
1
Indexable
package LAB4;

import java.util.ArrayList;

public class traverseInOrder
{
    // Declare an ArrayList to store the traversal results
    private ArrayList<Integer> results;

    // Constructor for the traverseInOrder class
    traverseInOrder(Node currentNode, ArrayList<Integer> results)
    {
        // Set the results instance variable to the passed-in ArrayList
        this.results = results;

        // If the current node has a left child, recursively traverse the left subtree
        if (currentNode.left != null)
        {
            new traverseInOrder(currentNode.left, results);
        }

        // Add the current node's value to the results ArrayList
        results.add(currentNode.value);

        // If the current node has a right child, recursively traverse the right subtree
        if (currentNode.right != null)
        {
            new traverseInOrder(currentNode.right, results);
        }
    }
}