CSC300: Linked Mutators using Recursion

Contents [0/6]

Video [1/6]
Loop [2/6]
Non-Nullable Recursion [3/6]
Non-Nullable Recursion [4/6]
Nullable Recursion [5/6]
Nullable Recursion [6/6]

(Click here for one slide per page)


Video [1/6]

This lecture is optional. It will not be covered on exams. This is a preview of material that is covered in CSC 301/403.

Open Playlist


      

Loop [2/6]

11
12
13
14
15
16
17
18
19
20
21
22
23
24
  public void insert (double item) {  
    if (first == null || first.item >= item) {
      first = new Node (item, first);
    } else {
      Node x = first;



      while (x.next != null && x.next.item < item) {
        x = x.next;
      }
      x.next = new Node (item, x.next);
    }
  }
insertI-

Non-Nullable Recursion [3/6]

11
12
13
14
15
16
17
18
19
20
21
22
23
24
  public void insert (double item) {  
    if (first == null || first.item >= item) {
      first = new Node (item, first);
    } else {
      insertH (first, item);
    }
  }
  private static void insertH (Node x, double item) { 
    if (x.next != null && x.next.item < item) {
      insertH (x.next, item);
    } else {
      x.next = new Node (item, x.next);
    }
  }

Direct translation of loop.

insertRNNF-First-

Non-Nullable Recursion [4/6]

11
12
13
14
15
16
17
18
19
20
21
22
23
24
  public void insert (double item) {  
    if (first == null || first.item >= item) {
      first = new Node (item, first);
    } else {
      insertH (first, item);
    }
  }
  private static void insertH (Node x, double item) { 
    if (x.next == null || x.next.item >= item) {
      x.next = new Node (item, x.next);
    } else {
      insertH (x.next, item);
    }
  }

Computes forwards

A single function call both:

  • creates the new node
  • puts it in it's place
insertRNNF-

Nullable Recursion [5/6]

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  public void insert (double item) {  



    first = insertH (first, item);

  }
  private static Node insertH (Node x, double item) { 
    if (x == null || x.item >= item) {
      return new Node (item, x);
    } else {
      x.next = insertH (x.next, item);
      return x;
    }
  }

Computes both forwards and backwards

Two separate function calls

  • the sender makes the new node
  • the receiver puts it in it's place

The receiver could be a call to the starter, or the helper

  • Only check the termination condition once!

This pattern is really great with balanced trees (next quarter).

insertRNF-First-

Nullable Recursion [6/6]

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  public void insert (double item) {  



    first = insertH (first, item);

  }
  private static Node insertH (Node x, double item) { 
    if (x == null || x.item >= item) {
      return new Node (item, x);
    } else {
      x.next = insertH (x.next, item);
      return x;
    }
  }

Easiest to write, once you understand it.

Some common mistakes:

  • Forget to update first in the starter.
  • Forget to update x.next in the helper.
  • Forget to return x in the helper.
insertRNF-

Revised: 2008/03/17 13:01