trees i assess

PHOTO EMBED

Sat May 04 2024 06:14:48 GMT+0000 (Coordinated Universal Time)

Saved by @cms

2------------------------------------------------------------------------------------
//BinarySearchTree.java
public class BinarySearchTree {
    public Node insertNode(Node root, int data) {
        if (root == null) {
            return new Node(data);
        }

        if (data < root.data) {
            root.left = insertNode(root.left, data);
        } else if (data > root.data) {
            root.right = insertNode(root.right, data);
        }

        return root;
    }

    public void display(Node root) {
        if (root != null) {
            display(root.left);
            System.out.print(root.data + " ");
            display(root.right);
        }
    }

    public int printAncestor(Node root, int element) {
        System.out.println("Ancestors of "+element+": ");
        return printAncestorHelper(root, element);
    }

    private int printAncestorHelper(Node root, int element) {
        if (root == null) {
            return 0;
        }

        if (root.data == element) {
            return 1;
        }

        if (printAncestorHelper(root.left, element) == 1 || printAncestorHelper(root.right, element) == 1) {
            System.out.print(root.data + " ");
            return 1;
        }

        return 0;
    }
}
// Main.java
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BinarySearchTree bst = new BinarySearchTree();
        Node root = null;
        int choice;

        do {
            System.out.println("Binary Search Tree Implementation");
            System.out.println("1. Insert");
            System.out.println("2. Print ancestor");
            System.out.println("3. Display");
            System.out.println("4. Exit");
            System.out.println("Enter your choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("Enter the element to insert: ");
                    int elementToInsert = scanner.nextInt();
                    root = bst.insertNode(root, elementToInsert);
                    System.out.println("Element inserted successfully.");
                    break;
                case 2:
                    System.out.println("Enter the element to find its ancestor: ");
                    int elementToFindAncestor = scanner.nextInt();
                    bst.printAncestor(root, elementToFindAncestor);
                    System.out.println();
                    break;
                case 3:
                    if(root==null)
                        System.out.println("There are no elements in the BST. ");
                    else{
                        System.out.print("The elements are :");
                    bst.display(root);
                    }
                    break;
                case 4:
                    System.out.println("Exiting the program.");
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
                    break;
            }
        } while (choice != 4);
    }
}

1-----------------------------------------------------------------------------------------------------------------------------------------------
public class BinaryTree {
 public void insert(TNode[] s, int num) {
        TNode newNode = new TNode(num);

        if (s[0] == null) {
            s[0] = newNode;
            return;
        }
        TNode root=s[0];
        while(root!=null){
            if(num > root.data){
                if(root.right==null){
                    root.right=newNode;
                    break;
                }
                root=root.right;
            }
            else{
                if(root.left==null){
                    root.left=newNode;
                    break;
                }
                root=root.left;
            }

        }
    }


    public void inorder(TNode s) {
        if (s != null) {
            inorder(s.left);
            System.out.print(s.data + " ");
            inorder(s.right);
        }
    }

    public int diameter(TNode tree) {
        if (tree == null) {
            return 0;
        }

        int leftHeight = height(tree.left);
        int rightHeight = height(tree.right);

        int leftDiameter = diameter(tree.left);
        int rightDiameter = diameter(tree.right);

        return max(leftHeight + rightHeight + 1, max(leftDiameter, rightDiameter));
    }

    public int height(TNode node) {
        if (node == null) {
            return 0;
        } else {
            int leftHeight = height(node.left);
            int rightHeight = height(node.right);

            return 1 + max(leftHeight, rightHeight);
        }
    }

    public int max(int a, int b) {
        return a > b ? a : b;
    }
}
content_copyCOPY