From 46d40408bcc071d45de9de7a8bbadc28547a6e97 Mon Sep 17 00:00:00 2001 From: hackprot2 <91518287+hackprot2@users.noreply.github.com> Date: Sat, 2 Oct 2021 23:51:27 +0530 Subject: [PATCH] Fast tree search and retrieval in Java --- Java/Data-Structures/Array/fast_tree_search | 168 ++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 Java/Data-Structures/Array/fast_tree_search diff --git a/Java/Data-Structures/Array/fast_tree_search b/Java/Data-Structures/Array/fast_tree_search new file mode 100644 index 00000000..fed4f550 --- /dev/null +++ b/Java/Data-Structures/Array/fast_tree_search @@ -0,0 +1,168 @@ +// binary search tree creation, new node insertion , and node deletion +import java.util.*; + +class bstree{ + //node structure + nodes headp; + static class nodes{ + nodes left; + int value; + nodes right; + } + //bst creation + void bstreec(){ + int size; + nodes duph=headp; + Scanner in= new Scanner(System.in); + System.out.println("enter the no of nodes you want to insert"); + int no=in.nextInt(); //no of nodes asked + for(int i=0;iduph.value && duph.right != null){ + duph=duph.right; + } + else if( value duph.value && duph.right != null){ + duph=duph.right; + } + else if( value value) { + root.left = deleteNode(root.left, value); + } else if (root.value < value) { + root.right = deleteNode(root.right, value); + + } else { + // if nodeToBeDeleted have both children + if (root.left != null && root.right != null) { + nodes temp = root; + // Finding minimum element from right + nodes minNodeForRight = minimumElement(temp.right); + // Replacing current node with minimum node from right subtree + root.value = minNodeForRight.value; + // Deleting minimum node from right now + root.right = deleteNode(root.right, minNodeForRight.value); + + } + // if nodeToBeDeleted has only left child + else if (root.left != null) { + root = root.left; + } + // if nodeToBeDeleted has only right child + else if (root.right != null) { + root = root.right; + } + // if nodeToBeDeleted do not have child (Leaf node) + else + root = null; + } + return root; + } + + public void treet(nodes poi){ + //recursive + //tree traversal + if(poi != null){ + System.out.println(poi.value); + treet(poi.left); + treet(poi.right); + } + + } +} +public class binary_search_tree{ + public static void main(String [] arrrr){ + Scanner wow = new Scanner(System.in); + //creting object + bstree obj = new bstree(); + obj.bstreec(); + + System.out.println("tree :-"); + obj.treet(obj.headp); //display of tree + + //calling to insert + System.out.println("enter the value you wanna insert"); + int ni=wow.nextInt(); + obj.insert(ni); + + //calling for deletion of node + System.out.println("enter the value you wanna delete"); + int val=wow.nextInt(); + obj.deleteNode(obj.headp,val); + + System.out.println("tree afterd:-"); + obj.treet(obj.headp); //display of tree after deleting the node + + } +}