Sunday, August 6, 2023

Hash table Binary search tree stack java program

 import java.util.Hashtable;

public class javaHashtable { public static void main(String[] args) { //1. Create Hashtable Hashtable<Integer, String> myHT = new Hashtable<>(); //2. Add mappings to hashtable myHT.put(1, "Ahmad"); myHT.put(2, "Bilal" ); myHT.put(3, "Omer"); //3. Remove mapping for key 3 myHT.remove(3); //3 is deleted //4. Add mapping: 2, Abdullah myHT.put(2, "Abdullah" ); System.out.println(myHT); } } Output: {2=Abdullah, 1=Ahmad}

===========================================

Binary search tree and performs in-order, pre-order, and post-order traversals on the tree. import java.util.Scanner; class Node { int data; Node left, right; Node(int k) { data = k; left = right = null; } } class BST { Node root; BST() { root = null; } void insert(int key) { root = insertRec(root, key); } Node insertRec(Node root, int key) { if (root == null) { // insert in empty tree return new Node(key); } if (key < root.data) { // recur down the left subtree root.left = insertRec(root.left, key); } else { // recur down the right subtree root.right = insertRec(root.right, key); } return root; } void inorder() { inorderRec(root); } void inorderRec(Node root) { if (root != null) { // inorder traversal inorderRec(root.left); System.out.println(root.data); inorderRec(root.right); } } void preorder() { preorderRec(root); } void preorderRec(Node root) { if (root != null) { // preorder traversal System.out.println(root.data); preorderRec(root.left); preorderRec(root.right); } } void postorder() { postorderRec(root); } void postorderRec(Node root) { if (root != null) { // postorder traversal postorderRec(root.left); postorderRec(root.right); System.out.println(root.data); } } public static void main(String[] args) { BST bst = new BST(); bst.insert(50); bst.insert(30); bst.insert(70); bst.insert(20); bst.insert(40); bst.insert(60); bst.insert(80); System.out.println("In-order traversal: "); bst.inorder(); System.out.println("Pre-order traversal: "); bst.preorder(); System.out.println("Post-order traversal: "); bst.postorder(); } }

=========================
//stack push pop import java.util.LinkedList; public class Stack { private LinkedList<String> list = new LinkedList<>(); // This method should push an element onto the stack. public void push(String Planets) { // Add the element to the front of the list. list.addFirst(Planets); } // This method should pops the top element off the stack. public String pop() { // Remove the element from the front of the list. return list.removeFirst(); } // This method should prints the elements in the stack. public void print() { // Iterate through the list and print each element. for (String Planets : list) { System.out.println(Planets); } } // This is the main method of the program. public static void main(String[] args) { // Create a new stack. Stack stack = new Stack(); // Push three Planetss onto the stack Apple, Banana, orange stack.push("Earth"); stack.push("Jupiter"); stack.push("Mars"); // Print the stack. stack.print(); // Pop the top element off the stack. System.out.println("The top element is: " + stack.pop()); // Print the stack again. stack.print(); } }

No comments:

virtual representations of physical objects or systems.

Digital Twins - Virtual Replicas of Cities, Factories, or Human Organs for Simulations How virtual copies are revolutionizing the phys...