CSC300: Optimistic Recursion [6/13] Previous pageContentsNext page

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;
    }
  }

This is the easiest to write, once you understand it.

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

Some common mistakes:

Previous pageContentsNext page