├── .idea
├── DSAassignment3.iml
├── encodings.xml
├── misc.xml
├── modules.xml
├── vcs.xml
└── workspace.xml
├── README.md
├── RPN.java
├── SinglyLinkedList.java
└── Tree.java
/.idea/DSAassignment3.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 | 1551808274779
101 |
102 |
103 | 1551808274779
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DSAassignment3
--------------------------------------------------------------------------------
/RPN.java:
--------------------------------------------------------------------------------
1 | import java.util.Stack;
2 | import java.util.Scanner;
3 | class RPN {
4 | private Stack evaluationStack = new Stack();
5 | // Main input loop
6 | public void loop() {
7 | Scanner in = new Scanner(System.in);
8 | final String prompt = "> ";
9 | System.out.print(prompt);
10 | while (in.hasNext()) {
11 | String s = in.next();
12 | if (s.equals("quit") || s.equals("q")) {
13 | System.out.println("Quitting");
14 | break;
15 | }
16 | System.out.println("Got a string: " + s);
17 | System.out.print(prompt);
18 | }
19 | in.close();
20 | }
21 | // Evaluate the stack and push the result back in the same stack
22 | public void evaluate() {
23 | System.out.println("Tokens: ");
24 | System.out.println(this.toString());
25 | }
26 | // To visualise the token stack
27 | public String toString() {
28 | StringBuilder res = new StringBuilder();
29 | return res.toString();
30 | }
31 | // For unit test purposes
32 | public void addToken(String token) {
33 | evaluationStack.add(token);
34 | }
35 | // For unit test purposes
36 | public Stack getEvaluationStack() {
37 | Stack copy = new Stack();
38 | copy.addAll(evaluationStack);
39 | return copy;
40 | }
41 | public static void main(String[] args) {
42 | RPN calc = new RPN();
43 | calc.loop();
44 | }
45 | }
--------------------------------------------------------------------------------
/SinglyLinkedList.java:
--------------------------------------------------------------------------------
1 | class SinglyLinkedList- {
2 | private int size = 0;
3 | private ListNode
- header;
4 | public static class ListNode
- {
5 | public ListNode
- next = null;
6 | public Item el = null;
7 | }
8 | public SinglyLinkedList() {
9 | header = new ListNode
- ();
10 | }
11 | public int size() {
12 | return size;
13 | }
14 | // Set function for unit testing purposes
15 | public void setList(ListNode
- list, int listSize) {
16 | header.next = list;
17 | size = listSize;
18 | }
19 | // Iterator class for unit testing purposes
20 | public Iterator
- getIterator() {
21 | return new Iterator
- (header);
22 | }
23 | // Iterator class for unit testing purposes
24 | public static class Iterator
- {
25 | private ListNode
- current;
26 | private Iterator(ListNode
- header) {
27 | current = header;
28 | }
29 | public Item next() {
30 | current = current.next;
31 | return current.el;
32 | }
33 | public boolean hasNext() {
34 | return current.next != null;
35 | }
36 | }
37 | public Item get(int n) {
38 | // if (...)
39 | // throw new IllegalArgumentException("Index ouf of bounds");
40 | throw new UnsupportedOperationException();
41 | }
42 | // Insert element x at index n in the list
43 | public void insertAt(int n, Item x) {
44 | // if (...)
45 | // throw new IllegalArgumentException("Index ouf of bounds");
46 | throw new UnsupportedOperationException();
47 | }
48 | // Remove the element at index n from the list
49 | public void removeAt(int n) {
50 | // if (...)
51 | // throw new IllegalArgumentException("Index ouf of bounds");
52 | throw new UnsupportedOperationException();
53 | }
54 | // Reverse the list
55 | public void reverse() {
56 | throw new UnsupportedOperationException();
57 | }
58 | // Represent the contents of the list as a String
59 | /*
60 | public String toString() {
61 | StringBuilder res = new StringBuilder("{");
62 | if (size > 0) {
63 | res.append(firstEl.toString());
64 | for (int i = 1; i < size; ++i) {
65 | res.append(", ");
66 | res.append(el.toString());
67 | }
68 | }
69 | res.append("}");
70 | return res.toString();
71 | }*/
72 | public static void main(String[] args) {
73 | SinglyLinkedList l = new SinglyLinkedList();
74 | System.out.println(l.size());
75 | }
76 | }
--------------------------------------------------------------------------------
/Tree.java:
--------------------------------------------------------------------------------
1 | import java.util.LinkedList;
2 | import java.util.Queue;
3 | import java.util.Stack;
4 | class Tree
- > {
5 | // If the tree is empty, root is a null reference.
6 | private Node
- root = null;
7 | // A node of a tree contains a label, and optionally
8 | // references to the roots of its left and right subtrees,
9 | // which might be null if the subtrees are empty.
10 | public static class Node
- {
11 | public Node
- left = null;
12 | public Node
- right = null;
13 | public Item el = null;
14 | // Will print out the binary node structure
15 | public void print() {
16 | print("", true);
17 | }
18 | private void print(String prefix, boolean isTail) {
19 | System.out.println(prefix + (isTail ? "âââ " : "âââ ") + el.toString());
20 | if (right != null) {
21 | right.print(prefix + (isTail ? " " : "â "), false);
22 | }
23 | if (left != null) {
24 | left.print(prefix + (isTail ? " ":"â "), true);
25 | }
26 | }
27 | }
28 | // For unit testing purposes
29 | public void setRoot(Node
- newRoot) {
30 | root = newRoot;
31 | }
32 | public Node
- getRoot() {
33 | return root;
34 | }
35 | // This method constructs the following
36 | // example tree with 5 in the root:
37 | //
38 | // __5__
39 | // / \
40 | // / \
41 | // 1 8
42 | // \ / \
43 | // 2 1 3
44 | // / \
45 | // 5 7
46 | public static Tree exampleTree() {
47 | Node t = new Node();
48 | t.el = 5;
49 | Node t1 = new Node();
50 | t.left = t1;
51 | t1.el = 1;
52 | Node t2 = new Node();
53 | t.right = t2;
54 | t2.el = 8;
55 | Node t12 = new Node();
56 | t1.right = t12;
57 | t12.el = 2;
58 | Node t21 = new Node();
59 | t2.left = t21;
60 | t21.el = 1;
61 | Node t22 = new Node();
62 | t2.right = t22;
63 | t22.el = 3;
64 | Node t211 = new Node();
65 | t21.left = t211;
66 | t211.el = 5;
67 | Node t212 = new Node();
68 | t21.right = t212;
69 | t212.el = 7;
70 | Tree res = new Tree();
71 | res.root = t;
72 | return res;
73 | }
74 | // This method constructs the following
75 | // example binary tree with 4 in the root:
76 | //
77 | // __4__
78 | // / \
79 | // / \
80 | // 1 8
81 | // \ / \
82 | // 2 6 9
83 | // / \
84 | // 5 7
85 | public static Tree exampleTreeBin() {
86 | Node t = new Node();
87 | t.el = 4;
88 | Node t1 = new Node();
89 | t.left = t1;
90 | t1.el = 1;
91 | Node t2 = new Node();
92 | t.right = t2;
93 | t2.el = 8;
94 | Node t12 = new Node();
95 | t1.right = t12;
96 | t12.el = 2;
97 | Node t21 = new Node();
98 | t2.left = t21;
99 | t21.el = 6;
100 | Node t22 = new Node();
101 | t2.right = t22;
102 | t22.el = 9;
103 | Node t211 = new Node();
104 | t21.left = t211;
105 | t211.el = 5;
106 | Node t212 = new Node();
107 | t21.right = t212;
108 | t212.el = 7;
109 | Tree res = new Tree();
110 | res.root = t;
111 | return res;
112 | }
113 | public int size() {
114 | return size(root);
115 | }
116 | private int size(Node
- node) {
117 | if (node == null) {
118 | return 0;
119 | } else {
120 | return 1 + size(node.left) + size(node.right);
121 | }
122 | }
123 | /**
124 | * Assignment 3 Question 7. Returns the nth element in Breadth First
125 | Search
126 | * (BFS) order
127 | *
128 | * @param n the position
129 | * @return the element found at the position
130 | */
131 | public Item nthBFS(int n) {
132 | throw new UnsupportedOperationException();
133 | }
134 | /**
135 | * Hands on session 7, exercise 2. Prints the labels of the tree's
136 | nodes in
137 | * breadth first order (BFS)
138 | *
139 | */
140 | public void printBFT() {
141 | throw new UnsupportedOperationException();
142 | }
143 | /**
144 | * Hands on session 7, exercise 1. Returns the n:th element in Depth
145 | First
146 | * Search
147 | *
148 | * @param n the node to find
149 | * @return the element in the n:th place
150 | *
151 | */
152 | public Item nthDFS(int n) {
153 | throw new UnsupportedOperationException();
154 | }
155 | /**
156 | * Assignment 3 Question 8. Prints the nodes of the tree in depth-
157 | first
158 | * order
159 | */
160 | public void printDFS() {
161 | System.out.println(toStringDFS());
162 | throw new UnsupportedOperationException();
163 | }
164 | /**
165 | * Creates a string according to DFS.
166 | * Made public for unit testing
167 | *
168 | * @return a string representation of the tree in DFS order
169 | */
170 | public String toStringDFS() {
171 | throw new UnsupportedOperationException();
172 | // StringBuilder sb = new StringBuilder();
173 | // return sb.toString();
174 | }
175 | /**
176 | * Assignment 3, Question 9. Insert i into a binary search tree
177 | *
178 | * @param i the Item to insert
179 | */
180 | public void insertBST(Item i) {
181 | throw new UnsupportedOperationException();
182 | }
183 | /**
184 | * Hands on session 7, exercise 3. Removes one item from a binary
185 | search
186 | * tree and then rearranges the nodes so that the tree after the item
187 | is
188 | * removed is still a binary search tree.
189 | *
190 | * @param i - the item to remove
191 | */
192 | public void removeBST(Item i) {
193 | // This method mainly calls deleteRec()
194 | root = deleteRec(root, i);
195 | }
196 | /**
197 | * A recursive function to delete a new element in BST, assuming
198 | existing
199 | * tree is BST.
200 | *
201 | * @param root the current root node
202 | * @param i the Item to delete
203 | * @return the new root node
204 | */
205 | private Node
- deleteRec(Node
- root, Item i) {
206 | throw new UnsupportedOperationException();
207 | }
208 | /**
209 | * A method for visualization and debugging
210 | */
211 | public void printTree() {
212 | if (root != null) {
213 | root.print();
214 | }
215 | }
216 | public static void main(String[] args) {
217 | Tree t = exampleTree();
218 | // System.out.println("Size: " + t.size());
219 | t.printTree();
220 | }
221 | }
--------------------------------------------------------------------------------