Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2005 Week 8 Lectures Maps & Binary Trees.

Similar presentations


Presentation on theme: "CS2005 Week 8 Lectures Maps & Binary Trees."— Presentation transcript:

1 CS2005 Week 8 Lectures Maps & Binary Trees

2 Aims Describe and specify the Map Data Type
CS2005 Aims Describe and specify the Map Data Type Describe java.util.HashMap and java.util.TreeMap Describe the Binary Tree Data Structure Define basic Binary Tree terminology Describe ways of traversing a Binary Tree Java implementation of a Binary Tree

3 Learning Outcomes describe and use a Map ADT
CS2005 Learning Outcomes describe and use a Map ADT describe the structure of a Binary Tree implement a simple Binary Tree in Java write Java code to perform simple operations using Binary Trees recall main points of Watt & Brown chs 10 & 11

4 Map equivalent to table
CS2005 Map equivalent to table

5 CS2005 Map equivalent to set

6 Map ADT Essential Operations create:  Map
CS2005 Map ADT Essential Operations create:  Map put: Key  Value  Map  Map containsKey: Key  Map  Boolean get: Key  Map  Value remove: Key  Map  Map isEmpty: Map  Boolean

7 Java.util classes TreeMap HashMap complexity O(log n)
CS2005 Java.util classes TreeMap complexity O(log n) iterator over set of keys is sorted HashMap complexity O(1) iterator over set of keys ‘random’

8 CS2005 Binary Tree Structure Terminology: node, branch, root node, leaf node, parent, child, ancestor, descendant, subtree, null tree, depth

9 Balanced Binary Trees M = abs(Size(left stree) - Size(right stree))
CS2005 Balanced Binary Trees M = abs(Size(left stree) - Size(right stree)) Tree balanced if M <= 1 for itself and all its subtrees Measure if ill-balance largest value of M for tree and all subtrees Degree of ill-balance 0 (for trees of size 0 or 1) (Measure if ill-balance)/(size-1) otherwise

10 Tree Traversals Breadth First Depth First visit root node
CS2005 Tree Traversals Breadth First visit root node then all nodes depth 1 then all nodes depth 2, etc Depth First visit all nodes in one subtree before another same rule applies to all subtrees

11 Breadth First Traversal
CS2005 Breadth First Traversal If tree empty then return Make empty queue Q Append root node to Q Loop until Q is empty remove node from Q process node append child nodes to Q Return

12 Depth First Traversal (pre- order)
CS2005 Depth First Traversal (pre- order) If tree empty then return Make empty stack S Push root node on S Loop until S is empty pop node from S process node push child nodes on S Return

13 Depth First Traversal (recursive)
CS2005 Depth First Traversal (recursive) If tree is empty then return Process the root node Recursively traverse left subtree Recursively traverse right subtree Return Order of middle steps can be changed

14 Java Implementation Public class BTNode { protected Object element;
CS2005 Java Implementation Public class BTNode { protected Object element; protected BTNode left, right; protected BTNode(Object elem, BTNode lt, BTNode rt) { element = elem; left = lt; right = rt; }

15 CS2005 Sample Code Html document Binary tree code test program

16 Example - equals method
CS2005 Example - equals method public static boolean equals(CharTree t1, CharTree t2) { if (t1==t2) return true; if (t1==null || t2==null) return false; return ((t1.element == t2.element) && (CharTree.equals(t1.left,t2.left)) && (CharTree.equals(t1.right,t2.right))); }


Download ppt "CS2005 Week 8 Lectures Maps & Binary Trees."

Similar presentations


Ads by Google