└── java /java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner sc=new Scanner(System.in); 6 | //object creation for BST class 7 | BST o=new BST(); 8 | 9 | o.insert(5); 10 | o.insert(6); 11 | o.insert(3); 12 | o.insert(2); 13 | o.insert(11); 14 | o.insert(20); 15 | o.insert(110); 16 | //Looping based search 17 | System.out.println(o.search(20)); 18 | //recursion based search 19 | System.out.println(o.Search(o.Realroot,50)); 20 | } 21 | } 22 | //structure of each node 23 | class node{ 24 | int data; 25 | node left,right; 26 | node(int key){ 27 | data=key; 28 | left=right=null; 29 | } 30 | } 31 | //BINARY SEARCH TREE CLASS 32 | class BST{ 33 | //original Root declaration 34 | node Realroot; 35 | ////normal insert method to avoid unwanted error for backtrack 36 | void insert(int key){ 37 | //calling recursive insert method and reassigning root again 38 | Realroot=insertRecursively(Realroot,key); 39 | } 40 | //recursive insert method 41 | node insertRecursively(node dupRoot,int key){ 42 | //condtion for creating new node 43 | if(dupRoot==null){ 44 | dupRoot= new node(key); 45 | return dupRoot; 46 | } 47 | //condition for right side insertion 48 | if(key>dupRoot.data){ 49 | //calling recursive insert but this time with dupRoot->right as dupRoot 50 | dupRoot.right=insertRecursively(dupRoot.right,key); 51 | } 52 | //condition for left side insertion 53 | else if(keyleft as dupRoot 55 | dupRoot.left=insertRecursively(dupRoot.left,key); 56 | } 57 | //return for original Root 58 | return dupRoot; 59 | } 60 | //inOrder traversal 61 | void inOrder(node Root){ 62 | if(Root!=null){ 63 | //traversing till left end 64 | inOrder(Root.left); 65 | //printing data 66 | System.out.print(Root.data+" "); 67 | //traversing till right side end 68 | inOrder(Root.right); 69 | } 70 | 71 | } 72 | //preOrder traversal 73 | void preOrder(node Root){ 74 | if(Root!=null){ 75 | //printing data 76 | System.out.print(Root.data+" "); 77 | //traversing till left end 78 | inOrder(Root.left); 79 | //traversing till right side end 80 | inOrder(Root.right); 81 | } 82 | } 83 | //postOrder traversal 84 | void postOrder(node Root){ 85 | if(Root!=null){ 86 | //traversing till left end 87 | inOrder(Root.left); 88 | //traversing till right side end 89 | inOrder(Root.right); 90 | //printing data 91 | System.out.print(Root.data+" "); 92 | } 93 | } 94 | boolean search(int target){ 95 | //assigning temp with original Root Value 96 | node temp=Realroot; 97 | //condition for not finding any data 98 | if(temp==null) return false; 99 | else { 100 | //loopd to check all nodes till it reaches null 101 | while(temp!=null){ 102 | //left search condition 103 | if(temp.data>target) 104 | //moving temp to its left side 105 | temp=temp.left; 106 | //right search condition 107 | else if(temp.datatemp.data) 129 | //moving temp to its right side 130 | return Search(temp.right,target); 131 | //Equal case 132 | else return true; 133 | } 134 | } 135 | 136 | --------------------------------------------------------------------------------