|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.Arrays; |
| 3 | +import java.util.Collections; |
| 4 | + |
| 5 | +public class BST { |
| 6 | + private static class Node { |
| 7 | + int data; |
| 8 | + Node left; |
| 9 | + Node right; |
| 10 | + |
| 11 | + public Node(int d) { |
| 12 | + this.data = d; |
| 13 | + } |
| 14 | + } |
| 15 | + public static void display(Node root) { |
| 16 | + if (root.left != null) |
| 17 | + System.out.print(root.left.data); |
| 18 | + else |
| 19 | + System.out.print("."); |
| 20 | + |
| 21 | + System.out.print("->" + root.data + "<-"); |
| 22 | + // System.out.print(root.data); |
| 23 | + // System.out.print("<-"); |
| 24 | + |
| 25 | + if (root.right != null) |
| 26 | + System.out.print(root.right.data); |
| 27 | + else |
| 28 | + System.out.print("."); |
| 29 | + |
| 30 | + System.out.println(); |
| 31 | + if (root.left != null) |
| 32 | + display(root.left); |
| 33 | + if (root.right != null) |
| 34 | + display(root.right); |
| 35 | + |
| 36 | + } |
| 37 | +public static int min(Node root) { |
| 38 | + if(root==null) { |
| 39 | + return 0; |
| 40 | + } |
| 41 | + if(root.left!=null) { |
| 42 | + return min(root.left); |
| 43 | + }else { |
| 44 | + return root.data; |
| 45 | + } |
| 46 | +} |
| 47 | +public static int max(Node root) { |
| 48 | + if(root==null) { |
| 49 | + return 0; |
| 50 | + } |
| 51 | + if(root.right!=null) { |
| 52 | + return max(root.right); |
| 53 | + }else { |
| 54 | + return root.data; |
| 55 | + } |
| 56 | +} |
| 57 | +public static boolean find(Node root,int data) { |
| 58 | + if(root==null) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + if(data>root.data) { |
| 63 | + return find(root.right, data); |
| 64 | + }else if(data<root.data) { |
| 65 | + return find(root.left, data); |
| 66 | + }else { |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | +} |
| 72 | +public static Node construct(ArrayList<Integer> data,int lo,int hi) { |
| 73 | + if(lo>hi) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + int mid=(lo+hi)/2; |
| 77 | + Node root=new Node(data.get(mid)); |
| 78 | + root.left=construct(data, lo, mid-1); |
| 79 | + root.right=construct(data, mid+1, hi); |
| 80 | + return root; |
| 81 | +} |
| 82 | +public static class sum{ |
| 83 | + int sum=0; |
| 84 | +} |
| 85 | +public static void replacenodewithsum(Node root,sum s) { |
| 86 | + if(root==null) { |
| 87 | + return ; |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + replacenodewithsum(root.right,s); |
| 92 | +//int ord=root.data; |
| 93 | +//root.data=s.sum; |
| 94 | +//s.sum=root.data; |
| 95 | +//s.sum+=ord; |
| 96 | + s.sum+=root.data; |
| 97 | + root.data=s.sum-root.data; |
| 98 | +replacenodewithsum(root.left,s); |
| 99 | + |
| 100 | + |
| 101 | +} |
| 102 | +public static void pir(Node root, int lo,int hi) { |
| 103 | + if(root==null) { |
| 104 | + return ; |
| 105 | + |
| 106 | + } |
| 107 | + if(root.data<lo) { |
| 108 | + pir(root.right, lo, hi); |
| 109 | + }else if(root.data>hi) { |
| 110 | + pir(root.left, lo, hi); |
| 111 | + } |
| 112 | + else { |
| 113 | + pir(root.left,lo,hi); |
| 114 | + System.out.print(root.data); |
| 115 | + pir(root.right, lo, hi); |
| 116 | + } |
| 117 | +} |
| 118 | +//approach 1 for n in bst,nlogn for bt |
| 119 | +public static void pairsumtarget(Node or,Node root,int tar) { |
| 120 | + if(root==null) { |
| 121 | + return; |
| 122 | + } |
| 123 | + int comp=tar-root.data; |
| 124 | + if(root.data<comp) { |
| 125 | + if(find(or, comp)==true) { |
| 126 | + { |
| 127 | + System.out.println(root.data+" "+comp); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + pairsumtarget(or, root.left, tar); |
| 132 | + pairsumtarget(or, root.right, tar); |
| 133 | +} |
| 134 | +// approach 2 for bst in o(n) |
| 135 | +public static void pairsumtarget1(Node root,int tar) { |
| 136 | + ArrayList<Integer> list = new ArrayList<>(); |
| 137 | + filler(root, list); |
| 138 | + int li=0,ri=list.size()-1; |
| 139 | + while(li!=ri) { |
| 140 | + if(list.get(li)+list.get(ri)<tar) { |
| 141 | + li++; |
| 142 | + }else if(list.get(li)+list.get(ri)>tar) { |
| 143 | + ri--; |
| 144 | + }else { |
| 145 | + System.out.println(list.get(li)+" "+ list.get(ri)); |
| 146 | + li++; |
| 147 | + ri--; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | +public static void filler(Node root,ArrayList<Integer> list) { |
| 152 | + if(root==null) { |
| 153 | + return; |
| 154 | + } |
| 155 | + filler(root.left, list); |
| 156 | + list.add(root.data); |
| 157 | + filler(root.right, list); |
| 158 | +} |
| 159 | +// aproach 2 in bt for nlogn |
| 160 | +public static void pairsumtarget2(Node root,int tar) { |
| 161 | + ArrayList<Integer> list = new ArrayList<>(); |
| 162 | + filler(root, list); |
| 163 | + Collections.sort(list); |
| 164 | + int li=0,ri=list.size()-1; |
| 165 | + while(li!=ri) { |
| 166 | + if(list.get(li)+list.get(ri)<tar) { |
| 167 | + li++; |
| 168 | + }else if(list.get(li)+list.get(ri)>tar) { |
| 169 | + ri--; |
| 170 | + }else { |
| 171 | + System.out.println(list.get(li)+" "+ list.get(ri)); |
| 172 | + li++; |
| 173 | + ri--; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +public static void main(String[] args) { |
| 179 | +//ArrayList<Integer> list= new ArrayList<Integer>(Arrays.asList(12,25,37,50,62,75,87)); |
| 180 | + ArrayList<Integer> list= new ArrayList<Integer>(Arrays.asList(80,70,60,50,30,20,10)); |
| 181 | + |
| 182 | + Node root=construct(list, 0,list.size()-1); |
| 183 | +//display(root); |
| 184 | +//System.out.println(find(root, 40)); |
| 185 | +//display(root); |
| 186 | +// sum s=new sum(); |
| 187 | +//replacenodewithsum(root,s); |
| 188 | +//display(root); |
| 189 | + //pir(root, 10, 60); |
| 190 | +//pairsumtarget(root, root, 100); |
| 191 | + |
| 192 | + //pairsumtarget1(root, 100); |
| 193 | + pairsumtarget1(root, 100); |
| 194 | + //pairsumtarget2(root, 100); |
| 195 | + |
| 196 | +} |
| 197 | + |
| 198 | +} |
0 commit comments