Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Section 5 Lists again. Double linked lists – insertion, deletion. Trees."— 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 first element of l, and the list that results from deleting 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): 1. delete o null = return null; 2. delete o (x:xs) = if x = = o then return xs; 3. else { rest = delete o xs; 4. 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; 1. if (l = = null) return l; 2. if (l.getDatum().equals(o)) return l.getNext(); 3. rest = deleteRec(l.getNext(), o); 4. l.setNext(rest); 5. 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; }


Download ppt "Section 5 Lists again. Double linked lists – insertion, deletion. Trees."

Similar presentations


Ads by Google