190 likes | 203 Views
Section 5. Lists again. Double linked lists – insertion, deletion. Trees. Lists: Revisited. class List { protected ListCell head; public void delete(Object o) {...} }. head. null. List cells:. Cells containg Objects. Each Cell has a pointer to the next cell in the list.
E N D
Section 5 • Lists again. • Double linked lists – insertion, deletion. • Trees
Lists: Revisited • class List { protected ListCell head; public void delete(Object o) {...} } head null
List cells: • Cells containg Objects. Each Cell has a pointer to the next cell in the list. class ListCell { ... public ListCell getNext(); // returns the next element. public void setNext(ListCell l); // sets the pointer to point to l public Object getDatum(); // returns the object stored in the cell }
Deleting • Iterative version: public void delete(Object o) { ListCell current = head, previous = null; while (current != null) { if (current.getDatum().equals(o)) // found the object { if (previous == null) return l.getNext() ; // it was the first one else { previous.setNext(current.getNext()); return l; } } else previous = current; current = current.getNext(); } }
Deleting 2: Revisited • Deleting element: recursive way: Intuition: • – If list l is empty, return null. • – If first element of l is o, return rest of list l. • – Otherwise, return list consisting of firstelement of l, and the list that results fromdeleting o from the rest of list l.
Deleting 3: Revolutions • Notation: (x:xs) – a list which first element is x and the rest is list xs • Example: (1:(2:(3:null))) Then (pseudo-code): • delete o null = return null; • delete o (x:xs) = if x = = o then return xs; • else { rest = delete o xs; • 5 . return (x:(delete o rest)); }
Deleting 4: New hope • Deleting an element from list: public void delete(Object o) { head = deleteRec(o, head); } public static ListCell deleteRec(Object o, ListCell l) { ListCell rest; • if (l = = null) return l; • if (l.getDatum().equals(o)) return l.getNext(); • rest = deleteRec(l.getNext(), o); • l.setNext(rest); • return l; }
Doubly-linked lists class DLLCell { protected Object datum;protected DLLCell next; protected DLLCell previous; ….. }
Doubly-linked lists • class Dlist { protected DLLCell head; public void insertAfter(Object o, Object a) // inserts o after a { insertAfterRec(head, o, a); } public void delete(Object o); }
DLL: Insertion Intuition: • The result of inserting o to the empty list is ... • The result of inserting o to the list starting with a is ... • The result of inserting o to the list starting with x is ...
DLL: Insertion Intuition: • The result of inserting o to the empty list is a list containing o. • The result of inserting o to the list starting with a is a list containing a, o and the rest of original list. • The result of inserting o to the list starting with x is the list containing x and the result of inserting o to the rest of the original list.
DLL: Insertion DLLCell insertAfterRec(DLLCell l, Object o, Object a) { if (l == null) // empty list return new DLLCell(o, null, null); if (l.getDatum().equals(a)) // list starting with a { DLLCell cell = new DLLCell(o, l, l.getNext()); l.setNext(cell); return l; } //otherwise l.setNext(insertAfterRec(l.getNext(), o, a)); return l; }
DLL: Deletion Intuition: • The result of deleting o from the empty list is ... • The result of deleting o from the list starting with o is ... • The result of deleting o from the list starting with x <> o is ...
DLL: Deletion Intuition: • The result of deleting o from the empty list is the empty list • The result of deleting o from the list starting with o is the rest of the list • The result of deleting o from the list starting with x <> o is the list containing x and the result of deleting o from the rest of the list
DLL: Deletion DLLCell deleteRec(DLLCell l, Object o) { DLLCell rest; if (l == null) // empty list return null; if (l.getDatum().equals(o)) // list starting with o { return l.getNext(); } //otherwise rest = deleteRec(l.getNext(), o); l.setNext(rest); rest.setPrev(l); // to make sure links are updated return l; }
Trees! Class for binary tree cells class TreeCell { protected Object datum; protected TreeCell left; protected TreeCell right; public TreeCell(Object o) { datum = o; } public TreeCell(Object o, TreeCell l, TreeCell r) { datum = o; left = l; right = r; } methods called getDatum, setDatum, getLeft, setLeft, getRight, setRight with obvious code }
Height of a tree • Intuition: • The height of a an empty tree is -1 • The height of a tree containing leaf is ... • The height of a tree containing subtrees l1 and l2 is ...
Height of a tree int height(TreeCell t) { if (t == null) return -1; if (isLeaf(t)) return 0; return Math.max( height(t.getLeft()), height(t.getRight())) + 1; }
Number of nodes in the tree int nodes(TreeCell t) { if (t == null) return 0; return nodes(t.getLeft()) + nodes(t.getRight()) + 1; }