IntList
is an implementation of a list of integers.IntList
is eitherIntList
.
public static IntList empty()
public static IntList prepend (int n, IntList L)
Returns a new list node with head n and with tail L.
public static int head (IntList L)
Returns the head of a list node; complains if given an empty list.
public static IntList tail (IntList L)
Returns the tail of a list node; complains if given an empty list.
public static boolean isEmpty (IntList L)
Returns true if L is an empty list; otherwise, returns false.
IntList
operators are found in the file IntListOps.java
.
A few useful operators are given below. Many more are available. See the file.
public static int length (IntList L)
Returns the number of elements in the list L.
public static IntList append (IntList L1, IntList L2)
Returns a list whose elements are those of L1 followed by those of L2.
public static IntList postpend (IntList L, int i)
Returns a list whose elements are those of L followed by i.
IntList L0 = empty() = [] IntList L1 = prepend(1, empty()) = [1] IntList L2 = prepend(1, prepend(2, empty())) = [1,2] IntList L3 = prepend(1, prepend(2, prepend(3, empty()))) = [1,2,3] head(L3) = 1 tail(L3) = [2,3] isEmpty(L0) = true isEmpty(L3) = false length(L0) = 0 length(L1) = 1 length(L3) = 3 append(L0,L2) = [1,2] append(L1,L3) = [1,1,2,3] append(L2,L3) = [1,2,1,2,3] postpend(L0,4) = [4] postpend(L1,4) = [1,4] postpend(L3,4) = [1,2,3,4]
IntListList
is an implementation of a list of lists of integers.IntListList
is eitherIntList
andIntListList
.
public static IntListList empty()
public static IntListList prepend (IntList L, IntListList LL)
Returns a new list node with head L and with tail LL.
public static IntList head (IntListList LL)
Returns the head of a list node; complains if given an empty list.
public static IntListList tail (IntListList LL)
Returns the tail of a list node; complains if given an empty list.
public static boolean isEmpty (IntListList LL)
Returns true if LL is an empty list; otherwise, returns false.
IntListList
operators are found in the file IntListListOps.java
.
A few useful operators are given below. Others are available. See the file.
public static int length (IntListList LL)
Returns the number of elements in the list LL.
public static IntListList append (IntListList LL1, IntListList LL2)
Returns a list whose elements are those of LL1 followed by those of LL2.
public static IntListList postpend (IntListList LL, IntList L)
Returns a list whose elements are those of LL followed by L.
IntList L0 = [] IntList L1 = [1] IntList L2 = [1,2] IntList L3 = [1,2,3]Examples using IntListList methods and operators:
IntListList L4 = empty() = [] IntListList L5 = prepend(L0,empty()) = [[]] IntListList L6 = prepend(L2,empty()) = [[1,2]] IntListList L7 = prepend(L1, prepend(L2, (prepend(L3, empty())))) = [[1],[1,2],[1,2,3]] head(L5) = [] head(L6) = [1,2] head(L7) = [1] tail(L5) = [] tail(L6) = [] tail(L7) = [[1,2],[1,2,3]] isEmpty(L4) = true isEmpty(L5) = false isEmpty(L6) = false isEmpty(L7) = false length(L4) = 0 length(L5) = 1 length(L6) = 1 length(L7) = 3 append(L4,L6) = [[1,2]] append(L5,L7) = [[],[1],[1,2],[1,2,3]] append(L6,L7) = [[1,2],[1],[1,2],[1,2,3]] append(L5,L5) = [[],[]] postpend(L5,L2) = [[],[1,2]] postpend(L6,L1) = [[1,2],[1]] postpend(L7,L3) = [[1],[1,2],[1,2,3],[1,2,3]]
IntTree
is an implementation of a binary tree with integer values.IntTree
is eitherIntTree
, andIntTree
.
public static IntTree leaf()
public static IntTree node (int v, IntTree l, IntTree r)
Returns a tree node with value v, a left subtree l, and a right subtree r.
public static int value (IntTree t)
Returns the value of the tree node; complains if given a leaf.
public static IntTree left (IntTree t)
Returns the left subtree of a tree node; complains if given a leaf.
public static IntTree right (IntTree t)
Returns the right subtree of a tree node; complains if given a leaf.
public static boolean isLeaf (IntTree t)
Returns true if t is a leaf; otherwise, returns false.
IntTree T1 = leaf() = * IntTree T2 = node(5,leaf(),leaf()) = (* 5 *) IntTree T3 = node(3,leaf(),node(1,leaf(),leaf())) = (* 3 (* 1 *)) IntTree T4 = node(8,T2,T1) = ((* 5 *) 8 *) IntTree T5 = node(10,T4,T3) = (((* 5 *) 8 *) 10 (* 3 (* 1 *))) value(T2) = 5 value(T3) = 3 value(T4) = 8 value(T5) = 10 left(T2) = * left(T3) = * left(T4) = (* 5 *) left(T5) = ((* 5 *) 8 *) right(T2) = * right(T3) = (* 1 *) right(T4) = * right(right(T5)) = (* 1 *) value(left(T5)) = 8 value(right(T3)) = 1 isLeaf(T1) = true isLeaf(T2) = false isLeaf(right(T3)) = false isLeaf(right(left(left(T5)))) = true
IntTree
. In the general case, if we
had a tree with Object
nodes, the middle slot (box) would point to the
Object
it refers to. The left slot (box) points to the left subtree and
the right slot (box) points to the right subtree (since trees are Object
s).
The box-and-pointer diagram for T1, T2, T3, T4, and T5 defined above (in the IntTree
examples) is shown below.
or, redrawn a bit more tree-like: