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,

Slides:



Advertisements
Similar presentations
 The implementation of this class is testable on the AP CS AB exam.  Stacks are last in first out. LIFO.  A stack is a sequence of items of the same.
Advertisements

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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Marc Smith and Jim Ten Eyck
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
1 Sets and Maps Starring: keySet Co-Starring: Collections.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
Starting at Binary Trees
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 6 Data Structures.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Binary Search Trees Chapter 7 Objectives
Binary Trees.
Efficiency of in Binary Trees
Binary Search Tree (BST)
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Ch. 11 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.
Chapter 21: Binary Trees.
Binary Trees.
Binary Trees.
Lecture 12 CS203 1.
Binary Trees, Binary Search Trees
Binary Trees.
CSC 143 Binary Search Trees.
Data Structures II AP Computer Science
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

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, each of you leaves with something new and exciting learned about trees. Try not to lose sight of the forest through the trees. Sorry, this was all very bad. I hope it didn’t leaf a bad taste in your mouth.

3 TREES A binary tree is a set of elements that is either empty or contains a single element (the root of the tree) and whose remaining elements are partitioned into two disjoint subsets, each of which itself is a binary tree.

4 H D A G E P LW R root Right subtree rooted at P Left subtree

5 H D A G E P LW R root level 0 level 1 level 2 level 3 Right subtree Left subtree

6 Full Binary Tree of Level n

7 A B F C ED JIHK LM G N O A is the root of the tree. A is the parent of B and C. A is an ancestor of all nodes.

8 A B F C ED JIHK LM G N O B and C are siblings. J is a descendent of B.

9 Height of binary tree : Number of nodes on the longest path from the root to a leaf. The height of the empty tree is 0. The height of a single node tree is 1. Note: (not an AP term) the definition of “height” is not from some “cs bible” – some will define other ways

10 A B F C ED JIHK LM G N O Height of binary tree ?

11 A B F C ED JIHK LM G N O Height of binary tree ? Height = 4

12 Full Binary : A recursive definition A binary tree is full if the tree is empty or if the tree's left and right subtrees have the same height and both are full

13 Binary Search Tree A binary search tree : every node in the left subtree is less than or equal to its parent node. Every node in the right subtree is greater than its parent node When the tree is traversed in order, the values of the nodes will be in order.

14 To insert to a binary search tree: if value less than go left else go right

15 Insert :

16 15 Insert :

17 15 Insert :

18 15 Insert :

19 15 Insert :

20 15 Insert :

21 15 Insert :

22 15 Insert :

23 15 Insert :

24 15 Insert :

25 15 Insert :

26 Tree Traversals See also: animations on web site

27 A B F C ED JIHK LM G N O Inorder traversal of a binary tree left…root…right

28 A B F C ED JIHK LM G N O Inorder traversal of a binary tree left…root…right H D I B J E K A L F M C N G O

29 A B F C ED JIHK LM G N O Preorder traversal of a binary tree: root…left…right

30 A B F C ED JIHK LM G N O Preorder traversal of a binary tree: root…left…right A B D H I E J KC F L M G N O

31 A B F C ED JIHK LM G N O Postorder traversal of a binary tree: left…right…ROOT

32 A B F C ED JIHK LM G N O Postorder traversal of a binary tree: left…right…ROOT H I D J K E B L M F N O G C A

33 L B F W EX AQ P G T E Inorder traversal ?

34 L B F W EX AQ P G T E Inorder traversal : XA B E Q L P F W T G E

35 L B F W EX AQ P G T E Preorder traversal ?

36 L B F W EX AQ P G T E Preorder traversal: L B X A E Q W F PG T E

37 L B F W EX AQ P G T E Postorder traversal ?

38 L B F W EX AQ P G T E Postorder traversal : A X Q E B P F T E G W L

39 breadth-first-order tree traversal ROW (or level) order traversal A BC DE FG HI

40 breadth-first-order tree traversal ROW (or level) order traversal A BC DE FG HI A B C D E F G H I

41 15 Inorder Traversal ?

42 15 Inorder Traversal :

43 15 Preorder Traversal ?

44 15 Preorder Traversal :

45 15 Postorder Traversal ?

46 15 Postorder Traversal :

47 15 Our Tree: What is the height of the tree?

48 15 Height : Height = 5

49 Deleting a node from a binary tree L D H C A F J P

