From 8b09eff401a900683b20e31975dc7a6a262ce5c9 Mon Sep 17 00:00:00 2001 From: Divyansh Bhardwaj Date: Sat, 28 Mar 2020 11:06:18 +0530 Subject: [PATCH 1/6] Setting up GitHub Classroom Feedback From e0f915dde601798d0ce773cde8c8b0c856587a32 Mon Sep 17 00:00:00 2001 From: Gopal868484 Date: Sat, 28 Mar 2020 12:04:53 +0530 Subject: [PATCH 2/6] problem 1 completed --- .idea/.gitignore | 2 + .idea/codeStyles/codeStyleConfig.xml | 5 + .idea/misc.xml | 6 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + assignment-2-Gopal868484.iml | 11 ++ .../problem1/main/MyMain.class | Bin 0 -> 1382 bytes .../problem1/mybst/MyBinarySearchTree.class | Bin 0 -> 3384 bytes .../problem1/node/TreeNode.class | Bin 0 -> 943 bytes .../problem2/main/MyMain.class | Bin 0 -> 271 bytes .../problem3/main/MyMain.class | Bin 0 -> 271 bytes .../problem3/myqueue/MyPriorityQueue.class | Bin 0 -> 304 bytes .../problem3/node/Node.class | Bin 0 -> 265 bytes .../problem4/main/MyMain.class | Bin 0 -> 271 bytes .../problem4/myqueue/MyQueue.class | Bin 0 -> 280 bytes .../circularqueue/MyCircularQueue.class | Bin 0 -> 316 bytes .../problem5/main/MyMain.class | Bin 0 -> 271 bytes .../problem5/node/Node.class | Bin 0 -> 265 bytes .../problem5/student/Student.class | Bin 0 -> 280 bytes src/problem1/main/MyMain.java | 28 +++- src/problem1/mybst/MyBinarySearchTree.java | 145 +++++++++++++++++- src/problem1/node/TreeNode.java | 41 ++++- 22 files changed, 230 insertions(+), 22 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 assignment-2-Gopal868484.iml create mode 100644 out/production/assignment-2-Gopal868484/problem1/main/MyMain.class create mode 100644 out/production/assignment-2-Gopal868484/problem1/mybst/MyBinarySearchTree.class create mode 100644 out/production/assignment-2-Gopal868484/problem1/node/TreeNode.class create mode 100644 out/production/assignment-2-Gopal868484/problem2/main/MyMain.class create mode 100644 out/production/assignment-2-Gopal868484/problem3/main/MyMain.class create mode 100644 out/production/assignment-2-Gopal868484/problem3/myqueue/MyPriorityQueue.class create mode 100644 out/production/assignment-2-Gopal868484/problem3/node/Node.class create mode 100644 out/production/assignment-2-Gopal868484/problem4/main/MyMain.class create mode 100644 out/production/assignment-2-Gopal868484/problem4/myqueue/MyQueue.class create mode 100644 out/production/assignment-2-Gopal868484/problem5/circularqueue/MyCircularQueue.class create mode 100644 out/production/assignment-2-Gopal868484/problem5/main/MyMain.class create mode 100644 out/production/assignment-2-Gopal868484/problem5/node/Node.class create mode 100644 out/production/assignment-2-Gopal868484/problem5/student/Student.class diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..e7e9d11 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1763e15 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..971920f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/assignment-2-Gopal868484.iml b/assignment-2-Gopal868484.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/assignment-2-Gopal868484.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/assignment-2-Gopal868484/problem1/main/MyMain.class b/out/production/assignment-2-Gopal868484/problem1/main/MyMain.class new file mode 100644 index 0000000000000000000000000000000000000000..5f25cb327ad8874018a16080f380700ca347f09a GIT binary patch literal 1382 zcmaJ>;cgR05dPMLoO2w+Bypex(hCGI1TchBAP#}XV4BJy5spB>ueH6zN6vTF-a3-s zgx7!{K>~>f;71>+;@h*4jgr=uW_D(0X21Dn>~H^m{tDnOp1L@Wa}MTQWN^-k-zMN= zK8y1%E?~iWE@pAb>dRA1S#)sC!F7T0l^_bts=$%Ll@|hI<+!N?CaXcDo^{)GrMG20 zq-UxcH)Qxi>cH9q=a^{)iNI{Nqhos8w~B2UM8%EX233jGtjYVro9a8cCySwsc8WEl zgJ`Fez9#h!^QRBFCbD+VXvL>5jFj)y6T|vzK_qprrlfAPwi(2?+*;h#4OIy&+GBkP zH|(n(PGC}Ceu(iuig>tzqJx_rZed9vzopftZYu3>Yq_U%B13=KcW~Ro@3`aP4_h@~ zRd0>oXa!+YtH|Gs!XAS>j^v&|ZY_>YVzlgRDAS6Y$>cchXR+*|gcX6AA)+B`%o}o@NJy=h7}e(Z;x6y=eV&83xJ8l;saCR0a;DRoKf48-jtvSq zp?u-+0p$qZwyu6~A>jRR+0N6oS$FXO+@Qb6HcYI04xW&+ zY8>x&I|RHQMM{@LnIwuYydQ0KP@_Mdzhu-5RkF}eEFFX~;`u3YyVp^NgpY1@f0Cuq z-UcPgG7M{hQ6&czfr|(FI}Dq^-2ca69v=S@>?Z?Ze*ly@T19F>ee~)lh!5%YNy_oQ z6ZWsc8=R*xLu&$g%+h0%|6#EVb^Qx6uRh^ObuqWJkFmwvTq$W{p*aFFHq1kudE#jlL=iSrU$yhiU5W9|{* zDo&!p9{$EOo)fpkskEcnK5AO0Su2az_=PpNyEsETgR@wpcIkP9$M}=Cg)~m!3Vm0< E1B&QaD*ylh literal 0 HcmV?d00001 diff --git a/out/production/assignment-2-Gopal868484/problem1/mybst/MyBinarySearchTree.class b/out/production/assignment-2-Gopal868484/problem1/mybst/MyBinarySearchTree.class new file mode 100644 index 0000000000000000000000000000000000000000..9a4dc9d9d6326ad0c23e245f35e350a029300167 GIT binary patch literal 3384 zcma)8+fo!)6kR~2V?B$bDl z+aKi7N>Y^}DpQrmSe0+GQd#G8Gc?0sr7X{!-o4M>YwfkqZvOfAQ3L>^;xe5;FS8Z#)sF%)VO>05V&e*y>&P~69=zw7wZcahC=hPh@7^Yc4 zUEVPD@wL*tZqH~usGu=#6|~|V%{IidZ;ZIhhNGZ!CwysR-f^>I8$*Vv*&EZkW*3%4 zHfD+~>F$(-bEN0g&WSyM4AaqVf{yfzN<<5qt0_=MiOPF5r5Dz0$5rhdpGKW)YwNo0=%eO@y`bAxQ``UJoxD#RUb;!6VX3=1J|F=e~#;U$m`~mqek&H3e(UWWxIGV3W4)vuLU` z$?y$jtL3=9XBo-lwoST59Vwuvg=i-smV$(5mM(FbUT)t|AHq&fTWfYfA2CEXweIbZ zGmo`(O~qR{qM{XtRh&e(f|lxyEBrcDJiuucAE86V$H=I7D0Yv;E{#kKb1F2<$52qQ zfFrEX5WCMI`_(bsUA7jSx+wHG7F8@^S%rbeDzZ4M;(`cqNi6;7RxyA<1s!D|`?WX| z2nyMvW-etX<{#??sv?^HnrvXP^HhsD+q<~962{Up9pg0m+d!D<4ae0>>?UiCK8Nx$ zfnjAQZNqe@U0c^myje|E%uKy1oQh@=D<~=mDebI#Nn6o}EwiAxcMW%$JIB>bmv{IO zZ<`lh;%993Dr48kDB}Izx2NvJP!iR9qM&PsyJ4{tFIE`;VxX_Jn6oW(1S4EXQ${0xV{Pg{);M z>sZ8A7O|UevqUw(l86$uds)laa-#9M0dLaeRmpqRxX5vGFW|6~V*+OJs zj0k!u_G$7vLzT`3s@_%3)#Wo2e8NzKn7usW^}{$#B7LJ|!y4BQczoKP(;Dos^X#n) z?8}RRB#nNOB*~ufrRA{nV{Z~(gi&(g9LZfyMV_IN!Gp}WkG(?lX;?Wbxkm_KPSO=n z=4Nxxj|v-otLn^;hF^?bI#*P|m%YK)xuH_$NfNrnJ2gd}r>XPp zfCO*U`QpB8gb-D__8!^HlFj|9Z20x7#>R)==)azIJG|IT>2~kDqRMZF3+d*?)juD4TREHzR`WeE9O(epHIy@7ulQg zUk0>I`0*89h(Vvx=hR0Ow}TUzB@GWg4Jj#K<7Qvu=DE4SRAq&q^5EC3^*3yyZ+Sbv zXg<9wdv|6Bh78R>4y literal 0 HcmV?d00001 diff --git a/out/production/assignment-2-Gopal868484/problem1/node/TreeNode.class b/out/production/assignment-2-Gopal868484/problem1/node/TreeNode.class new file mode 100644 index 0000000000000000000000000000000000000000..cc44e128bf11969f3428231e0f5da057250567af GIT binary patch literal 943 zcmZ{i%}(1;5QWcm96K08`2PznrLahXgw+)b5F1b>q=-r_in?`@OE3xvGKQyO0TKwQ zi#|Xfs_L0*2T@~wUl5_UaPA;Mw1-Gk{W7E7)t+Bm(Gd#;p!?3JXYvXC8AWf@O z$7Km!iE`gl@DfY1(EBmCjobQbB%D*jA$KpsyU>S^EgvpQJ|^J#n1rXWl)n~*8L0_+ zVZR%EYhCI#7sQF0*yNrazCZz%Xfd)x&Uh-)yHIR3MLW&YPu^Sf3-uSMdgU2bT|J{9 z#(p$aW!fdGf@(IkL~WXM^HiBJccvb*Mx9EgFJBjP%t^Zh^oqH~`V*{s1{OrtF(1oD z#!nM68AX{P;$ep97`!(q&w$SpZQ&h=^9tg;f-G`?|3L~5BOuGUAj??Eir+wXxS~;z r3PIM#fXL2d1FN|p_9)0kF34Iw$h&7sH?l>L?J*$o3kXQXjK2O4Y<_zo literal 0 HcmV?d00001 diff --git a/out/production/assignment-2-Gopal868484/problem2/main/MyMain.class b/out/production/assignment-2-Gopal868484/problem2/main/MyMain.class new file mode 100644 index 0000000000000000000000000000000000000000..3ba7ba56dbc2649d2009d914e9fcbc1c6d1a2873 GIT binary patch literal 271 zcmZvWy>7xl5QJxqf54c45Td683QkIsf`|qwl??@m{%lTg$Un$2NIX`mNGW x7sw zP-a(}+xfI}+S~8X6~G%hK5A%rc=6CCxbHgG)r?RZjy42)UThVi9qL>yPFbwVwTx3H z-LObxx{;+e{t4}BuRjU>@K_d1vsaNxor~xa@iQg(tKw89>O-3!ntyn5kY^ddhC}H6 zmm_q{ib!R?6U+FZk_rP@e6%Uz<}9nrPCPu|jK4u&3@mu8E@Qw)oi%vO4jSxDyvb@I FxC5LhHy8i_ literal 0 HcmV?d00001 diff --git a/out/production/assignment-2-Gopal868484/problem3/main/MyMain.class b/out/production/assignment-2-Gopal868484/problem3/main/MyMain.class new file mode 100644 index 0000000000000000000000000000000000000000..71832683da6957f00e79a4eb49084b97f75f0965 GIT binary patch literal 271 zcmZvWy>7xl5QJxqf54c45Td683a&_zf`|qwl??@m{%lTg$Un$2NIX`mNGW x7sw zP-a(}+xfI}+S~8X6~G%hK5A%rc=6CCxbHgG)r?RZjy42)UThVi9qL>yPFbwVwTx3H z-LObxx{;+e{t4}BuRjU>@K_d1vsaNxor~xa@iQg(tKw89>O-3!ntyn5kY^ddhC}H6 zmm_q{ib!R?6U+FZk_rP@e6%Uz<}9nrPCPu|jK4u&3@mu8E@Qw)oi%vO4jSxDyvb@I FxC5O=HyQu{ literal 0 HcmV?d00001 diff --git a/out/production/assignment-2-Gopal868484/problem3/myqueue/MyPriorityQueue.class b/out/production/assignment-2-Gopal868484/problem3/myqueue/MyPriorityQueue.class new file mode 100644 index 0000000000000000000000000000000000000000..2b256ff0e17206cbe6d32bfff434736314e0bbda GIT binary patch literal 304 zcmaivy-ve06ot=C^V5bvtEvw`nF<3Q7#KiZkUFJ>s;IjYtl}z(!6Zm|EG8rd9)O3c zdR;NG;42;Z`)nQQ{_MWK0bFAcql4oJClUID@K#s4o)S8f^EJW0Gf#@pPj#ghO_?eC zD6@jiFg3X>*3xRnztXRt^&87xV6ot7xl5QJxqf54c45Td683a*Kif`|qwl??@m{%lTg$Un$2NIX`mNGW x7sw zP-a(}+xfI}+S~8X6~G%hK5A%rc=6CCxbHgG)r?RZjy42)UThVi9qL>yPFbwVwTx3H z-LObxx{;+e{t4}BuRjU>@K_d1vsaNxor~xa@iQg(tKw89>O-3!ntyn5kY^ddhC}H6 zmm_q{ib!R?6U+FZk_rP@e6%Uz<}9nrPCPu|jK4u&3@mu8E@Qw)oi%vO4jSxDyvb@I FxC5SKHyi)} literal 0 HcmV?d00001 diff --git a/out/production/assignment-2-Gopal868484/problem4/myqueue/MyQueue.class b/out/production/assignment-2-Gopal868484/problem4/myqueue/MyQueue.class new file mode 100644 index 0000000000000000000000000000000000000000..fcb8537e021ebbd1f8fad6a67293f89fdc6f5905 GIT binary patch literal 280 zcmZ{ey>7xl5QJxqf535qMTOGQ0R`7YN|A^LiOPVYMD%BKfRA!&S}q=;|ahkLLUu0dU*2CCb;i9*VUNN=ns|zds3_vp&jX5&Fd^y<(G_8 zCY`8AWV)24HvSInYNLM%&(U{TFwI^>w)?47t;B5iVcs`_|5?;!qNdvX65RP{EB|Bw z8xEoSUziY@HId4EEf(=sB^7$G_s&3RUrow#|x86H4;1{OS4moea@$r{{d2QBs{ J{=jM>xB^TWJ8=L2 literal 0 HcmV?d00001 diff --git a/out/production/assignment-2-Gopal868484/problem5/circularqueue/MyCircularQueue.class b/out/production/assignment-2-Gopal868484/problem5/circularqueue/MyCircularQueue.class new file mode 100644 index 0000000000000000000000000000000000000000..28bed5bcd62b66933794431e34d1941809ddddf3 GIT binary patch literal 316 zcma)%%}xR_6ot(qqDVr9aD2jHQM zw}h1|dy|uXU(=J^!_RIH;1Qh^EnFtJO3)_6&!#fX3!&A&T@b=S{i+G=+*Eq()`hln zS(I$Ld97r*kk)uU$*@_PPeMO`w>8`KgHXmQS4#Wgw9{g=8Ju|P`H7IuYG;)m8sB;S z=ly;qzhs6G5#i>4m4uG}BuZJmiAk~2szDC{Z|D#4a2sdLB#sP3ci*5bivS5{%o;ey R8E1C9M@%Ub;es7xV6otc9i=P>5s1 zhOhL==abKsPv`$5fENs6wD1t27ojf*-)w1{nV>ZquLS`>@rh->a3u7 zOsiZMD_vWcZ_;lz_FFJa_jN_PdsZduYDv5b;*aW3=VoDDzvJX|tABI?9|0HMcLW3X zKoz=NtM_bc@&-@v_+58|!zLLrscQy;$sgpG!b3!c)IbMqGP&LZrkF|4B|Rh;js`T! literal 0 HcmV?d00001 diff --git a/out/production/assignment-2-Gopal868484/problem5/student/Student.class b/out/production/assignment-2-Gopal868484/problem5/student/Student.class new file mode 100644 index 0000000000000000000000000000000000000000..65dd57331c633a0782546d227b4f1524af8e148e GIT binary patch literal 280 zcmZ{eJ#WG=5Qg6i`G7QS5EEi!D-88ib)f2k#1z2*Vt0Z?jq*_?PX8BEr4IZ6epJ;n zuwqN^`FV7A^7-%g1mF$*5FI=QcnS~`{C8by^Fio5PqqYiUhNbiPIRf3ZIP;aBh#GC zAgMB$Z)L45-=u3kbwd~>UvxUSkdQwwdkL^uC(kl!+b3y(0o zH%#c;Hj&G6FIMS6Wd>t7{Cj(dFVA!OOya_TH~j$}SvUwdebzt^UC!w8_LxE@!ULy+ F=pS|@JH7w_ literal 0 HcmV?d00001 diff --git a/src/problem1/main/MyMain.java b/src/problem1/main/MyMain.java index 2539f2b..064aa96 100644 --- a/src/problem1/main/MyMain.java +++ b/src/problem1/main/MyMain.java @@ -1,11 +1,25 @@ -/* - * Created by IntelliJ IDEA. - * User: Vaibhav - * Date: 23-Mar-20 - * Time: 7:17 PM - */ package problem1.main; -// executable class + +import problem1.mybst.MyBinarySearchTree; public class MyMain { + public static void main(String[] args) { + MyBinarySearchTree mytree = new MyBinarySearchTree(); + mytree.insert(50); + mytree.insert(30); + mytree.insert(20); + mytree.insert(40); + mytree.insert(70); + mytree.insert(60); + mytree.insert(80); + mytree.insert(90); + mytree.insert(10); + System.out.println("PreOrder Traversal : "); + mytree.traversePreOrder(); + System.out.println("Left children Only : "); + //traverseLeft() methods prints all left children as well as returns the no of nodes not having left a child + System.out.println("No of nodes not having Left Subchild: " + mytree.traverseLeft()); + + } } + diff --git a/src/problem1/mybst/MyBinarySearchTree.java b/src/problem1/mybst/MyBinarySearchTree.java index 1dd6794..90cbbf2 100644 --- a/src/problem1/mybst/MyBinarySearchTree.java +++ b/src/problem1/mybst/MyBinarySearchTree.java @@ -1,10 +1,139 @@ -/* - * Created by IntelliJ IDEA. - * User: Vaibhav - * Date: 23-Mar-20 - * Time: 7:17 PM - */ package problem1.mybst; -// to implement BinarySearchTree + +import problem1.node.TreeNode; + public class MyBinarySearchTree { -} + private TreeNode root; + + public MyBinarySearchTree() { + root = null; + } + + public TreeNode getRoot() { + return root; + } + + + public void insert(int data) { + root = insertRecursive(root, data); + } + + TreeNode insertRecursive(TreeNode root, int data) { + if (root == null) { + root = new TreeNode(data); + return root; + } else { + if (root.getData() > data) { + root.setLeft(insertRecursive(root.getLeft(), data)); + } else if (root.getData() < data) { + root.setRight(insertRecursive(root.getRight(), data)); + } + return root; + } + } + + + public void traverseInOrder() { + traverseInOrderRec(root); + System.out.println(); + } + + // A recursive function to do inorder traversal of BST + void traverseInOrderRec(TreeNode root) { + if (root != null) { + traverseInOrderRec(root.getLeft()); + System.out.print(root.getData() + " "); + traverseInOrderRec(root.getRight()); + } + } + + //prints only left nodes + public int traverseLeft() { + int noOfNodesNotHavingLeftChild = traverseLeftNodesRec(root, false); + System.out.println(); + return noOfNodesNotHavingLeftChild; + + } + + // A recursive function to do left traversal of BST + //Also returns no of nodes not having left subchild + int traverseLeftNodesRec(TreeNode root, boolean fromLeft) { + int count = 0; + if (root != null) { + if (fromLeft == true) + System.out.print(root.getData() + " "); + if (root.getLeft() == null) { + count++; + } + return count = count + traverseLeftNodesRec(root.getLeft(), true) + traverseLeftNodesRec(root.getRight(), false); + } + return count; + } + + public void traversePreOrder() { + traversePreOrderRec(root); + System.out.println(); + } + + // A recursive function to do preorder traversal of BST + void traversePreOrderRec(TreeNode root) { + if (root != null) { + System.out.print(root.getData() + " "); + traversePreOrderRec(root.getLeft()); + traversePreOrderRec(root.getRight()); + } + } + + public void traversePostOrder() { + traversePostOrderRec(root); + System.out.println(); + } + + // A recursive function to do preorder traversal of BST + void traversePostOrderRec(TreeNode root) { + if (root != null) { + traversePostOrderRec(root.getLeft()); + traversePostOrderRec(root.getRight()); + System.out.print(root.getData() + " "); + } + } + + //Searches Element + public boolean search(int data) { + TreeNode node = searchRec(root, data); + if (node == null) { + return false; + } else if (node.getData() == data) { + return true; + } else { + return false; + } + } + + public TreeNode searchRec(TreeNode root, int data) { + // Base Cases: root is null or key is present at root + if (root == null || root.getData() == data) + return root; + + // val is greater than root's key + if (root.getData() > data) + return searchRec(root.getLeft(), data); + + // val is less than root's key + return searchRec(root.getRight(), data); + } + + public int count() { + return countRec(root); + } + + // A recursive function to count total no of nodes of BST + public int countRec(TreeNode root) { + if (root == null) + return 0; + int count = 0; + count++; + count += (countRec(root.getLeft()) + countRec(root.getRight())); + return count; + } +} \ No newline at end of file diff --git a/src/problem1/node/TreeNode.java b/src/problem1/node/TreeNode.java index 80c857d..59f43af 100644 --- a/src/problem1/node/TreeNode.java +++ b/src/problem1/node/TreeNode.java @@ -1,10 +1,37 @@ -/* - * Created by IntelliJ IDEA. - * User: Vaibhav - * Date: 23-Mar-20 - * Time: 7:17 PM - */ package problem1.node; public class TreeNode { -} + private int data; + private TreeNode left; + private TreeNode right; + + public TreeNode(int data) { + this.data = data; + this.left = null; + this.right = null; + } + + public int getData() { + return data; + } + + public void setData(int data) { + this.data = data; + } + + public TreeNode getLeft() { + return left; + } + + public void setLeft(TreeNode left) { + this.left = left; + } + + public TreeNode getRight() { + return right; + } + + public void setRight(TreeNode right) { + this.right = right; + } +} \ No newline at end of file From 32a501853a955efb9b86a89921e8c8c795bee046 Mon Sep 17 00:00:00 2001 From: Gopal868484 Date: Sat, 28 Mar 2020 12:40:51 +0530 Subject: [PATCH 3/6] problem 2 completed --- .../problem2/main/MyMain.class | Bin 271 -> 1290 bytes src/problem2/main/MyMain.java | 29 +++- src/problem2/mybst/MyBinarySearchTree.java | 138 ++++++++++++++++++ src/problem2/node/TreeNode.java | 37 +++++ 4 files changed, 197 insertions(+), 7 deletions(-) create mode 100644 src/problem2/mybst/MyBinarySearchTree.java create mode 100644 src/problem2/node/TreeNode.java diff --git a/out/production/assignment-2-Gopal868484/problem2/main/MyMain.class b/out/production/assignment-2-Gopal868484/problem2/main/MyMain.class index 3ba7ba56dbc2649d2009d914e9fcbc1c6d1a2873..fd53048d03b5ec34b5c0e28fd09a52d8f368ff2e 100644 GIT binary patch literal 1290 zcmZ`&+fGwK6kVqxEu~_Cf_ELf7D4L$0&1d06Oy7ts)_Mw+B480=bUNg44VFf&p!AU z`hbZhJ{v#6Pcg1JEfyoT$?QFQ?^(0fboIxtFW&&%!rdau5P8fLk%REd=@Fd4*#gcL zaUK_ZrCPwOj~5G=^Krg_O9d?CQOo0UfZ@A3(e8eLebw2Q0fru#Pz5+x*NJ+%6*rY_ z$Y#XKMBTJx^io>w_x<9KYwI+?biHE@tMS!ZEOk;_>8>y=5zTMeufDEt$~UqW$z-Fp z=B!ROma^NV-QfAjJ=$azcb!$d`pho)ZZmbnf2b2_yK73?R=dF=-c?*PTehX1XwP=E zM`6L=TEaLc0?Z6J{s*LlE4Z4+wGyu5Mu4eRtDf0VS<$fajk2kX#3CR2W{EF^4$-QOcH9RO(Gr;_qj1nD88XeoY1N0M6#tqA)PRtj!Css zLM7pRQ7@2Fi+f6G@ieB9N^u6 z+(WV0XsfPh^9@AH#4!oSIusqF6W8a{G_IZ9@BX~hTOCDWL-R~3V{-2+qaM$*2S8O` z{<^WkgrP_>zX`K05}GjGMW-_O?FRk|P#&1-S#wjh98IH>R9PlN)n)InT_bDV)Tx+8 zH(Tsa^&F{9ZIyO(G^LU;Z8Vu-{#_1`@A#pJUY@G%ZRK)by4rCK|3o{bzDEL#?^OOc z12Z_t|FHk$zyVBfo#d{@7%=X?_!+^+tX<(c+$&;=d8waI;}CZvIE*8#_`^R5mSf!f zirlNu*jJw`huavMD_8tFj_+9r=6k#MS?grB7P)@H{)ypjsB=7 delta 131 zcmeC;>Sq!?^>5cc1_lOm1_3SxW(Hn%20nHM{)t}ka$LdrrA5i9Zkai$j11hqmA;9Y zd3sriWr;itEDWrZ-!o~6F(@)H0X2aDBM<_Gfh61Hhs_9O#Adi88i-7}3@_^Z_ Q44gndNHsT*WMbd}0ItOnr2qf` diff --git a/src/problem2/main/MyMain.java b/src/problem2/main/MyMain.java index 4d84403..14f1636 100644 --- a/src/problem2/main/MyMain.java +++ b/src/problem2/main/MyMain.java @@ -1,11 +1,26 @@ -/* - * Created by IntelliJ IDEA. - * User: Vaibhav - * Date: 23-Mar-20 - * Time: 7:15 PM - */ package problem2.main; +import problem2.mybst.MyBinarySearchTree; // executable class -// use problem1.mybst.MyBinarySearchTree class for creating binary tree + public class MyMain { + public static void main(String[] args) { + MyBinarySearchTree mytree = new MyBinarySearchTree(); + mytree.insert(100); + mytree.insert(20); + mytree.insert(200); + mytree.insert(300); + mytree.insert(150); + mytree.insert(30); + mytree.insert(10); + System.out.println("PreOrder Traversal : "); + mytree.traversePreOrder(); + System.out.println("InOrder Traversal : "); + mytree.traverseInOrder(); + System.out.println("PostOrder Traversal : "); + mytree.traversePostOrder(); + System.out.println("Condition A : Root element is placed at opposite end," + "\n" + + "i.e. in pre order root is the first element where as in post order root is the last element. is TRUE"); + System.out.println("Condition B :They have common mid point,\n" + + "i.e. both the traversal will give same element at the mid position for odd number of nodes. is TRUE"); + } } diff --git a/src/problem2/mybst/MyBinarySearchTree.java b/src/problem2/mybst/MyBinarySearchTree.java new file mode 100644 index 0000000..e010b12 --- /dev/null +++ b/src/problem2/mybst/MyBinarySearchTree.java @@ -0,0 +1,138 @@ +package problem2.mybst; +// to implement BinarySearchTree + +import problem2.node.TreeNode; + +public class MyBinarySearchTree { + private TreeNode root; + + public MyBinarySearchTree() { + root = null; + } + + public TreeNode getRoot() { + return root; + } + + + public void insert(int data) { + root = insertRecursive(root, data); + } + + TreeNode insertRecursive(TreeNode root, int data) { + if (root == null) { + root = new TreeNode(data); + return root; + } else { + if (root.getData() > data) { + root.setLeft(insertRecursive(root.getLeft(), data)); + } else if (root.getData() < data) { + root.setRight(insertRecursive(root.getRight(), data)); + } + return root; + } + } + + + public void traverseInOrder() { + traverseInOrderRec(root); + System.out.println(); + } + + // A recursive function to do inorder traversal of BST + void traverseInOrderRec(TreeNode root) { + if (root != null) { + traverseInOrderRec(root.getLeft()); + System.out.print(root.getData() + " "); + traverseInOrderRec(root.getRight()); + } + } + + //prints only left nodes + public int traverseLeft() { + int noOfNodesNotHavingLeftChild = traverseLeftNodesRec(root, false); + System.out.println(); + return noOfNodesNotHavingLeftChild; + } + + // A recursive function to do left traversal of BST + //Also returns no of nodes not having left subchild + int traverseLeftNodesRec(TreeNode root, boolean fromLeft) { + int count = 0; + if (root != null) { + if (fromLeft == true) + System.out.print(root.getData() + " "); + if (root.getLeft() == null) { + count++; + } + return count = count + traverseLeftNodesRec(root.getLeft(), true) + traverseLeftNodesRec(root.getRight(), false); + } + return count; + } + + public void traversePreOrder() { + traversePreOrderRec(root); + System.out.println(); + } + + // A recursive function to do preorder traversal of BST + void traversePreOrderRec(TreeNode root) { + if (root != null) { + System.out.print(root.getData() + " "); + traversePreOrderRec(root.getLeft()); + traversePreOrderRec(root.getRight()); + } + } + + public void traversePostOrder() { + traversePostOrderRec(root); + System.out.println(); + } + + // A recursive function to do preorder traversal of BST + void traversePostOrderRec(TreeNode root) { + if (root != null) { + traversePostOrderRec(root.getLeft()); + traversePostOrderRec(root.getRight()); + System.out.print(root.getData() + " "); + } + } + + public boolean search(int data) { + TreeNode node = searchRec(root, data); + if (node == null) { + return false; + } else if (node.getData() == data) { + return true; + } else { + return false; + } + } + + public TreeNode searchRec(TreeNode root, int data) { + // Base Cases: root is null or key is present at root + if (root == null || root.getData() == data) + return root; + + // val is greater than root's key + if (root.getData() > data) + return searchRec(root.getLeft(), data); + + // val is less than root's key + return searchRec(root.getRight(), data); + } + + public int count() { + return countRec(root); + } + + // A recursive function to count total no of nodes of BST + public int countRec(TreeNode root) { + if (root == null) + return 0; + int count = 0; + count++; + count += (countRec(root.getLeft()) + countRec(root.getRight())); + return count; + } +} \ No newline at end of file diff --git a/src/problem2/node/TreeNode.java b/src/problem2/node/TreeNode.java new file mode 100644 index 0000000..606506f --- /dev/null +++ b/src/problem2/node/TreeNode.java @@ -0,0 +1,37 @@ +package problem2.node; + +public class TreeNode { + private int data; + private TreeNode left; + private TreeNode right; + + public TreeNode(int data) { + this.data = data; + this.left = null; + this.right = null; + } + + public int getData() { + return data; + } + + public void setData(int data) { + this.data = data; + } + + public TreeNode getLeft() { + return left; + } + + public void setLeft(TreeNode left) { + this.left = left; + } + + public TreeNode getRight() { + return right; + } + + public void setRight(TreeNode right) { + this.right = right; + } +} \ No newline at end of file From 63a9e19d3ace28fb70b6b51c3a8bc50c8dc1bb4f Mon Sep 17 00:00:00 2001 From: Gopal868484 Date: Sat, 28 Mar 2020 12:50:09 +0530 Subject: [PATCH 4/6] problem 3 completed --- .../problem3/main/MyMain.class | Bin 271 -> 1847 bytes .../problem3/myqueue/MyPriorityQueue.class | Bin 304 -> 2186 bytes .../problem3/node/Node.class | Bin 265 -> 813 bytes .../problem5/student/Student.class | Bin 280 -> 1927 bytes src/problem3/main/MyMain.java | 45 ++++++- src/problem3/myqueue/MyPriorityQueue.java | 125 +++++++++++++++++- src/problem3/node/Node.java | 33 ++++- src/problem5/student/Student.java | 61 ++++++++- 8 files changed, 237 insertions(+), 27 deletions(-) diff --git a/out/production/assignment-2-Gopal868484/problem3/main/MyMain.class b/out/production/assignment-2-Gopal868484/problem3/main/MyMain.class index 71832683da6957f00e79a4eb49084b97f75f0965..4caa548a5902aa5e2d86fc1c4b35ecff6d3f74a1 100644 GIT binary patch literal 1847 zcmb_c*>W326g_QAnz1~{%6Lg23qx=~j)?~_VTrPUMN9;eVm1uC~l9GNVZgYVjjBZp4<1_wfphEM}GnM03wakc+)^G zjWNA1n7Euq0*hLHOD|VUESYdkylvtg6YrW>p1^y!s&((DiQ+?>n69UB12;8x%Lc-|+~sOF@-;TRfCn?tuwaml4BU zsTBrlxfQon-)o*dr;cr``q=ozw1_1DE-}h?2U)JWIsJZ&5-=DE!yPy z;sARm#>l~XD1%U{&W1iip3;?|9ahDv)Ui%>16a~NEu6v23{F47(HsnCdPlChk!sh3 zuPD-ZC&Dcmwj*av`g<|qrxtEw)xu|*In=+Jo6 zS(Q;;gv3+{JXv!pK5tN2e54R&D=Z3?yeQ{x!!N9k=$DCol-%RQ62My7?Y2EBrc~u2r$3T_td?^ST?G}w& zQO{Yy<546i~VBw><;&9o`~kVhWX(z29RTho1K;zaeVN?cPB4Dm|Kscz&)=b zmB<^QR$Us(Z&Yjy7hZf0uVId!RQ-+7LrAX!t*?;e(#~j4UOoi-JyxHkH5G#<&e6Zs zJ%IDLK+*(Wg+mA#v-?OA9q`X1jBOucytMcTiS4XmCl8T2fU$T0Glr7(#0ey2r%xcJ zJ$V92*wzUoo3dXz0U36tkNklt@|iursUy()e}HKn!vBTN9AP#l?bqoVGL(NBCMI!~ z{;yyPw~)mKPN7O6>|+|gU3(-CjbCDzyPw+bCsk-C1 fMDiHkzz?*i3Hc5?_@3JK5y=_STq7(WuOI&hx5?U4 delta 108 zcmdna*Uuz+>ff$?3=9nB3<6vX%nZEj41DYi{1emTCmS$IGqW(TPWENe5@S$gU;@g5 t03#3rg@Gj7Rbfp@xH7s$nZfJ7cmjSr3fZ*IfM^oz9Pd%Axz1f z)G)0fr4Vq8w4>0R%sKW-#>k#ZSoWHcNO2fb2;OyUt3a!~`Kh6xt|-)uo0eHPr{M4F zU*ye2S}Gh$nwF6&W>*YnA9y*2BY zwqq8y=48*@nwh_p%@wxXOg9xmj*-vVR$g#$TQ^@wuil?d=gOfq!*U}j9PZl-Kb*)H zife{dNX&clm_XJH^5mgH=j#p0OoZT6FtRyj&pF1XX%~q;kh3!xh1S0QJ@@~M<|k0G zGK~97wr1vYnKW5w>qcSTm66I($jH21bXJW?Q`BmHiT{av>CLo`E_CZ?N1KjT89LBO zVGmYH4EuES;%yx>nAI?+<0^)9%p;*=0oODv>bQ;@ER-v6&oyh!a3(YMLk&whZep2u zCOo7)qdhuqVOfUTxqSoG`j99^39GOcyDvx2V0OZT1(yvJL7t}T4ke)y!GT=}H9 z?#$A14naxHIUeRN#e2oMIra=Hrk=tlgFohfionmV*BcyL`QU*D1ki{uqNqm`Kh1cP zr@H5l9_9wu?2vNoCC)yc)%nL`zasDi!N7Rek38^0f<_q*1m|PVg$b>epd?_c=Tr{p z&qt8pD&NH6C`{s89mj<4pW}4Z2srYChR)Y2|I%9*vh=VV- zL>z%n$ZZ=R$4dy65FX&~dqm?qsHY1LBdP5d|8@i<$Peg>dvnvE%q7&iakf$45g6od zkoL9lKN0eBRKkmN9?KMk0!0Z^mU=R2B$sw_IZOt<_{R94AYc~3P5l<47dUUMZq?pf@2 zQPo}TFVLfPCDaf6RMF-%>bRa_!X&Xz6J3hh%uuOWDlXPM3mn>1YHj)sQ5%_m8Op09^Pfk|IZIsD%?vaa1%XoxBY@B2jHm^I^h|M6jyTr9o zmAA8!x1BZJ%gd*=i+1K^)^{sU>^sH~Nk>G1LnSo0Dh=%52oKnOY{EuPRM^!hN( zK&RMhUiGC56f*<~F(5D1=e|_FI<6nPVZG-bEQjS^R7KrtXTKD=+WCuDIuE;6r~d;T Crjtnk delta 131 zcmeAY+`uGy>ff$?3=9nB3<6vX%nZEj41DYi{1aznPkzj(!p6eD%E+KF@qxnR?M%YK z42ld)Ks6x12!udMAjt;g>4W(U46It)85lPL1(+Dvfg~GHn1O+lfdfeLfZ41-9v6_u L$iNLGnHYEgZM_aM diff --git a/out/production/assignment-2-Gopal868484/problem3/node/Node.class b/out/production/assignment-2-Gopal868484/problem3/node/Node.class index d2b4e7de92b2e1852020acc01bfed4853301be0a..0f804a936c41764950beee91adeb64d6a912035e 100644 GIT binary patch literal 813 zcmZ{h%TB^T7=_P3X>F~5R}@j>4luC`V_az5n7AUiz_2ek(Gn;Iit({Dk(jvf0emRq znQ6s(%Oum8={e{B{+YM;msbEs*m01%=1C9+(W$^z^;7UWq}FE484Uu3Mi9#8WYCeL zOP?hLmKwvZ-*5Y)K<$%5Bf1I30?xIJ5)y$*wf27rYR4(9MPpNQuYI|`lUvlslF|%4 ztx(WTYd9Ho<$0h$ZPoIA&%g6sIM{Y!!g4VOn>{4=m+}h~mDKC|;k9?s>B%lHFFh59o@eQ%0PT}+XE)P@DlOAG>y22D0?dD`+k zS)-9Q4i&0as*K<$VlfH#^qwW_Pslt_l!1ygd8`a7#?|Xw62m5EWTy$4sj0qz99?*s yeXOKF%o&I|16igb?lKLM{RvX~6J(Vg#^!Gzshf#GHVIPs2Si=Cx{;D@ef*2$!F>ff$?3=9nB3<6vX%nZEj41DYi{EQ52Hko;uC3cJq%o-EBEhqYm%Wwtf zmlh?bx@G31GBR-b<)@_TWhIs+@-VP4uujfm)Cgu!WMBds0RoIb2owgAY(Snqn9sn# qsF~>lHu% diff --git a/out/production/assignment-2-Gopal868484/problem5/student/Student.class b/out/production/assignment-2-Gopal868484/problem5/student/Student.class index 65dd57331c633a0782546d227b4f1524af8e148e..fa33c6a4ed4cf7eba04833a1785d5ce9ba625ad2 100644 GIT binary patch literal 1927 zcmaJ>U2hXd6uo1wKQVR^ND8Kb1VV5O#DxNZngWd_R7-9uktnF@)5IAiOEx>!?ih(T zenx);4h7bNAkJ&OLY5U%!3%kBBztMVX4UTA~8o;-$gM z8ZXT`yk9R+t3W>q$_Lu>wzMG873BExSP7czy^(Ka%ai^=Yuf~_f6x|G-IM*Jo<8X4 zp>I@xfE)~z_?8rUOGU~qcc7ev{ zIOv!|HxyLw4FZkm@xxYVhWpAl5M_6$x|BxM?J`;EK$%x@bJ@lk+AD?Qh|{o11te|R zrY|`Krey-31dI-nPaub!~ zvH!XEM)i##7`I!dN3?mm6HBKB2_-|kikOW8v z#U&J10eS(ErKpT9(mYN8u(ZNHF>%JyEW|YLan9jg{WMK;@r0b_ymLaiF=L(b$UG0m zDlltUXTUg1m!XB3A4Eta=K6@aZd=YU58rmHu+CQ=Sh$vSDOS_M&PsF5&y@LqhYX+z zv>cqbB$X&3CJ`X7Bf^e7LpD?7ty^Q+WgEQ=adnHZ5M_;I=3|+HuF#J$R#HgzBS4cv z7E^^R($y&;w@*zh60!^-*Uu2b^I-?Bjf6Y~GdEv_BUyIk3lCvZ(P~0bb!5#_s;|zZ zugyepZdAOS?yEED>pI$LozV`Dn%lXaDkL{4ff$?3=9nB3<6vX%nZEj41DYi{EQ52Hko;uC3cJq%o-DChD|=pD8tOc zz&cr;NiUc|k%0-Q6a*N75GV{J*?>HKFrR^eRcku~<3^wW69YStWCIE_FmN((07)J& Sn-$380`eFcxPc@S0}lY2hYlYA diff --git a/src/problem3/main/MyMain.java b/src/problem3/main/MyMain.java index c719a1a..bb5f79d 100644 --- a/src/problem3/main/MyMain.java +++ b/src/problem3/main/MyMain.java @@ -1,11 +1,42 @@ -/* - * Created by IntelliJ IDEA. - * User: Vaibhav - * Date: 23-Mar-20 - * Time: 7:13 PM - */ package problem3.main; // executable class // use problem5.student.Student class to create object of student + +import problem3.myqueue.MyPriorityQueue; +import problem5.student.Student; + public class MyMain { -} + public static void main(String[] args) { + MyPriorityQueue queue = new MyPriorityQueue(); + queue.enqueue(new Student(7, "Virus Ninja")); + queue.display(); + //System.out.println(queue.getSize()); + //System.out.println(queue.peek().getStudent() + " element at front"); + queue.enqueue(new Student(2, "Peter Maximoff")); + queue.display(); + queue.enqueue(new Student(3, "Matt Murdock")); + queue.display(); + queue.enqueue(new Student(5, "Sam Fisher")); + queue.display(); + queue.enqueue(new Student(1, "Solid Snake")); + queue.display(); + queue.enqueue(new Student(6, "Erik Lehnsherr")); + queue.display(); + queue.enqueue(new Student(4, "Bruce Banner")); + queue.display(); + System.out.println(queue.poll().getStudent() + " polled"); + queue.display(); + //System.out.println(queue.rear().getStudent() + " is rear"); + System.out.println(queue.poll().getStudent() + " polled"); + queue.display(); + //System.out.println(queue.rear().getStudent() + " is rear"); + System.out.println(queue.poll().getStudent() + " polled"); + queue.display(); + //System.out.println(queue.rear().getStudent() + " is rear"); + System.out.println(queue.poll().getStudent() + " polled"); + queue.display(); + //System.out.println(queue.rear().getStudent() + " is rear"); + System.out.println("The Size Queue is = " + queue.getSize()); + + } +} \ No newline at end of file diff --git a/src/problem3/myqueue/MyPriorityQueue.java b/src/problem3/myqueue/MyPriorityQueue.java index 5426bfa..f4efcf8 100644 --- a/src/problem3/myqueue/MyPriorityQueue.java +++ b/src/problem3/myqueue/MyPriorityQueue.java @@ -1,10 +1,123 @@ -/* - * Created by IntelliJ IDEA. - * User: Vaibhav - * Date: 23-Mar-20 - * Time: 7:15 PM - */ package problem3.myqueue; +import problem3.node.Node; +import problem5.student.Student; + public class MyPriorityQueue { + // a field to refer rear end of queue + private Node rear; + // a field to refer front end of queue + private Node front; + private int size;// total number of elements in the queue + + // constructor + public MyPriorityQueue() { + front = null; + rear = null; + size = 0; + } + + // a method to check whether queue is empty + public boolean isEmpty() { + boolean response = false; + if (size == 0) { + response = true; + } + return response; + } + + + // a method to add a new element in queue + public void enqueue(Student element) { + Node node = new Node(element); + Node temp = front; + Node previous = temp; + if (isEmpty()) { + rear = node; + front = node; + size++; + } else { + while (temp.getStudent().getRollNo() < element.getRollNo() && temp != null) { + previous = temp; + temp = temp.getNext(); + } + if (previous == front && temp == front) { + node.setNext(front); + front = node; + } else { + previous.setNext(node); + node.setNext(temp); + } + + if (temp == null) { + rear = node; + } + size++; + } +// if (isEmpty()) { +// rear = node; +// front = node; +// size++; +// } else { +// rear.setNext(node); +// rear = node; +// size++; +// } + //sortQueue(); + } + + + public Node poll() { + Node res = null; + if (!isEmpty()) { + if (front.getNext() != null) { + res = front; + front = front.getNext(); + res.setNext(null); + size--; + } else { + res = front; + //res.getNext(null);//redundant + front = null; + rear = null; + size--; + } + } else { + System.out.println("Queue UnderFlow"); + } + return res; + + } + + // a method to get front element without removing it + public Node peek() { + Node response = null; + if (!isEmpty()) { + response = new Node(front.getStudent()); + } + return response; + } + + + public void display() { + System.out.print("Display : "); + Node temp = front; + while (temp != null) { + System.out.print(temp.getStudent()); + if (temp.getNext() != null) { + System.out.print("<---"); + } + temp = temp.getNext(); + } + System.out.println(); + } + + // a method to get size of queue + public int getSize() { + return size; + } + + public Node rear() { + return rear; + } } diff --git a/src/problem3/node/Node.java b/src/problem3/node/Node.java index 277d7ec..e9dbd18 100644 --- a/src/problem3/node/Node.java +++ b/src/problem3/node/Node.java @@ -1,10 +1,29 @@ -/* - * Created by IntelliJ IDEA. - * User: Vaibhav - * Date: 23-Mar-20 - * Time: 7:13 PM - */ package problem3.node; +import problem5.student.Student; + public class Node { -} + private Student student; + private Node next; + + public Node(Student student) { + this.student = student; + next = null; + } + + public Student getStudent() { + return student; + } + + public void setStudent(Student student) { + this.student = student; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } +} \ No newline at end of file diff --git a/src/problem5/student/Student.java b/src/problem5/student/Student.java index 4d11bed..7ff79b3 100644 --- a/src/problem5/student/Student.java +++ b/src/problem5/student/Student.java @@ -1,10 +1,57 @@ -/* - * Created by IntelliJ IDEA. - * User: Vaibhav - * Date: 23-Mar-20 - * Time: 7:06 PM - */ package problem5.student; // to store student information and properties public class Student { -} + private int rollNo; + private String name; + private int backLogCounter; + private int appearingCount; + + public Student(int rollNo, String name) { + this.rollNo = rollNo; + this.name = name; + } + + public Student(int rollNo, String name, int backLogCounter, int appearingCount) { + this.rollNo = rollNo; + this.name = name; + this.backLogCounter = backLogCounter; + this.appearingCount = appearingCount; + } + + public int getRollNo() { + return rollNo; + } + + public void setRollNo(int rollNo) { + this.rollNo = rollNo; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return name + " #" + rollNo; + } + + public int getBackLogCounter() { + return backLogCounter; + } + + public void setBackLogCounter(int backLogCounter) { + this.backLogCounter = backLogCounter; + } + + public int getAppearingCount() { + return appearingCount; + } + + public void setAppearingCount(int appearingCount) { + this.appearingCount = appearingCount; + } +} \ No newline at end of file From 717640b421efa4ef742ea0e7785951538fc6bcdf Mon Sep 17 00:00:00 2001 From: Gopal868484 Date: Sat, 28 Mar 2020 12:57:12 +0530 Subject: [PATCH 5/6] problem 4 completed , yeah !! --- .../problem3/main/MyMain.class | Bin 1847 -> 1891 bytes .../problem3/myqueue/MyPriorityQueue.class | Bin 2186 -> 2186 bytes .../problem4/main/MyMain.class | Bin 271 -> 1032 bytes .../problem4/myqueue/MyQueue.class | Bin 280 -> 2449 bytes .../problem5/student/Student.class | Bin 1927 -> 1927 bytes src/problem4/main/MyMain.java | 30 +++-- src/problem4/myqueue/MyQueue.java | 116 ++++++++++++++++-- src/problem4/node/Node.java | 27 ++++ 8 files changed, 159 insertions(+), 14 deletions(-) create mode 100644 src/problem4/node/Node.java diff --git a/out/production/assignment-2-Gopal868484/problem3/main/MyMain.class b/out/production/assignment-2-Gopal868484/problem3/main/MyMain.class index 4caa548a5902aa5e2d86fc1c4b35ecff6d3f74a1..5ff2a2000aa07ece5fb4f541b176ee618e2739db 100644 GIT binary patch delta 122 zcmWN|Jqm#V007YU4J73MpNL2XCotHR!3hkC-QWZUyUpMXpC~slIDhzg}WNuvx1kDIujQtto3HXDn}`V5(@YWT9-SVil>anqBnv8V=DoX*p{Pb=-74 R^gQ*w47?5fjE>*<><_#C6zSm%lj?%3psEnYFavrV5I9~2mJXurpQ&5`)WB9`^BB3f0< znm8x&owCCjdz_1Sp{-7XOZyY-c-KnZD0Qnu(;D|Pc~6KI9uI2OCQe6<9@XQSEM1Dc V*gv-(c(W$78|Z1yxH%9d{|`8GCJz7r delta 255 zcmWNL%Su9F6oo(IW`Fe5QPwuT0xjWHOdgg%8U{cq?y?8e~~5) zdX7f*G%Yr4Sev!JwMYKQ|1^$2frC$&NutCwQ})m)dU3{>(KSJYq_xg(DJ?ZGPe$$q z;w&=95^0uMBg+A+6vb&{vPl($PmxtnkbRFY0}@ Ue{L=CW=&`>FwmZM^B_v$AC$i)?*IS* diff --git a/out/production/assignment-2-Gopal868484/problem4/main/MyMain.class b/out/production/assignment-2-Gopal868484/problem4/main/MyMain.class index c5d2f962de4d5a8f8a114faf2b6910f08b626098..f79474d5f79ea49491353d7482589492c0481f8a 100644 GIT binary patch literal 1032 zcmZ`&VNcUg6g?e-ZllT^3=tKFC>A!UASwdk113u}Ly#>P#V_4>fs(Do*N&t=OFxW6 z5`Ta{z@KA0?~OT0U?0xwz4!FKbIg~a1#X+(-|aCQ1CE~89YkiaR#$^ zqB6FLxeU^(oW{I@qKT4;rv`@BJ>QEq3=G(Fdj^tkf&*b-q~iJFeY;f`vgX#C%;YLT z!)@-l(o^w8G8r9tp@H$ri42%+EjumO^PR1Di^m$fsgQMhzjExJx=z#e51ncxJ^ye` z-{#6gzCU_JnaWymBn7XY>9t>UT5&y$*niXWT^Uz}D;q~Ol6cqP*S2VrJ=tSZOssR{ ze^51&GS#3h8=~x~k%qfE7nC#$Bgh(s2SOHV(mfS2 zben}$ZsWOyWxTNPQgxl^+jCiw#r>^Vc&$ideNx{;1KGZD+x277h-i%GheGlpX}?u2 z{zs|CVI*2yX3%Cm-*w#!oE=&o(Rksu=)$P(c{y#sJW-@3_m#J=URO5i&M10AbG2a< z$bWIQ-Drp~45WcsySInyz0}`;dEkX7O*h6gZtzQ1zXbRNB1^o*$l+=59GpLc@lBU= z#37wDG0JDFOTZZNjM5m#ZD!QVzu9bn=a+LNKAmBpQp_%OkSu1WG>&PsHI_B5YuwTJ zS!0Iy9fP@{4$NW)sdJ>+DYI2#FkI@u>TBAhXmx)I>Q6aLJr*W8R)NthM=G(tL|rS$ uVS~!cWW1y1T`K-a&HI?p^Ax(88Qo#O6l&~pmlN%>?>*et22HV&z=OYy$Lj|G delta 148 zcmeC+=w}i=^>5cc1_lOm1_3SxW(Hn%20nHM{)uk!a$LdrrA5i9Zkai$j11hqmA;9Y zd3sriWr;itEDWrZ&ogQTGbl1J0X2aDBM<_Gfg~G{rw`^cFtBQEXJFh26kuXt2a;?+ bVFm^+1`Z&}17@=_a02-t?c6|;iGc?I+58gF diff --git a/out/production/assignment-2-Gopal868484/problem4/myqueue/MyQueue.class b/out/production/assignment-2-Gopal868484/problem4/myqueue/MyQueue.class index fcb8537e021ebbd1f8fad6a67293f89fdc6f5905..3dcfd5f28df30f9ff18453ceb352456a6f68c92d 100644 GIT binary patch literal 2449 zcma)7TT@e46#h;woLo)>xkMU3pIBmbR&moD{&U7-f_da{CwZ3nCYwexi|N7-O0OwFiAcL!M ze1vNp&*7qh=(^{YYqY95-zfve ztbw)(r|i@w4TMLIFZ1LYEe*8iowAzWDXl8+j?Fj*I`eMP-d?snN4|s2NNv-p8pt%! zO1n>Y)Q-x{?Jmkp%yhNpdTPN_x4kvx6?Te6Rjs<@cx+_P?NqL`yIQT$>8ew=6MX4uZ?^G+>HUT3wRjP1fL)8kpn5-YmnJBnBUQx|BQZp;4!pSZBYumyghAa%A z-$I`Z2XV;6BMXngvoV}Bv1*}+DGR4@#=@FxE7&akzk5l7(dHAJvapT~3!4}>;aJ!L zS+?ui58f@a;4|CqHzu|%lu+i$w9kJ~TU;2l;G!(U8D=ywHZ~@9Lo8nd^1X&K&>_j> zw(asp?)K`IDzex^f%Sj)kuTJeZjDS+=~MpgXlaggBh)+9_x-2s8s!kl^J<;AF|L-J zjZH4is;bSaZ@D_q=%Cg*>gvn9Q&~ko#^842zM>tA4hSFUj~LT62x9) z>@fF|qjf~*vM1_jYlhsXk0kH`QtH*A27U7&6diFKSq6)u4Wl^0aFF&5!f)|gb_Pb@ zpf5`dvyPa|aRSMl2mr%q=Fra^2FUp!x~WD#b&wp=exL^MfQB=W<5?I+KM{EW0^AkO zf}09YWM3lk4AIC$-wSOiIGscQwFps(IJHPJg(gNSzzA{wQ+&oyg3$)!Es}gn44X)%d2}A*0rNiy zC;~k+@DhEUdQ*K{7kzD2VWTh5X{qoZ?w9i5{;HnKX4 z6xWvsI7xO>Y|6{*rYkJ@Rf>O&a?UWP8$6lCG;U&%Ex&?=mXbVdlw^gLQfnzm0^zqf z!`%8z?p$mnw;3dt*-Nfd>flV jV~cZL(qa84BN~L|UvWd-R_Cx3xz+jU2c0i#t1JHi*tx)$ delta 125 zcmbOzJcCK})W2Q(7#JAL83ec(m>GE48Ti;4_$O{mp4`MF!_30KI@z6BFPK4*fe9!J z0*pWi6b6!PK%PFB&%nT{wVi=+BT#^efgMP)0fiYDxEMHqBoCO)%D@TagS2x4NhSs! E09^SC!~g&Q diff --git a/out/production/assignment-2-Gopal868484/problem5/student/Student.class b/out/production/assignment-2-Gopal868484/problem5/student/Student.class index fa33c6a4ed4cf7eba04833a1785d5ce9ba625ad2..3c6e41566f4ce13ed93cdeda246e18ec4ae030ec 100644 GIT binary patch delta 115 zcmZqYZ|C2zj75}>frWvefs;Xifsa9uL6|{^L6Sju@@|$CMzP6^?4pxRSv6Rt7+4vk zCnvGGG0K6(_5sC|fMUv%U$go%s!z6NvtiT*%k;8ou<8S43?}blb7M3HirKQ71Njz{ Ho!MOgaHSNl delta 115 zcmZqYZ|C2zj75}}frWvOfs=tBND43rGYB$BG6+rH&62_>I+>AObh0U{2CF0kD}&VJ zBvv;@S+LkXpqL_1Olk6KR)0pd$<}N(j9OrsUN#L@J)n&K Date: Sat, 28 Mar 2020 13:02:13 +0530 Subject: [PATCH 6/6] yeah!! final commit --- .../problem4/main/MyMain.class | Bin 1032 -> 1032 bytes .../problem4/myqueue/MyQueue.class | Bin 2449 -> 2449 bytes .../circularqueue/MyCircularQueue.class | Bin 316 -> 3116 bytes .../problem5/main/MyMain.class | Bin 271 -> 1315 bytes .../problem5/node/Node.class | Bin 265 -> 832 bytes .../circularqueue/MyCircularQueue.java | 123 +++++++++++++++++- src/problem5/main/MyMain.java | 35 ++++- src/problem5/node/Node.java | 35 ++++- 8 files changed, 172 insertions(+), 21 deletions(-) diff --git a/out/production/assignment-2-Gopal868484/problem4/main/MyMain.class b/out/production/assignment-2-Gopal868484/problem4/main/MyMain.class index f79474d5f79ea49491353d7482589492c0481f8a..2b91ec07f42699f7f95e2471c9e59c639457c70c 100644 GIT binary patch delta 79 zcmWN`D;7XN7=Y2wnc#Q4-%OGu!I2cAAS!|lO`nZej^&tg%Qa%;^_3S`QFaRvA`)UM frzT|}VaME(oantjl?Wg7+XKM)y delta 79 zcmeC+=-}AE&dkU@nS*(Px# diff --git a/out/production/assignment-2-Gopal868484/problem4/myqueue/MyQueue.class b/out/production/assignment-2-Gopal868484/problem4/myqueue/MyQueue.class index 3dcfd5f28df30f9ff18453ceb352456a6f68c92d..96617a1090940a325c6e275107dd68c8c1e132fe 100644 GIT binary patch delta 125 zcmbOzJW+VVYIadq1}g@4273k%1{VfThCl`%hDZj#$p_f8c|#bO7(y918NwJO7$PQH zaC9ifGH@`&G4L|PGYB#yFi0~bGAJ=5F=#QQFc>kUF<3C9PkzV|#+W_Xg40GepFxFz Zfq{pC5lFH!s4}PlX+{PIAj!nw2mrrP6Y2l} delta 125 zcmbOzJW+VVYIadK1}g>+273li1{Ve|hCl{ChDe5h$p_f8dBYf(7{VDi86p@Y7@{Uy zaC9ifGjK2@Fz_-YG6*sxF-S8cGbk~nFlaHPF&HsqFjz2TPJYM{#+WnrzzBrsss56QFq4 zKcUX}Cc_I4>S&z786S1VpWuTJ{*HS2);WhHwjHL^wQpKZ&9Kv}q7sO16xtQeYYweiC>uK(uPT~?Sr=f7gz^rV#Dh_W- z@0ytF3A~lSoVeW(b2EXrrG3Z1y9VA5o*`gL=P0BLPSGr1Gpm-2!^xPp zXt@f5&1gldTCA1L>YbXd>D<(6J{-@;m&T%}~ ztD2Q5?JYVbx2p}y36xV5X|I(T}B&!%zwyYAM{s30{YYl_>iZUTB>D%se1!p`9z6_CoH`{E{wG_rAv7 zb@#)_qG*r^3cFfwWt}V2qno~GD-~9OWiRBNnp~;Oonz7zoM1+?Lb~0JezCzx$pu+j z@7@AsaQ_#G9wZu3vgvDiB&vMIH(S~89A!#EV!9`nW%IrGpI5F zLepj`3=IqnC~RLbmv!E;i>7zo@)p^1J=3p`+%qzFawv%I3(3%=ObL26LBzDI2)?pS z$b%bq3GWwG=5kP-1UZ_yqE}454faBl!8+ubQyHaj_*noqM`%#tX?>saZExA!EHS?# zd1--9+(*6+caSz?L)Q$vFS-4(!v1DbS@vCLS@YEhvOr{=FpHj3T{ZA2AH9O(EZ118 zsY%<`)qL4>T`B1=T9paU2+Ha;(=L^@yFb`Cwo$;Ang4CPx?0hjxZiB?e@dlgDm)@@ zmdmr2r^gI@rf^^bzs;~I?0L2y%e|kUC?JZ6JU8q`qZaKIKbIdupckrAV>UQU?@EM zPx?nWr?WpGQW&lyJ~dKD8~=@O&@m!y9SOgc`BEM2GWRtGvUMb{e;@d?Jz)sc(aBR{ zUtve|@zVuyenKp=7<Q6KCfbg;jTkb9BDcJ}%xKtHk+<_JYO!O;XoxkMR?6hTO2 z0-zy@IBwBr2b-H(QZi< zgw0pD5uodl<{=JCQ^g0&7NPI@boW%%0$*^`n#q!Yob z1bde0a*ipI=T)62*$bpQ!QLeQ-e9d1FpDWB#|#$PYhK_|;{q4CLlQ^QC5h-7&Xev= zgEKET_%4Qg`=9bn%WVey;=I#B3%($6=7{eG@!e!9zta-8zY({OiSsmWLK3HsaT)cM zMy3D4GeqSXQDIvhy}nYzw9$vy&q*oJI<~Xv`4zraee{xH;nBZ;h~KZ+LOQtXorA{~gWp_4>1 zhE&@alP!3)a%|<*iW7@p`N1>Ais59{KR0-m;^DdcY0;3sb#qiQp>^}!O*TgfTD(r< bBeqEf`2e5bL+oHq$g3D(UMS{;*qQ$T2k4s{ delta 131 zcmZ1@v4=_Y)W2Q(7#JAL83ec(m>GE48Ti;4_$O{FoGitp!p6eD%E+KL@t@jc9cJNR z21N!Ypc)Wh1VW%BkYoe$^uc@v23D=@42&Cr0!$3-K#~n8%)r3OzyTzAz-(3^j|<3S LWZ(vpObk2#C{Yb# diff --git a/out/production/assignment-2-Gopal868484/problem5/main/MyMain.class b/out/production/assignment-2-Gopal868484/problem5/main/MyMain.class index c0fefe533486c90dcf3aedb5e69955ac2e0853cd..a5ee8e2b6c574d333f9255213a7b4283a4b40ecb 100644 GIT binary patch literal 1315 zcmah|OHzdRkRK>=*CMMYM@PO9*pe#8@iK? zHkWn7anE@OgYlM=2=xFH}qXj zAV8e93xELbQyXXQc(^n0#LcMel{_3h2yK9GlScs9t8KrDxZ( zWG3h~z9l~KEVIJam|kL5QO|jfX%#1eXv`^+e_I2ZoE>Hrc;LX*_so+1m=L>67e`+D zhEG`~{zT53PTnsuCtxUr>H7W=KS?glIgjO6v#e6jUCG(LlQ(8fUR_fyQgxe~naYsPcZXmbjNumWe zP~~f;ltO5F54%fqt;8)*5_#kJemZ9 z@piTaTJMLcj0!(gq513LOOQa8j~g>YsYBH2jxGoqSQg*>>8*rKQ@k%34RP2%${ zs8uu%R&gX?B3g^on2BofXk$jwTII%!tZCsx49}#EXHrG`8${p#`p%>CLc`#Nj!=1^ zGGr<&4uy1S3a|{Cv5X^ljTXEmW&^GGNE&`36}c!qwZh5v2L2y#24_i@INsqL&XdZ0 VgsTYsz=nj71TjYJ1;Ro|{00znFFODL delta 125 zcmZ3?)z2h)>ff$?3=9nB3<6vX%nZEj41DYi{1X%6Cr2|%GqW(TPVQpT3T9AbU;@g5 z03#3rg@Gg+kf#skGcd4fZD(NI2ozvqU!o_p?h@ATvI?H#}|_HCrFl|~ZVYA9LQv9K$U?0BIkATGEJ^?uvyHobwb_R(Q7y!D3y_KggqTmswW%KQ}64r5x2fel?=J|6Y6z;DS$ z41K0h(Dqe-G-%6nUxB7oXAbYY2hV|nJqI=x9V{Tt8veFWU`0vYt{2?6m#sV5W-Alr zN)!8hj}#v%0Rtv1t_70!oMIJ*o=aIfW$g80Vxq=XAX#Jyy{3nmtOn= DI_rmn delta 141 zcmX@W*2$!F>ff$?3=9nB3<6vX%nZEj41DYi{EQ52Hko;uC3cJq%o-D?Sxz(-pX|>l z$;`sQI=PflBbY&vfeEM@1Q>x3C=4XofINLLpMil@YdZtuMxX!_13Qpp0}3-Ra58WJ VNggnp70BZP@)#Mofg}?H4*>eL4#@xj diff --git a/src/problem5/circularqueue/MyCircularQueue.java b/src/problem5/circularqueue/MyCircularQueue.java index 23ec7ea..5ac0cb7 100644 --- a/src/problem5/circularqueue/MyCircularQueue.java +++ b/src/problem5/circularqueue/MyCircularQueue.java @@ -1,10 +1,119 @@ -/* - * Created by IntelliJ IDEA. - * User: Vaibhav - * Date: 23-Mar-20 - * Time: 7:06 PM - */ package problem5.circularqueue; //to implement circular queue + +import problem5.node.Node; +import problem5.student.Student; + public class MyCircularQueue { -} + // a field to refer rear end of queue + private Node rear; + // a field to refer front end of queue + private Node front; + private int size;// total number of elements in the queue + + // constructor + public MyCircularQueue() { + front = null; + rear = null; + size = 0; + } + + public boolean processStudents() { + Node node = front; + Student stu = node.getStudent(); + if (stu.getAppearingCount() > stu.getBackLogCounter()) { + stu.setAppearingCount(stu.getBackLogCounter()); + } + stu.setBackLogCounter(stu.getBackLogCounter() - stu.getAppearingCount()); + if (stu.getBackLogCounter() <= 0) { + dequeue(); + System.out.println(stu + "'s Backlogs Cleared (" + stu.getAppearingCount() + " Appearances)!!!"); + if (size == 0) return false; + } else { + System.out.println(stu + " Processed - Remaining Backlogs (" + stu.getAppearingCount() + " Appearances) = " + stu.getBackLogCounter()); + } + return true; + } + + // a method to check whether queue is empty + public boolean isEmpty() { + boolean response = false; + if (size == 0) { + response = true; + } + return response; + } + + // a method to add a new element in queue + public void enqueue(Student element) { + Node node = new Node(element); + if (isEmpty()) { + rear = node; + front = node; + rear.setNext(front); + size++; + } else { + rear.setNext(node); + rear = node; + node.setNext(front); + size++; + } + } + + public Node dequeue() { + Node res = null; + if (!isEmpty()) { + if (front.getNext() != front) { + res = front; + front = front.getNext(); + rear.setNext(front); + res.setNext(null); + size--; + } else { + res = front; + //res.getNext(null);//redundant + front = null; + rear = null; + size--; + } + } else { + System.out.println("Queue UnderFlow"); + } + return res; + + } + + // a method to get front element without removing it + public Node peek() { + Node response = null; + if (!isEmpty()) { + response = new Node(front.getStudent()); + } + return response; + } + + + public void display() { + System.out.print("Display : "); + Node temp = front; + while (temp != rear) { + System.out.print(temp.getStudent()); + if (temp.getNext() != null) { + System.out.print("<---"); + } + temp = temp.getNext(); + } + if (rear != null) + System.out.print(rear.getStudent()); + System.out.println(); + } + + // a method to get size of queue + public int getSize() { + return size; + } + + public Node rear() { + return rear; + } +} \ No newline at end of file diff --git a/src/problem5/main/MyMain.java b/src/problem5/main/MyMain.java index 9bdcc20..a3f1072 100644 --- a/src/problem5/main/MyMain.java +++ b/src/problem5/main/MyMain.java @@ -1,10 +1,31 @@ -/* - * Created by IntelliJ IDEA. - * User: Vaibhav - * Date: 23-Mar-20 - * Time: 7:06 PM - */ package problem5.main; //executable class + +import problem5.circularqueue.MyCircularQueue; +import problem5.student.Student; + +import java.util.Random; + public class MyMain { -} + public static void main(String[] args) { + Random rand = new Random(); + MyCircularQueue queue = new MyCircularQueue(); + System.out.println("Adding Students :\n---------------------------"); + queue.enqueue(new Student(1, "virus", 2, rand.nextInt(2) + 1)); + queue.display(); + queue.enqueue(new Student(2, "ninja", 4, rand.nextInt(2) + 1)); + queue.display(); + queue.enqueue(new Student(3, "Magneto", 3, rand.nextInt(2) + 1)); + queue.display(); + queue.enqueue(new Student(4, "Hulk", 5, rand.nextInt(2) + 1)); + queue.display(); + queue.enqueue(new Student(5, "Professor", 0, rand.nextInt(2) + 1)); + queue.display(); + System.out.println("Processing Elements :\n---------------------------"); + while (queue.processStudents() != false) { + //Processing Students Until Queue is empty + //EveryProcess the student clears 1-2 backlogs + } + queue.display(); + } +} \ No newline at end of file diff --git a/src/problem5/node/Node.java b/src/problem5/node/Node.java index 64fd972..5b3489b 100644 --- a/src/problem5/node/Node.java +++ b/src/problem5/node/Node.java @@ -1,10 +1,31 @@ -/* - * Created by IntelliJ IDEA. - * User: Vaibhav - * Date: 23-Mar-20 - * Time: 7:06 PM - */ package problem5.node; + +import problem5.student.Student; + // to define node properties public class Node { -} + private int data; + private Node next; + private Student student; + + public Node(Student student) { + this.student = student; + next = null; + } + + public Student getStudent() { + return student; + } + + public void setStudent(Student student) { + this.student = student; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } +} \ No newline at end of file