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,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