50 To delete a leaf... L D H C A F J P

51 To delete a leaf... L D H C A F J P Set appropriate parent to NULL

52 To delete a leaf... L D H C A F P

53 Deleting a node with one child... L D H C A F J P

54 Deleting a node with one child... L D H C A F J P Make appropriate left or right reference of parent skip over the deleted node and reference the child of the node we want to delete

55 Deleting a node with one child... L D H A F J P

56 Deleting a node with 2 children... L D H C A F J P

57 Deleting a node with 2 children... L D H C A F J P Go once left and as far right as you can for node’s replacement.

58 Deleting a node with 2 children... D H C A F J P Go once left and as far right as you can.

59 Binary Search Trees AP Implementation

60 public class TreeNode { private Object value; private TreeNode left; private TreeNode right; public TreeNode(Object initValue) { value = initValue; left = null; right = null; } public TreeNode(Object initValue, TreeNode initLeft, TreeNode initRight) { value = initValue; left =initLeft; right = initRight; }

61 TreeNode…. public Object getValue() { return value; } public TreeNode getLeft() { return left; } public TreeNode getRight() { return right; }

62 TreeNode…. public void setValue(Object theNewValue) { value = theNewValue; } public void setLeft(TreeNode theNewLeft) { left = theNewLeft; } public void setRight(TreeNode theNewRight) { right = theNewRight; } }

63 Implementing Binary Search Tree Class common behaviors (insertions, deletions, traversals, iterators) isEmpty() insert print (inorder traversal) search

64 TreeSet The TreeSet class uses a form of balanced binary trees that guarantees that adding and removing an element takes O(log n) time. We know that a good hashing function can give us a retrieval with efficiency O(1) but if we also wanted to be able to list our data in order, a hash table would not be the appropriate choice. The TreeSet class implements the Set interface.

65 interface java.util.Set MethodMethod Summary boolean add(Object obj) Adds the parameter obj as an element to this set if it is not already in the set and returns true. If the element is already in the set, returns false. boolean contains(Object obj) Returns true if this set contains the element obj, otherwise returns false. boolean remove(Object obj) Removes the element obj from this set and returns true if the obj is in this set; if not present returns false. int size() Returns the number of elements in this set. Iterator iterator() Returns an Iterator that provides access to the elements of this set.

66 TreeSet… A TreeSet requires that its elements be comparable. compareTo is defined for the objects placed in the TreeSet. the elements in a tree are ordered For your own classes, realize the Comparable interface define compareTo or provide a Comparator ( Comparator is not in AP Subset) Since a TreeSet implements Set, a TreeSet contains no duplicates. A TreeSet guarantees reasonable search performance (O(log n)) and allows for visiting the elements in order.

67 Nice practice with iterators. The class TreeSetWithOps has all functionality of TreeSet and also includes the methods setIntersection, setUnion, setDifference, isSubset, and isProperSubset. Implement TreeSetWithOps.

68 Maps A map is a data type that keeps associations between keys and values. Each key in a map has a unique value. But a value may be associated with more than one key. A mapping of your college friends to the university that they attend. ("Owen" maps to "Duke University", "Fran" maps to "Drew University"). A mapping of login ids to passwords. Each association has a key mapped to a value. Associations can be put in, changed, and located in a map.

69 TreeMap MethodMethod Summary boolean put(Object key, Object value) Associates value with key in this map so that get(key ) returns value. boolean get(Object key) Return the value associated with key in this map, or null if there is no value associated with key. boolean containsKey(Object key) Returns true if there is a value associated with key in this map, otherwise returns false. int size() Returns the number of keys in this map. Set keySet() Returns a set of the keys in the maps. Object remove(Object key) Removes the mapping for this key from the map and returns the value previously associated with the key in the map.

70 Map The Map interface requires that the keySet method be implemented. The keySet method produces a Set of keys. We can visit all of the elements of a TreeMap by iterating through the keys in the set that the keyset method produces. The Map method get will return the value associated with a map key. A TreeMap keeps the elements in an order (according to the key) from smallest to largest.

71 Multiple Choice Sample Question: AP Course Description The following integers are inserted into an empty binary search tree in the following order Which traversal of the tree would produce the following output? (A) Preorder (B) Inorder (C) Postorder (D) Reverse postorder (E) Level-by-level