1 / 129

Chapter 10 Trees

Chapter 10 Trees. Section 1 - General Information about Trees Section 2 - Review of compareTo and Comparator Classes Section 3 - The java.util.TreeSet Class Section 4 - The java.util.TreeMap Class Section 5 - A Do-It-Yourself BST Class. Go. Go. Go. Go. Go. 1. Objectives of Chapter 10.

Télécharger la présentation

Chapter 10 Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10Trees Section 1 - General Information about Trees Section 2 - Review of compareTo and Comparator Classes Section 3 - The java.util.TreeSet Class Section 4 - The java.util.TreeMap Class Section 5 - A Do-It-Yourself BST Class Go Go Go Go Go 1

  2. Objectives of Chapter 10 • Learn about general trees and binary trees. • Learn about binary search trees. • Learn in detail the concepts of a set and a map and how the java.util.TreeSet and java.util.TreeMap classes can be used as data structures in a program. • Learn how to represent a tree node using the do-it-yourself TreeNode class. • Learn how to write the templated, do-it-yourself binary search tree class named MyTreeSet. 2

  3. Chapter 10 - Section 1 General Information about Trees 3

  4. 10.1 Various Applications of Trees • Organizational charts or hierarchies • Game strategies • Artificial Intelligence - 20Q game • A system that implements branching processes • Fast Data retrieval (searching) • Decision charts (like flow charts) 4

  5. 10.1 Trees are used for Organizational Charts Head Master Business Director Maintenance Director Assistant Head Master Business Personnel Maintenance Personnel LS Director US Director MS Director US Faculty MS Faculty LS Faculty 5

  6. 10.1 Trees Used to Develop Game Strategies 6

  7. 10.1 General Trees Versus Binary Trees • With a general tree, each node can have any number of children. • A tree in which each node has no more than two children is called a binary tree. If the node has two children, then they are referred to as the left child and the right child. • We will work exclusively with binary trees in this chapter more specifically with what are called binary search trees. However, we will discuss tree terminology in light of all kinds of trees. 7

  8. 10.1 Basic Tree Terminology Root Right child Left child node Number of levels (equals 5 here) node’s right subtree Leaves (nodes with no children) In a general tree, a node may have any number of children. In reality, having trees with nodes that can have more than two children is not practical, but it is possible. 8

  9. 10.1 Any Node is the root of a Subtree Each node in a tree is the root of its own subtree. root of black subtree 9

  10. 10.1 The Levels in a Tree root Identification of levels in a tree. This is a general tree but it would be similar for a binary tree. Notice the root is always at the level identified as zero. 10

  11. 10.1 The Height of a Tree If a tree only has a root node, then its height is 1. If the root node has only a left child, a right child or both, then its height is 2. So the height is based on the number of levels. 11

  12. 10.1 A Shallow Tree Can Have Many Nodes A shallow tree can hold many nodes, but it does it in only a few levels. This tree is relatively shallow because it has a height of only 4. 12

  13. 10.1 The Number of Nodes in a Binary Tree A full binary tree is one that has each level completely full, meaning that each node has a left and right child, except the last level. The number of nodes in a full binary tree can easily be calculated based on the height of the tree: # of nodes = 2h - 1. For a full tree of four levels there are 15 nodes, since 24 - 1 = 16 -1 = 15. 13

  14. 10.1 Binary Tree Properties • A shallow tree can hold many nodes. For example, a tree with 20 levels can have 220- 1 or approximately 1,000,000 nodes. • The path to the bottom is relatively short as compared to the total number of nodes. This is great for searching! • The design of a binary tree lends itself to recursive operations, which can be much easier to apply because they can be visually traced. 14

  15. 10.1 A Degenerate Tree Has A Linear Shape A tree with a nearly linear shape would hold only a few nodes in each level. We sometimes call this a degenerate tree. 15

  16. a BST not a BST 15 / \ 8 20 / \ 1 12 15 / \ 12 20 / \ 1 8 10.1 A Binary Search Tree (BST) • A Binary Search Tree is a special case of a binary tree and must contain Comparableobjects. • For each node, all the values in its left subtree are smaller than the value in the node and all the values in its right subtree are larger than the value in the node. 16

  17. 10.1 Adding Values to a Binary Search Tree The add method of a Binary Search Tree class controls how values are placed in a BST. It’s the order that the values are added to the tree that determines what the tree looks like. 50 67 37 43 25 53 70 69 28 39 47 68 49 42 38 26 17

  18. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. First, we add 50. 50 18

  19. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 67. 50 67 19

  20. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 37. 50 67 37 20

  21. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 43. 50 67 37 43 21

  22. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 25. 50 67 37 43 25 22

  23. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 70. 50 67 37 43 25 70 23

  24. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 53. 50 67 37 43 25 53 70 24

  25. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 69. 50 67 37 43 25 53 70 69 25

  26. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 68. 50 67 37 43 25 53 70 69 68 26

  27. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 28. 50 67 37 43 25 53 70 69 28 68 27

  28. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 26. 50 67 37 43 25 53 70 69 28 68 26 28

  29. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 47. 50 67 37 43 25 53 70 69 28 47 68 26 29

  30. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 39. 50 67 37 43 25 53 70 69 28 39 47 68 26 30

  31. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 49. 50 67 37 43 25 53 70 69 28 39 47 68 49 26 31

  32. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Next, we add 38. 50 67 37 43 25 53 70 69 28 39 47 68 49 38 26 32

  33. 10.1 Adding Values to a Binary Search Tree Let’s see how this BST is constructed by adding these values in this order: 50, 67, 37, 43, 25, 70, 53, 69, 68, 28, 26, 47, 39, 49, 38, 42. Last, we add 42. 50 67 37 43 25 53 70 69 28 39 47 68 49 42 38 26 33

  34. 10.1 Traversing a Binary Tree Any binary tree, even if it is not a binary search tree, can be traversed in different ways. The three primary methods of traversing a binary tree are: preorder inorder postorder Let’s see how each of them would traverse the values in this tree. 43 39 47 49 42 38 34

  35. 10.1 Traversing a Binary Tree Using Preorder When preorder is used, the root is visited first followed by the left child and then the right child, but this algorithm is applied recursively for each node that is visited. root left child right child If we printed the nodes in this tree according to preorder, then the output would be: 43, 39, 38, 42, 47, 49. 43 39 47 49 42 38 35

  36. 10.1 Traversing a Binary Tree Using Inorder When inorder is used, the left child is visited first followed by the root and then the right child, but this algorithm is applied recursively for each node that is visited. left child root right child If we printed the nodes in this tree according to inorder, then the output would be: 38, 39, 42, 43, 47, 49. 43 39 47 49 42 38 36

  37. 10.1 Traversing a Binary Tree Using Postorder When postorder is used, the left child is visited first followed by the right child and then the root, but this algorithm is applied recursively for each node that is visited. left child right child root If we printed the nodes in this tree according to postorder, then the output would be: 38, 42, 39, 49, 47, 43. 43 39 47 49 42 38 37

  38. 10.1 Searching a BST for a Value Using the following tree, let’s consider the process for searching for a value in a tree. 50 67 37 43 25 53 70 38

  39. 10.1 The Efficiency of Searching a BST What is the maximum number of checks that need to be made to see if a value is in the tree? 50 67 37 43 25 53 70 39

  40. 10.1 The Efficiency of Searching a BST What is the maximum number of checks that need to be made to see if a value is in the tree? 3 50 67 37 43 25 53 70 40

  41. 10.1 The Efficiency of Searching a BST What is the Big O Efficiency of Searching a full BST like this one if you always begin searching at the root? 50 67 37 43 25 53 70 41

  42. 10.1 The Efficiency of Searching a BST What is the Big O Efficiency of Searching a full BST like this one if you always begin searching at the root? O(log2 n) + 1 Here O(log2 7) + 1 isequivalent toO(log2 4) + 1 = 2 + 1 = 3. 50 67 37 43 25 53 70 Since 7 is not a power of 2, we go to the next lowest power of 2, which is 4. If there is an 8th node, then …. 42

  43. 10.1 The Efficiency of Searching a BST Here O(log2 8) + 1 isequivalent toO(log2 23) + 1 = 3 + 1 = 4. You can see the formula O(log2 n) + 1 still works for 8 and gives 4, the number of checks needed to find any number including 19. 50 67 37 43 25 53 70 19 43

  44. 10.1 Benefits of BSTs • BSTs combine the benefits of • quick searching - O(log2 n) like binary search for arrays • linked lists for dynamically inserting and deleting values 44

  45. 10.1 Binary Search Tree Properties The properties of any binary search tree: Searching requires O(log2 n) efficiency where n is the number of nodes. Remember for Big O purposes O(log2 n) is the same as O(log2 n) + 1. But to find the maximum number of checks we need to actually use O(log2 n) + 1. In searching for a value in a BST, we check each node to see if it contains the value we are looking for and if it is not, then checking to see if the target value is larger or smaller than the value in the current node. So at each node, a decision is made on whether to proceed left or right based on the comparison of the two values. 45

  46. 10.1 No Duplicate Values in a BST It is important to note thattrees never contain duplicate values. If you think about it, in the real world, there wouldn’t be a need for a database of persons stored in a BST to have the exact same person twice in the database. This would just cause confusion. Java’s library classes TreeSet and TreeMap don’t allow duplicate values, so we will proceed with the understanding that any BST we will work with will not contain duplicate values. By the way, if you try to put a duplicate value in a TreeSet, then Java will not allow you to do so, because it does a check first before adding to see if that value is already in the BST. 46

  47. 10.1 Smallest & Largest Values of BSTs Where are the smallest and largest values in a BST? Smallest: Largest: 47

  48. 10.1 Smallest & Largest Values of BSTs Where are the smallest and largest values in a BST? Smallest: left most node Largest: right most node 48

  49. 10.1 Smallest & Largest Values of BSTs The smallest node The largest node 49

  50. 50 30 75 25 40 60 90 20 70 69 72 50 50 60 60 30 75 30 75 25 40 90 25 40 90 70 70 20 20 69 72 69 72 10.1 Removing Values from a BST Remove the value 50 Step 1: Find the smallest node in the right subtree of 50, which is 60. Step 2: Unlink 60 and promote its right child (70) into its place. Note: If 60 had a left child then it wouldn’t be the smallest value in 50’s right subtree. Step 3: Link that note in place of the root and remove the old root, i.e. overwrite 50 with 60. 50

More Related