vault backup: 2024-03-21 22:58:58

This commit is contained in:
2024-03-21 22:58:58 +00:00
parent 68fa81d02f
commit e241fe8bd6
49 changed files with 1950 additions and 210 deletions

View File

@@ -0,0 +1 @@
File Input / Output

View 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

View File

@@ -0,0 +1,35 @@
```java
public class IntNode
{
public int info;
public IntNode next;
}
```
![](Pasted%20image%2020240319224232.png)
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;
}
```

View File

@@ -0,0 +1,53 @@
a) List the order in which the nodes are visited in:
post-order.
![](Pasted%20image%2020240319225420.png)
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;
}
```
![](Pasted%20image%2020240319234142.png)
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