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

Slides:



Advertisements
Similar presentations
A.A DA1: Binary Tree1 Binary Tree. A.A DA1: Binary Tree2 Implementazione usa Tree LinkedBinaryTree BinaryTree NodePositionList PositionList.
Advertisements

Lecture 15 Linked Lists part 2
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Data Structure Lecture-5
Tree representation and tree search - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 10.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
LISTS & TREES Lecture 8 CS2110 – Fall List Overview 2  Purpose  Maintain an ordered set of elements (with possible duplication)  Common operations.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Department of Computer Science University of Maryland, College Park
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Binary Search Trees CMSC 132 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
1 Lists. 2 Overview Arrays –Random access: –Fixed size: cannot grow on demand after creation:  Characteristics of some applications: –do not need random.
trees1 Binary Trees trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Trees Weiss sec (preview) ch. 18 (sort of).
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
LISTS Slides of K. Birman, Cornell University. List Overview 2  Purpose  Maintain an ordered collection of elements (with possible duplication)  Common.
TREES Lecture 12 CS2110 – Fall Announcements  Prelim #1 is tonight!  Olin 155  A-L  5:30  M-Z  5:30  A4 will be posted today  Mid-semester.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lecture1 introductions and Tree Data Structures 11/12/20151.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
LISTS Lecture 9 CS2110 – Fall Time spent on A2 2  max: 25.4; avg: 5.2 hours, mean: 4.5 hours, min: 0.57 hours.
Section 4 Boxing classes Boxing classes Array initialization Array initialization Lists: recursion and iteration Lists: recursion and iteration.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
1 Lecture 14: Trees & Insertion Sort CompSci 105 SS 2006 Principles of Computer Science.
TREES Lecture 11 CS2110 – Spring Readings and Homework  Textbook, Chapter 23, 24  Homework: A thought problem (draw pictures!)  Suppose you use.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
CS1020 L AB 3 (L INKED L IST ) Problem 1: Balls Problem 2: Josephine Problem 3: Keyboard.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Recursive Objects (Part 4)
Data Structures Lecture 22 Sohail Aslam.
UNIT III TREES.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Lecture No.15 Data Structures Dr. Sohail Aslam
CSCS-200 Data Structures and Algorithms
ITEC 2620M Introduction to Data Structures
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Trees Lecture 12 CS2110 – Spring 2018.
Search Sorted Array: Binary Search Linked List: Linear Search
singleRightRotation 
Lecture No.16 Data Structures Dr. Sohail Aslam.
Trees Lecture 9 CS2110 – Fall 2009.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Recursive Linked List Operations
Operations on Binary Tree
Non-Linear Structures
Announcements Prelim 1 on Tuesday! A4 will be posted today
Search Sorted Array: Binary Search Linked List: Linear Search
Trees Lecture 10 CS2110 – Spring 2013.
Presentation transcript:

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 first element of l, and the list that results from deleting 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): 1. delete o null = return null; 2. delete o (x:xs) = if x = = o then return xs; 3. else { rest = delete o xs; 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; 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; }

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