vault backup: 2024-03-21 22:58:58
This commit is contained in:
1
Semester 2/Programming 2/Week 10 Revision/Q1.md
Normal file
1
Semester 2/Programming 2/Week 10 Revision/Q1.md
Normal file
@@ -0,0 +1 @@
|
||||
File Input / Output
|
15
Semester 2/Programming 2/Week 10 Revision/Q3.md
Normal file
15
Semester 2/Programming 2/Week 10 Revision/Q3.md
Normal file
@@ -0,0 +1,15 @@
|
||||
Recursion via factorial function
|
||||
```java
|
||||
int fact(int n)
|
||||
{
|
||||
int result;
|
||||
if(n==0 || n==1)
|
||||
return 1;
|
||||
|
||||
result = fact(n-1) * n;
|
||||
return result;
|
||||
}
|
||||
```
|
||||
Since factorial(0) or factorial(1) is just 1, we return 1 in these scenarios
|
||||
If we have factorial(2), we would do factorial(1)\*2 = 2
|
||||
fact(5) = factorial(4) \* 5 = 5\*4\*3\*2\*1 = 120
|
35
Semester 2/Programming 2/Week 10 Revision/Q4.md
Normal file
35
Semester 2/Programming 2/Week 10 Revision/Q4.md
Normal file
@@ -0,0 +1,35 @@
|
||||
```java
|
||||
public class IntNode
|
||||
{
|
||||
public int info;
|
||||
public IntNode next;
|
||||
}
|
||||
```
|
||||

|
||||
a) Write code to add 50 to the appropriate field of the node pointed to by temp.
|
||||
|
||||
```java
|
||||
temp.info += 50;
|
||||
```
|
||||
|
||||
b) Write code to remove from the list the item after temp.
|
||||
```java
|
||||
temp.next = temp.next.next;
|
||||
```
|
||||
|
||||
c) Write the code to remove from the list temp.
|
||||
```java
|
||||
temp.info = temp.next.info; // Assign the next node's value to temp - this will overwrite temp's value.
|
||||
temp.next = temp.next.next; // Set the pointer for temp's next node to the node after the value taken for temp - this effectively clones temp.next and replaces temp.
|
||||
```
|
||||
|
||||
d) Write code to go through the list and to sum all the values within the list. You may assume the existence of head, a reference to the start of the list.
|
||||
|
||||
```java
|
||||
IntNode current = head;
|
||||
int sum = 0;
|
||||
while ( current != null ) {
|
||||
sum += current.info;
|
||||
current = current.next;
|
||||
}
|
||||
```
|
53
Semester 2/Programming 2/Week 10 Revision/Q5.md
Normal file
53
Semester 2/Programming 2/Week 10 Revision/Q5.md
Normal file
@@ -0,0 +1,53 @@
|
||||
a) List the order in which the nodes are visited in:
|
||||
post-order.
|
||||

|
||||
D,B,F,G,E,C,A
|
||||
|
||||
pre-order
|
||||
A,B,D,C,E,F,G
|
||||
|
||||
in-order
|
||||
D,B,F,E,G,C,A
|
||||
|
||||
b)
|
||||
```java
|
||||
public class BinTreeNode
|
||||
{
|
||||
public String info;
|
||||
public BinTreeNode leftChild, rightChild;
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public void postTraverse( BinTreeNode node )
|
||||
{
|
||||
if ( node != null )
|
||||
{
|
||||
postTraverse( node.leftNode );
|
||||
postTraverse( node.rightNode );
|
||||
System.out.println( node.info );
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
c) The following method counts the number of nodes in the tree
|
||||
```java
|
||||
public int countTree(BinTreeNode t)
|
||||
{
|
||||
int count;
|
||||
if (t == null)
|
||||
count = 0
|
||||
else
|
||||
count = countTree(t.leftChild)+countTree(t.rightChild)+1;
|
||||
return count;
|
||||
}
|
||||
```
|
||||

|
||||
|
||||
countTree(root)
|
||||
-> count = countTree(t.leftChild) + countTree(t.rightChild+1)
|
||||
countTree(t.leftChild)
|
||||
-> return 1, since left and right child null
|
||||
countTree(t.rightChild)
|
||||
-> return 1, since left and right child null
|
||||
count = 1 + 1 + 1 = 3
|
Reference in New Issue
Block a user