prateeksharma

data_structure_assignment_2

Mar 29th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.59 KB | None | 0 0
  1. package problem1.node;
  2.  
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5.  
  6. public class TreeNode {
  7. private TreeNode left;
  8. private TreeNode right;
  9. private int data;
  10.  
  11. //default constructor
  12. public TreeNode() {
  13. System.out.print("Enter integer value : ");
  14. try {
  15. data = new Scanner(System.in).nextInt();
  16. } catch (InputMismatchException e) {
  17. System.out.println("Invalid input");
  18. e.getMessage();
  19. System.exit(-1);
  20. }
  21. left = right = null;
  22.  
  23. }
  24.  
  25. public TreeNode getLeft() {
  26. return left;
  27. }
  28.  
  29. public void setLeft(TreeNode left) {
  30. this.left = left;
  31. }
  32.  
  33. public TreeNode getRight() {
  34. return right;
  35. }
  36.  
  37. public void setRight(TreeNode right) {
  38. this.right = right;
  39. }
  40.  
  41. public int getData() {
  42. return data;
  43. }
  44.  
  45. public void setData(int data) {
  46. this.data = data;
  47. }
  48. }
  49.  
  50.  
  51.  
  52. package problem1.mybst;
  53.  
  54.  
  55. import problem1.node.TreeNode;
  56. import problem4.myqueue.MyQueue;
  57. import problem4.myqueue.Node;
  58.  
  59.  
  60. // to implement BinarySearchTree
  61. public class MyBinarySearchTree {
  62.  
  63.  
  64. private TreeNode newnode, root, tmp;
  65. private MyQueue queue;
  66. private int totalInsertion;
  67.  
  68.  
  69. public MyBinarySearchTree() {
  70. tmp = null;
  71. root = null;
  72. totalInsertion = 0;
  73. queue = new MyQueue();
  74.  
  75. }
  76.  
  77. //seeting root node
  78. public void setRoot() {
  79. newnode = new TreeNode();
  80. if (root == null) {
  81. root = newnode;
  82. tmp = root;
  83. newnode = null;
  84. }
  85. }
  86.  
  87. public TreeNode getRoot() {
  88. return root;
  89. }
  90.  
  91.  
  92. public int getTotalInsertion() {
  93. return totalInsertion;
  94. }
  95.  
  96. public void setTotalInsertion(int totalInsertion) {
  97. this.totalInsertion = totalInsertion;
  98. }
  99.  
  100. public TreeNode getNewnode() {
  101. return newnode;
  102. }
  103.  
  104. public void setNewnode(TreeNode newnode) {
  105. this.newnode = newnode;
  106. }
  107.  
  108. public TreeNode getTmp() {
  109. return tmp;
  110. }
  111.  
  112. public void setTmp(TreeNode tmp) {
  113. this.tmp = tmp;
  114. }
  115.  
  116.  
  117. public MyQueue getQueue() {
  118. return queue;
  119. }
  120.  
  121. public void setQueue(MyQueue queue) {
  122. this.queue = queue;
  123. }
  124.  
  125. //setting binary tree
  126. public void insert(TreeNode tmproot) {
  127. if (newnode == null) {
  128. newnode = new TreeNode();
  129. ++totalInsertion;
  130. }
  131.  
  132. try {
  133. if (newnode.getData() <= tmproot.getData()) {
  134. System.out.println("left traversal...");
  135. if (tmproot.getLeft() == null) {
  136. tmproot.setLeft(newnode);
  137. System.out.println("node inserted left");
  138. queue.enqueue(new Node(newnode));//only left insertion
  139. newnode = null;
  140. return;
  141. } else {
  142. System.out.println("left not empty changing tmproot ");
  143. insert(tmproot.getLeft());
  144. }
  145. }
  146. } catch (NullPointerException ignore) {
  147. }
  148.  
  149.  
  150. try {
  151. if (newnode.getData() > tmproot.getData()) {
  152. System.out.println("Right traversal...");
  153. if (tmproot.getRight() == null) {
  154. tmproot.setRight(newnode);
  155. System.out.println("Node inserted right");
  156. newnode = null;
  157. } else {
  158. System.out.println("Right not empty changing tmproot");
  159. insert(tmproot.getRight());
  160. }
  161. }
  162. } catch (NullPointerException ignore) {
  163. }
  164.  
  165. }
  166.  
  167. //preorder
  168. public void preOrder(TreeNode node) {
  169. if (node == null) {
  170. return;
  171. }
  172.  
  173.  
  174. preOrder(node.getLeft());
  175. preOrder(node.getRight());
  176. }
  177.  
  178. //postorder
  179. public void postOrder(TreeNode node) {
  180. if (node == null) {
  181. return;
  182. }
  183.  
  184. postOrder(node.getLeft());
  185. postOrder(node.getRight());
  186. }
  187.  
  188. }
  189.  
  190.  
  191.  
  192. /*
  193. Name : Nikhil Mohan
  194. university id : 181500427
  195. class id : 38
  196. created on Intellij
  197. Gla University
  198. */
  199. package problem1.main;
  200.  
  201. import problem1.mybst.MyBinarySearchTree;
  202.  
  203. public class MyMain {
  204. public static void main(String[] args) {
  205. MyBinarySearchTree m = new MyBinarySearchTree();
  206.  
  207. //setting root
  208. m.setRoot();
  209. System.out.println("root set : " + m.getRoot().getData());
  210.  
  211.  
  212. //insertion
  213. for (int i = 0; i < 5; i++) {
  214. m.insert(m.getRoot());
  215. }
  216.  
  217.  
  218.  
  219. /*
  220. My binary search tree class in this package supports insertion of left children in to queue during
  221. the time of insertion.
  222. also if we subtract total number the number of queue elements from totla elements present we will get
  223. nodes not having left children.
  224. */
  225.  
  226. //printing left children
  227. m.getQueue().queuePrint(m.getQueue());
  228. //Nodes not having left childrens
  229. System.out.println(m.getTotalInsertion() - m.getQueue().getSize(m.getQueue()));
  230.  
  231.  
  232. }
  233.  
  234. }
  235.  
  236. problem1 finished
  237.  
  238. problem 2 starts
  239.  
  240. package problem2.main;
  241.  
  242. import problem1.node.TreeNode;
  243. import problem4.myqueue.MyQueue;
  244. import problem4.myqueue.Node;
  245.  
  246.  
  247. public class Methods {
  248.  
  249.  
  250. private MyQueue pre, post;
  251.  
  252. //constructor
  253. public Methods() {
  254.  
  255. pre = new MyQueue();
  256. post = new MyQueue();
  257. }
  258.  
  259. public MyQueue getPre() {
  260. return pre;
  261. }
  262.  
  263. public void setPre(MyQueue pre) {
  264. this.pre = pre;
  265. }
  266.  
  267. public MyQueue getPost() {
  268. return post;
  269. }
  270.  
  271. public void setPost(MyQueue post) {
  272. this.post = post;
  273. }
  274.  
  275.  
  276. public void verify_A(TreeNode root) {
  277.  
  278. preOrder(root);
  279. System.out.println("First element of pre Order traversal : " + pre.getFront().getNode().getData());
  280. postOrder(root);
  281. System.out.println("Last Element of post order traversal : " + post.getEnd().getNode().getData());
  282.  
  283. }
  284.  
  285.  
  286. //verification of statement "Both the traversal will give same element at the mid position for odd number of nodes."
  287. public boolean verify_B(TreeNode root) {
  288.  
  289. pre.queuePrint(pre);
  290. post.queuePrint(post);
  291.  
  292. int size = pre.getSize(pre);
  293. int ctr = 0;
  294. int predata = 0;
  295. int postdata = 0;
  296.  
  297. if (size % 2 == 1) {
  298. //last element of this while loop is middle element of pre-order traversal queue
  299. while (ctr < size / 2 && pre.getTmp() != null) {
  300. pre.setTmp(pre.getTmp().getNext());
  301. ctr++;
  302. }
  303. ctr = 0;
  304. try {
  305. assert pre.getTmp() != null;
  306. predata = pre.getTmp().getNode().getData();
  307. } catch (NullPointerException ignore) {
  308. }
  309. while (ctr < size / 2 && post.getTmp() != null) {
  310. post.setTmp(post.getTmp().getNext());
  311. ctr++;
  312. }
  313. try {
  314. assert post.getTmp() != null;
  315. postdata = post.getTmp().getNode().getData();
  316. } catch (NullPointerException ignore) {
  317. }
  318. System.out.println(predata + "pre data");
  319. System.out.println(postdata + "post data");
  320. return predata == postdata;
  321. } else {
  322. System.out.println("Number of elements in tree are even");
  323. return false;
  324. }
  325.  
  326. }
  327.  
  328. //preorder
  329. public void preOrder(TreeNode node) {
  330. if (node == null) {
  331. return;
  332. }
  333.  
  334. pre.enqueue(new Node(node));
  335. preOrder(node.getLeft());
  336. preOrder(node.getRight());
  337. }
  338.  
  339. //postorder
  340. public void postOrder(TreeNode node) {
  341. if (node == null) {
  342. return;
  343. }
  344.  
  345. postOrder(node.getLeft());
  346. postOrder(node.getRight());
  347. post.enqueue(new Node(node));
  348.  
  349.  
  350. }
  351. }
  352.  
  353. package problem2.main;
  354.  
  355. import problem1.mybst.MyBinarySearchTree;
  356. import problem4.myqueue.MyQueue;
  357.  
  358. public class MyMain {
  359. public static void main(String[] args) {
  360. MyBinarySearchTree m = new MyBinarySearchTree();
  361. Methods mthds = new Methods();
  362. MyQueue pre = new MyQueue();
  363. MyQueue post = new MyQueue();
  364. int ctr = 0;
  365. //setting up the root
  366. m.setRoot();
  367. System.out.println("Root set Successfully value :" + m.getRoot().getData());
  368.  
  369. //Setting up the bst
  370. for (int i = 0; i < 4; i++) {
  371. m.insert(m.getRoot());
  372. }
  373. mthds.setPre(pre);
  374. mthds.setPost(post);
  375. mthds.preOrder(m.getRoot());
  376. mthds.postOrder(m.getRoot());
  377. pre.queuePrint(pre);
  378. post.queuePrint(post);
  379.  
  380. System.out.println("Verification of statement root element occours first in pre-order and last in post-order ");
  381. System.out.println(pre.getFront().getNode().getData() == post.getEnd().getNode().getData());
  382.  
  383.  
  384. //verification of statement "Both the traversal will give same element at the mid position for odd number ofnodes."
  385.  
  386. while (pre.getTmp() != null && ctr < pre.getSize(pre) / 2) {
  387. ++ctr;
  388. pre.setTmp(pre.getTmp().getNext());
  389. }
  390. assert pre.getTmp() != null;
  391. System.out.println(pre.getTmp().getNode().getData());
  392. ctr = 0;
  393. while (pre.getTmp() != null && ctr < pre.getSize(pre) / 2) {
  394. ++ctr;
  395. post.setTmp(post.getTmp().getNext());
  396. }
  397. System.out.println(post.getTmp().getNode().getData());
  398.  
  399.  
  400. }
  401.  
  402. }
  403.  
  404.  
  405. problem2 finished
  406.  
  407. problem3 starts
  408.  
  409.  
  410. package problem3.node;
  411.  
  412.  
  413. import problem5.student.Student;
  414.  
  415. public class Node {
  416.  
  417.  
  418. private Student s;
  419. private Node next;
  420.  
  421.  
  422. public Node() {
  423. s = new Student();
  424. next = null;
  425. }
  426.  
  427. public Student getS() {
  428. return s;
  429. }
  430.  
  431. public void setS(Student s) {
  432. this.s = s;
  433. }
  434.  
  435. public Node getNext() {
  436. return next;
  437. }
  438.  
  439. public void setNext(Node next) {
  440. this.next = next;
  441. }
  442.  
  443.  
  444. }
  445.  
  446.  
  447. package problem3.myqueue;
  448.  
  449.  
  450. import problem3.node.Node;
  451.  
  452. public class MyPriorityQueue {
  453.  
  454. private Node tmp, front, end;
  455.  
  456.  
  457. //inserting new student into into queue and priority is set by roll number
  458. public void enqueue(Node newNode) {
  459.  
  460. if (front == null || newNode.getS().getRollno() < front.getS().getRollno()) {
  461. newNode.setNext(front);
  462. front = newNode;
  463. } else {
  464. tmp = front;
  465.  
  466. while (tmp.getNext() != null && tmp.getNext().getS().getRollno() < newNode.getS().getRollno()) {
  467. tmp = tmp.getNext();
  468. }
  469. newNode.setNext(tmp.getNext());
  470. tmp.setNext(newNode);
  471. }
  472. }
  473.  
  474. // printing queue
  475. public void dequeue() {
  476. if (front == null) {
  477. System.out.println("No entry found");
  478. return;
  479. }
  480. do {
  481.  
  482. System.out.print(front.getS().getName() + ":");
  483. System.out.println(front.getS().getRollno());
  484. front = front.getNext();
  485. }
  486. while (front != null);
  487. }
  488. }
  489.  
  490. package problem3.main;
  491.  
  492. import problem3.myqueue.MyPriorityQueue;
  493. import problem3.node.Node;
  494.  
  495.  
  496. // use problem5.student.Student class to create object of student
  497. public class MyMain {
  498. public static void main(String[] args) {
  499. MyPriorityQueue m = new MyPriorityQueue();
  500. for (int i = 0; i < 4; i++) {
  501. Node newNode = new Node();
  502. m.enqueue(newNode);
  503. }
  504. m.dequeue();
  505.  
  506.  
  507. }
  508. }
  509.  
  510.  
  511. problem3 finished
  512.  
  513. problem4 starts
  514.  
  515. package problem4.myqueue;
  516.  
  517. import problem1.node.TreeNode;
  518.  
  519. public class Node {
  520. private TreeNode node;
  521. private Node next;
  522.  
  523. public Node() {
  524. node = new TreeNode();
  525. next = null;
  526. }
  527.  
  528. public Node(TreeNode treenode) {
  529. node = treenode;
  530. next = null;
  531. }
  532.  
  533. public TreeNode getNode() {
  534. return node;
  535. }
  536.  
  537. public void setNode(TreeNode node) {
  538. this.node = node;
  539. }
  540.  
  541. public Node getNext() {
  542. return next;
  543. }
  544.  
  545. public void setNext(Node next) {
  546. this.next = next;
  547. }
  548. }
  549.  
  550.  
  551.  
  552. package problem4.myqueue;
  553. // to create queue to store pre - order successor
  554.  
  555.  
  556. public class MyQueue {
  557.  
  558. private Node front, end, tmp;
  559. private int size;
  560.  
  561.  
  562. public MyQueue() {
  563. front = null;
  564. end = null;
  565. tmp = null;
  566. size = 0;
  567. }
  568.  
  569.  
  570. public int getSize(MyQueue queue) {
  571. size = 0;
  572. while (queue.front != null) {
  573. ++size;
  574. queue.setFront(queue.front.getNext());
  575. }
  576. queue.setFront(queue.getTmp());
  577. return size;
  578. }
  579.  
  580. public void setSize(int size) {
  581. this.size = size;
  582. }
  583.  
  584.  
  585. public Node getFront() {
  586. return front;
  587. }
  588.  
  589. public void setFront(Node front) {
  590. this.front = front;
  591. }
  592.  
  593. public Node getEnd() {
  594. return end;
  595. }
  596.  
  597. public void setEnd(Node end) {
  598. this.end = end;
  599. }
  600.  
  601. public Node getTmp() {
  602. return tmp;
  603. }
  604.  
  605. public void setTmp(Node tmp) {
  606. this.tmp = tmp;
  607. }
  608.  
  609. public void queuePrint(MyQueue queue) {
  610. while (queue.tmp != null) {
  611.  
  612. System.out.print(queue.tmp.getNode().getData() + ",");
  613. queue.tmp = queue.tmp.getNext();
  614. }
  615. System.out.println("\b");
  616. queue.tmp = queue.front;
  617. }
  618.  
  619.  
  620. public void enqueue(Node node) {
  621.  
  622. if (front == null) {
  623. tmp = front = end = node;
  624. } else {
  625. while (tmp.getNext() != null) {
  626. tmp = tmp.getNext();
  627. }
  628. end = node;
  629. tmp.setNext(node);
  630. tmp = front;
  631. }
  632. }
  633.  
  634. public int getSuccessor(int data) {
  635.  
  636. tmp = front;
  637. try {
  638. while (tmp.getNode().getData() != data && tmp != null) {
  639. tmp = tmp.getNext();
  640. }
  641.  
  642. assert tmp != null;
  643. return tmp.getNext().getNode().getData();
  644. } catch (NullPointerException ignore) {
  645. System.out.println("No preorder Successor found");
  646. return -1;
  647. }
  648.  
  649. }
  650.  
  651. }
  652.  
  653.  
  654.  
  655. package problem4.main;
  656.  
  657. import problem1.mybst.MyBinarySearchTree;
  658. import problem2.main.Methods;
  659. import problem4.myqueue.MyQueue;
  660.  
  661. import java.util.Scanner;
  662.  
  663. public class MyMain {
  664. public static void main(String[] args) {
  665.  
  666. Scanner sc = new Scanner(System.in);
  667. MyBinarySearchTree m = new MyBinarySearchTree();
  668. Methods mthds = new Methods();
  669. MyQueue q = new MyQueue();
  670. mthds.setPre(q);
  671.  
  672. //setting up the root
  673. m.setRoot();
  674.  
  675. //inserting into the tree
  676. for (int i = 0; i < 5; i++) {
  677. m.insert(m.getRoot());
  678. }
  679.  
  680. //method to print preorder Successor of given Node
  681. /*
  682. preorder method in
  683. */
  684. mthds.preOrder(m.getRoot());
  685. q.queuePrint(q);
  686. System.out.print("Enter value of which you want to find preorder Successor : ");
  687. System.out.println(q.getSuccessor(sc.nextInt()));
  688.  
  689. }
  690. }
  691.  
  692. problem5 starts
  693.  
  694. /*
  695. * Created by IntelliJ IDEA.
  696. * User: Vaibhav
  697. * Date: 23-Mar-20
  698. * Time: 7:06 PM
  699. */
  700. package problem5.node;
  701.  
  702. import problem5.student.Student;
  703.  
  704. // to define node properties
  705. public class Node {
  706. private Student s;
  707. private Node next;
  708.  
  709.  
  710. public Node(Student s) {
  711. this.s = s;
  712. next = null;
  713. }
  714.  
  715. public Student getS() {
  716. return s;
  717. }
  718.  
  719. public void setS(Student s) {
  720. this.s = s;
  721. }
  722.  
  723. public Node getNext() {
  724. return next;
  725. }
  726.  
  727. public void setNext(Node next) {
  728. this.next = next;
  729. }
  730.  
  731. }
  732.  
  733.  
  734. package problem5.student;
  735.  
  736. import java.util.Scanner;
  737.  
  738. public class Student {
  739. private String name;
  740. private int rollno, backlog, apperingcount;
  741. private Scanner sc;
  742.  
  743.  
  744. public Student() {
  745. sc = new Scanner(System.in);
  746. System.out.print("Name :");
  747. this.name = sc.next();
  748. System.out.print("Roll.no : ");
  749. this.rollno = sc.nextInt();
  750. System.out.print("Backlog_count : ");
  751. this.backlog = sc.nextInt();
  752. System.out.print("appering_count");
  753. this.apperingcount = sc.nextInt();
  754.  
  755.  
  756. }
  757.  
  758. public String getName() {
  759. return name;
  760. }
  761.  
  762. public void setName(String name) {
  763. this.name = name;
  764. }
  765.  
  766. public int getRollno() {
  767. return rollno;
  768. }
  769.  
  770. public void setRollno(int rollno) {
  771. this.rollno = rollno;
  772. }
  773.  
  774. @Override
  775. public String toString() {
  776. return "name='" + name + '\'' +
  777. ", rollno=" + rollno +
  778. ", backlog=" + backlog +
  779. ", apperingcount=" + apperingcount
  780. ;
  781. }
  782.  
  783. public int getBacklog() {
  784. return backlog;
  785. }
  786.  
  787. public void setBacklog(int backlog) {
  788. this.backlog = backlog;
  789. }
  790.  
  791. public int getApperingcount() {
  792. return apperingcount;
  793. }
  794.  
  795. public void setApperingcount(int apperingcount) {
  796. this.apperingcount = apperingcount;
  797. }
  798.  
  799. }
  800.  
  801. /*
  802. * Created by IntelliJ IDEA.
  803. * User: Vaibhav
  804. * Date: 23-Mar-20
  805. * Time: 7:06 PM
  806. */
  807. package problem5.main;
  808.  
  809. import problem5.circularqueue.MyCircularQueue;
  810. import problem5.node.Node;
  811. import problem5.student.Student;
  812.  
  813. import java.util.Scanner;
  814.  
  815. //executable class
  816. public class MyMain {
  817. public static void main(String[] args) {
  818. MyCircularQueue m = new MyCircularQueue();
  819. Node node;
  820.  
  821. for (int i = 0; i < 5; i++) {
  822. node = new Node(new Student());
  823. m.enqueue(node);
  824. }
  825. m.printQueue();
  826.  
  827. m.remove(new Scanner(System.in).next());
  828. m.printQueue();
  829.  
  830. m.process(new Scanner(System.in).next());
  831.  
  832.  
  833. }
  834. }
  835.  
  836.  
  837. /*
  838. * Created by IntelliJ IDEA.
  839. * User: Vaibhav
  840. * Date: 23-Mar-20
  841. * Time: 7:06 PM
  842. */
  843. package problem5.circularqueue;
  844.  
  845. import problem5.node.Node;
  846.  
  847.  
  848. public class MyCircularQueue {
  849. private Node front, tmp, end;
  850.  
  851. public MyCircularQueue() {
  852. front = null;
  853. tmp = null;
  854. end = null;
  855. }
  856.  
  857.  
  858. public Node getFront() {
  859. return front;
  860. }
  861.  
  862. public void setFront(Node front) {
  863. this.front = front;
  864. }
  865.  
  866. public Node getTmp() {
  867. return tmp;
  868. }
  869.  
  870. public void setTmp(Node tmp) {
  871. this.tmp = tmp;
  872. }
  873.  
  874. public void enqueue(Node newNode) {
  875. if (front == null) {
  876. tmp = front = newNode;
  877. return;
  878. }
  879. if (tmp.getNext() == null) {
  880. tmp.setNext(newNode);
  881. newNode.setNext(tmp);
  882. end = newNode;
  883. return;
  884. }
  885. newNode.setNext(tmp.getNext());
  886. tmp.setNext(newNode);
  887.  
  888. }
  889.  
  890. public void printQueue() {
  891. tmp = front;
  892. try {
  893. do {
  894. System.out.println(tmp.getS().toString());
  895. tmp = tmp.getNext();
  896. }
  897. while (tmp != front && tmp != null);
  898. } catch (NullPointerException ignored) {
  899. }
  900. }
  901.  
  902.  
  903. public void remove(String name) {
  904. tmp = front;
  905. if (tmp.getS().getName().equals(name) && tmp.getS().getBacklog() == 0) {
  906. tmp = front = front.getNext();
  907. }
  908. while (!tmp.getNext().getS().getName().equals(name)) {
  909. tmp = tmp.getNext();
  910. if (tmp == front)
  911. return;
  912. }
  913. if (tmp.getS().getBacklog() == 0) {
  914. tmp.setNext(tmp.getNext().getNext());
  915. return;
  916. }
  917. System.out.println("Student backlog is not 0 or student entry not found");
  918. }
  919.  
  920. public void process(String name) {
  921. tmp = front;
  922. if (tmp.getS().getName().equals(name)) {
  923. System.out.println(tmp.getS().toString());
  924. System.out.println(tmp.getS().getBacklog() - tmp.getS().getApperingcount());
  925. }
  926. while (!tmp.getS().getName().equals(name)) {
  927. tmp = tmp.getNext();
  928. if (tmp == front)
  929. return;
  930. }
  931. System.out.println(tmp.getS().toString());
  932. System.out.println(tmp.getS().getBacklog() - tmp.getS().getApperingcount());
  933. }
  934. }
  935.  
  936. problem5 finished
Add Comment
Please, Sign In to add comment