# Binary Trees & Recursion

CSC 251 Study Guide đŗ

đ ī¸Wed, Dec 3, 2014

đ4 min read â 694 words

CSC 251 Study Guide đŗ

đ ī¸Wed, Dec 3, 2014

đ4 min read â 694 words

*trees*- a structure with unique starting point - root - where each node can have multiple children nodes, and a unique path exists from the root node to every other node

*root*- top node of a tree structure, a node with no parent

*binary tree*- structure where each item is called a node and where each node can have a max of two children, left and right child node
- diagram:

*leaf*- node with no children

*descendant*- child of a node

*ancestor*- parent of a node

*binary search tree*- binary tree where value in any node is greater than or equal to value in its left child and any of descendants (nodes in left subtree) and less than value in its right child and any of its descendants (nodes in right subtree)
- diagram:

*full binary tree*- binary tree where all of leaves are on same level and every nonleaf node has 2 children

*complete binary tree*- binary tree that is full or full through next-to-last level, with leaves on last level as far as possible

- binary tree that is full or full through next-to-last level, with leaves on last level as far as possible
*balanced tree*- left and right subtrees of any node are the same height

- left and right subtrees of any node are the same height
*preorder*- node/root, left, right

*inorder*- left, node/root, right

*postorder*- left, right, node/root

FUN TIP Iđa fast way to remember this is through the following, starting by writing r in the far left then fill it out with L or R, representing left or right, as follows:

rL R - preorder LrR - inorder L Rr- postorder

FUN TIP IIđin-order traversal is probably easiest to see, because it sorts the values from smallest to largest (literally)

**How to Delete A Binary Search Tree**âĄ- no successor
- if node is leaf, simply removed
- but if root is leaf, pointer to tree assigned null value
- one successor
- parent node connected to sucessor node
- deleted node disposed of
- two successors
- find logical predecessor (node in left subtree with largest value)
- logical predecessor replaces deleted node

**recursive method is a method that calls itself**- in many cases, recursive algorithms are less efficient than iterative algorithms

**recursive solutions repetitively**- allocate memory for parameters and local variables
- store address of where control returns after method terminates

**basically works like this:**- a base case is established
- if matched, method solves it and returns
- if base case cannot be solved
- method reduces it to smaller problem (recursive case) and calls itself to solve smaller problem

**examples of recursion in real life**- quick sort, merge sort, flowers,Towers of Hanoi, Fibonacci sequence, factorials
- towers of hanoi fun pic:

**example 1 provided - emptyVase**đļ

```
void emptyVase(int flowersInVase) {
if (flowersInVase > 0) {
//takes one flower
emptyVase(flowersInVase-1);
} else {
// vase is empty, nothing to do
}
}
```

**example 2 provided - Recursion2**

```
public class Recursion2 {
public void countItDown(int counter) {
if (counter == 0)
return;
else {
System.out.println("Count: " + counter);
counter--;
countItDown(counter);
System.out.println("" + counter);
return;
}
}
public static void main (String[] args) {
Recursion2 myRecursor = new Recursion2();
myRecursor.countItDown(5);
}
}
```

**example 3 provided - count**â´

```
count(n)
if (n > 0)
print n
count (n-1)
else
print done
count: n --> n = 3
count: (n-1) --> n = 2
count: (n-1) --> n = 1
count: (n-1) --> n = 0
```

**example 4 - from textbook**

```
public class Recursive {
public static void message(int n) { //displays message n times
if (n > 0) {
System.out.println("This is a recursive method.");
message(n-1);
}
}
}
```

**example 5 - from textbook**

```
public static int fib(int n) { // returns nth number in Fibonacci sequence
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
```

**example 6 - from textbook**

```
public static int factorial(int n) { // returns factorial of non-negative argument
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
```

**example 7 - from textbook**

```
public static int gcd(int x, int y) { // returns greater common denominator of two arguments
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
```