Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS210- Lecture 9 June 20, 2005 Announcements

Similar presentations


Presentation on theme: "CS210- Lecture 9 June 20, 2005 Announcements"— Presentation transcript:

1 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 Chapter 4 Section , 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)  4/23/2019 CS210-Summer 2005, Lecture 9

2 Agenda Assignment 3 Tree ADT Tree Terminology Tree Methods
Tree Interface Tree Traversals 4/23/2019 CS210-Summer 2005, Lecture 9

3 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. 4/23/2019 CS210-Summer 2005, Lecture 9

4 Singly Linked List embedded in Array
Q 8 P -1 X 12 S 14 G 10 N 4 Head Index 2 Q X G S N P 4/23/2019 CS210-Summer 2005, Lecture 9

5 Doubly Linked List embedded in Array
Index  0   1   2   4   5   6   7   8   9  10  11  12  13  14  15  16  17  Contents 6 -23 9 4 15 12 17 3 -1 -57 589 Header Index: 12 Header    17   -23    0    4    Trailer 4/23/2019 CS210-Summer 2005, Lecture 9

6 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. 4/23/2019 CS210-Summer 2005, Lecture 9

7 What is a Tree A tree T consists of nodes with a parent-child 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 has a unique parent node w; every node with a parent w is a child of w. Applications: Organization charts File systems Programming environments A B D C G H E F I J K 4/23/2019 CS210-Summer 2005, Lecture 9

8 Tree Terminology subtree Root: node without parent (A)
Internal node: node with at least one child (A, B, C, F) External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, etc. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Descendant of a node: child, grandchild, grand-grandchild, etc. Subtree: tree consisting of a node and its descendants A B D C G H E F I J K subtree 4/23/2019 CS210-Summer 2005, Lecture 9

9 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. 4/23/2019 CS210-Summer 2005, Lecture 9

10 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. 4/23/2019 CS210-Summer 2005, Lecture 9

11 Recursive def. of Binary Tree
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. Edge 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. 4/23/2019 CS210-Summer 2005, Lecture 9

12 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. 4/23/2019 CS210-Summer 2005, Lecture 9

13 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) Query methods: boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) Update method: object replace (p, o) Additional update methods may be defined by data structures implementing the Tree ADT 4/23/2019 CS210-Summer 2005, Lecture 9

14 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. 4/23/2019 CS210-Summer 2005, Lecture 9

15 Basic Algorithms on Trees
Depth The depth of a node v can be recursively defined as follows: 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. 4/23/2019 CS210-Summer 2005, Lecture 9

16 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. Try writing height1 (Tree T) non recursively by calling depth. Worst case running time is O(n2). 4/23/2019 CS210-Summer 2005, Lecture 9

17 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) 4/23/2019 CS210-Summer 2005, Lecture 9

18 Preorder Traversal Algorithm preOrder(v) visit(v)
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 Algorithm preOrder(v) visit(v) for each child w of v preorder (w) 1 A 2 5 9 B E I 6 8 3 4 7 G C D F H 4/23/2019 CS210-Summer 2005, Lecture 9

19 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. 4/23/2019 CS210-Summer 2005, Lecture 9

20 Postorder Traversal Algorithm postOrder(v) for each child w of v
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. Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) 9 A 8 3 7 B E I 1 2 4 5 6 C D F G H 4/23/2019 CS210-Summer 2005, Lecture 9


Download ppt "CS210- Lecture 9 June 20, 2005 Announcements"

Similar presentations


Ads by Google