CS2005 Week 8 Lectures Maps & Binary Trees.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
data ordered along paths from root to leaf
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
1 Trees, Trees, and More Trees. 2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully,
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 16: Trees Announcements 1.Programming project.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
CS2005 Week 7 Lectures Set Abstract Data Type.
Binary Trees.
Binary Search Trees Chapter 7 Objectives
The Tree ADT.
Heap Chapter 9 Objectives Define and implement heap structures
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Trees.
Chapter 25 Binary Search Trees
Trees Chapter 11 (continued)
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Efficiency of in Binary Trees
Week 8 - Wednesday CS221.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Problems with Linked List (as we’ve seen so far…)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Cse 373 April 14th – TreEs pt 2.
COMP 103 Binary Search Trees.
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Binary Trees.
Lesson Objectives Aims
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees.
Trees CMSC 202, Version 5/02.
Lecture 12 CS203 1.
Binary Tree Traversals
CMSC 202 Trees.
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Binary Trees.
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Data Structures II AP Computer Science
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

CS2005 Week 8 Lectures Maps & Binary Trees

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

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

Map equivalent to table CS2005 Map equivalent to table

CS2005 Map equivalent to set

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

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’

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

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

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

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

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

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

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

CS2005 Sample Code Html document Binary tree code test program

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