Untitled

 avatar
unknown
plain_text
5 months ago
1.1 kB
2
Indexable
package org.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {


        BST tree = new BST(5);

        // Insert elements
        tree.insert(Arrays.asList(5, 15, 3, 7, 13, 18));

        // Search for an element
        System.out.println("Depth of 7: " + tree.search(7, tree, 0)); // Output: Depth of 7: 2

        // Print leaf nodes
        System.out.print("Leaf nodes: ");
        tree.printLeafNodes(tree); // Output: Leaf nodes: 3 7 13 18

        // Remove an element
        tree.removeElement(15, tree);
        System.out.print("\nLeaf nodes after removing 15: ");
        tree.printLeafNodes(tree); // Output: Leaf nodes: 3 7 13 18

        List<Integer> sortedList = new ArrayList<>();
        Collections.addAll(sortedList, 1, 2, 3, 4, 5, 6, 7);

        CBST CBSTtree = CBST.generateCBST(sortedList, 0, sortedList.size() - 1);

        System.out.println("Leaf nodes in the tree:");
        CBSTtree.printLeafs(CBSTtree);
    }
}
Editor is loading...
Leave a Comment