1 / 19

Section 5

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.

acohen
Download Presentation

Section 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Section 5 • Lists again. • Double linked lists – insertion, deletion. • Trees

  2. Lists: Revisited • class List { protected ListCell head; public void delete(Object o) {...} } head null

  3. 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 }

  4. 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(); } }

  5. 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.

  6. 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)); }

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

  8. Doubly-linked lists class DLLCell { protected Object datum;protected DLLCell next; protected DLLCell previous; ….. }

  9. 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); }

  10. 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 ...

  11. 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.

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

  13. 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 ...

  14. 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

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

  16. 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 }

  17. 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 ...

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

  19. Number of nodes in the tree int nodes(TreeCell t) { if (t == null) return 0; return nodes(t.getLeft()) + nodes(t.getRight()) + 1; }

More Related