CS210- Lecture 9 June 20, 2005 Announcements Assignment 3 has been posted Part 1 is due next Monday Part 2 is due next Tuesday Midterm is on June 30 Section 3.1 - 3.4 Chapter 4 Section 5.1 - 5.3, Section 5.5 Part of Chapter 6 that I will be able to cover till this Thursday (06/23). I will return Assignment 1 on Thursday (06/23) 6/30/2016 CS210-Summer 2005, Lecture 9 1 Agenda Assignment 3 Tree ADT Tree Terminology Tree Methods Tree Interface Tree Traversals 6/30/2016 CS210-Summer 2005, Lecture 9 2 Assignment 3 A linked list does not truly provide opportunity to use unbounded memory. In reality, memory for a system can be viewed as one large, consecutive array of memory cells. When programming in Java, or most other high-level languages, the systems takes care of most of the details of managing this memory. For example, when you, as a programmer, want memory for creating an additional node in a list, you might give an instruction such as, Node extra = new Node(); When interpreting this instruction, Java finds available cells of memory which can be used by you for an object of class Node. 6/30/2016 CS210-Summer 2005, Lecture 9 3 Singly Linked List embedded in Array 0 Head Index Q 6/30/2016 1 2 3 4 5 Q P -1 8 6 7 8 9 10 11 12 13 14 15 X 12 S 14 G 10 N 4 2 X G S CS210-Summer 2005, Lecture 9 N P 4 Doubly Linked List embedded in Array Array Index 0 1 2 Array Contents 6 -23 9 3 9 4 5 6 7 8 9 10 11 12 13 14 15 16 17 4 15 12 17 0 0 0 3 -1 -57 6 3 589 -1 Header Index: 12 Header 17 -23 0 4 Trailer 6/30/2016 CS210-Summer 2005, Lecture 9 5 Tree ADT Tree is an abstract data type that stores elements hierarchically. Except top element, each element in a tree has a parent element and zero or more children elements. The top element is called the root of the tree. 6/30/2016 CS210-Summer 2005, Lecture 9 6 What is a Tree A tree T consists of nodes with a parentchild relationship that satisfies following properties: If T is nonempty, it has a special node, called the root of T, that has no parent. Each node v of T different from the root E has a unique parent node w; every node with a parent w is a child of w. Applications: A I B C F J G D H K Organization charts File systems Programming environments 6/30/2016 CS210-Summer 2005, Lecture 9 7 Tree Terminology Root: node without parent (A) Subtree: tree consisting of a node and its Internal node: node with at least descendants one child (A, B, C, F) External node (a.k.a. leaf ): node A without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, B C D etc. Depth of a node: number of ancestors E F G H Height of a tree: maximum depth of any node (3) Descendant of a node: child, I J K subtree grandchild, grand-grandchild, etc. 6/30/2016 CS210-Summer 2005, Lecture 9 8 Tree Terminology Two nodes that are children of the same parent are siblings. A node u is an ancestor of a node v if u = v or u is an ancestor of the parent of v. The inheritance relation between classes in a java program forms a tree. 6/30/2016 CS210-Summer 2005, Lecture 9 9 Ordered Tree A Tree is ordered if there is a linear ordering defined for the children of each node; that is, we can identify children of a node as being the first, second, third and so on. A binary tree is an ordered tree with the following properties: Each node has at most two children. Each child node is labeled as being either a left child or a right child. The left child precedes the right child in the ordering of the children of a node. 6/30/2016 CS210-Summer 2005, Lecture 9 10 Recursive def. of Binary Tree Edge A node r, called the root of T and storing an element. A binary tree, called the left subtree of T. A binary tree, called the right subtree of T. An edge of tree T is a pair of nodes (u, v) such that u is the parent of v. Path A path of T is a sequence of nodes such that any two consecutive nodes in the sequence form an edge. 6/30/2016 CS210-Summer 2005, Lecture 9 11 Applications of Binary tree An arithmetic expression can be represented by a binary tree whose external nodes are associated with variables or constants, and whose internal nodes are associated with one of the operators +, - , *, and /. Each node in a tree has a value associated with it: If a node is external, then its value is that of its variable or constant. If a node is internal, then its value is defined by applying its operation to the value of its children. 6/30/2016 CS210-Summer 2005, Lecture 9 12 Tree ADT We use positions to abstract nodes Generic methods: integer size() boolean isEmpty() Iterator elements() Iterator positions() Accessor methods: position root() position parent(p) positionIterator children(p) 6/30/2016 Query methods: Update method: boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) object replace (p, o) Additional update methods may be defined by data structures implementing the Tree ADT CS210-Summer 2005, Lecture 9 13 Performance Assumptions The accessor methods root() and parent (v) take O(1) time. Query methods isInternal(v), isExternal(v) and isRoot(v) takes O(1) time as well. The accessor method children(v) takes O(ci) time, where ci is the number of children of v. The generic methods size() and isEmpty() take O(1) time. The generic methods replace(v, e) runs in O(1) time. The generic methods elements() and positions() take O(n) time where n is the number of nodes. 6/30/2016 CS210-Summer 2005, Lecture 9 14 Basic Algorithms on Trees Depth The depth of a node v can be recursively defined as follows: 6/30/2016 If v is the root, then the depth of v is 0. Otherwise, the depth of v is one plus the depth of the parent v. Try writing depth (Tree T, Position v) Draw recursive trace. Worst case running time is O(n) because in the worst case the depth might be n – 1. CS210-Summer 2005, Lecture 9 15 Basic Algorithms on Trees Height The height of a node v can be recursively defined as follows: If v is an external node, then the height of v is 0. Otherwise, the height of v is one plus the maximum height of a child of v. The height of a nonempty tree T is equal to the maximum depth an external node of T. 6/30/2016 Try writing height1 (Tree T) non recursively by calling depth. Worst case running time is O(n2). CS210-Summer 2005, Lecture 9 16 Height of a Tree Try writing height2 (Tree T, Position v) using recursive definition of height of a node. The height of the tree is computed by calling Height2(T, T.root()) This is more efficient than non recursive method: O(n) 6/30/2016 CS210-Summer 2005, Lecture 9 17 Preorder Traversal A traversal visits the nodes of a tree in a systematic manner In a preorder traversal, a node is visited before its descendants Application: print a structured document 1 2 Algorithm preOrder(v) visit(v) for each child w of v preorder (w) A 5 9 B E 3 4 C D 6/30/2016 6 7 F I 8 G CS210-Summer 2005, Lecture 9 H 18 Preorder Traversal public static String preorderPrint (Tree T, Position v) { String s = v.element().toString(); Iterator children = T.children(v); while(children.hasNext()) S+= “ ” + preorderPrint (T, (Position) children.next()); return s; } Preorder Traversal runs in O(n) time. 6/30/2016 CS210-Summer 2005, Lecture 9 19 Postorder Traversal In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories Try writing postorderPrint. 9 3 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) A 8 7 B E 1 2 C D 6/30/2016 4 5 F I 6 G CS210-Summer 2005, Lecture 9 H 20