Presentation is loading. Please wait.

Presentation is loading. Please wait.

DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.

Similar presentations


Presentation on theme: "DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT."— Presentation transcript:

1 DictionaryADT and Trees

2 Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT and Trees 2/23

3 Characteristics of sorting algorithms Consider the following code: import java.util.*; Public interface DictionaryADT { public void insert (String key, T value); public T find (String key); public boolean isEmpty(); public int size(); public Iterator iterator(); } DictionaryADT and trees p. 3/23

4 What could you use DictionaryADT for? Suppose you are starting a small business and you want an application that will let you store and use information about your inventory. Your first task is to choose what kind of store it will be and identify at least three different items that the store will carry. Your program should: Let you enter information about an item Store the information Let you search for an item by name (or id) Let you delete items DictionaryADT and Trees p. 4/23

5 Trees Consider Figure 9.2 on page 243. Which is the root node? Which are the leaf nodes? Which is the left child of node D? node B? DictionaryADT and Trees p. 5/23

6 Trees (2) Consider Figure 9.1 on page 242. What is the path from the root to node N? What is the level of node K? Is the tree complete? Why or why not? DictionaryADT and Trees p. 6/23

7 Balanced trees: definitions A tree with minimum height A tree where for each node, the heights of its two subtrees are equal or differ by one. A tree where for each node, the numbers of nodes in its two subtrees are equal or differ by one. Discussion: See if you can find examples of trees that satisfy at least one but not all of these criteria. DictionaryADT and Trees p. 7/23

8 Tree traversals (1) Consider Figure 9.7 on page 248. The pre-order traversal of that tree would visit nodes in the order: A. A B C D E B. A B D E C C. D B E A C D. D E B C A E. None of the above DictionaryADT and Trees p. 8/23

9 Tree traversals (2) Consider Figure 9.7 on page 248. The in-order traversal of that tree would visit nodes in the order: A. A B C D E B. A B D E C C. D B E A C D. D E B C A E. None of the above DictionaryADT and Trees p. 9/23

10 Tree traversals (3) Consider Figure 9.7 on page 248. The post-order traversal of that tree would visit nodes in the order: A. A B C D E B. A B D E C C. D B E A C D. D E B C A E. None of the above DictionaryADT and Trees p. 10/23

11 Tree traversals (4) Consider Figure 9.7 on page 248. The breadth-first traversal of that tree would visit nodes in the order: A. A B C D E B. A B D E C C. D B E A C D. D E B C A E. None of the above DictionaryADT and Trees p. 11/23

12 Array tree implementations Consider Figure 10.10 on p. 310 (left side). That tree would be represented in an array as: A. 13 7 15 5 10 3 B. 13 7 15 5 10 null null 3 C. 3 5 10 7 15 13 D. 3 5 7 10 13 null 15 E. None of the above DictionaryADT and Trees p. 12/23

13 Linked trees: the node class Consider the code for BinaryTreeNode p. 267. Why are the variables protected ? Write a get method for element. Write get and set methods for left and right. Why not write a set method for element ? How does this compare with LinearNode ? DictionaryADT and Trees p. 13/23

14 Linked trees: the tree class Consider the code for LinkedBinarySearchTree on the handout. Trace what happens if: nodes containing the Strings “cat”, “dog”, “bird”, and “aardvaark” are added to a tree. The “aardvaark” node is deleted. Instead, the first node deleted is the “bird” node. Instead, the first node deleted is the “cat” node. DictionaryADT and Trees p. 14/23

15 Linked tree implementations Consider the code for BinaryTreeNode p. 267. Why are the variables protected ? Write a get method for element. Write get and set methods for left and right. Why not write a set method for element ? How does this compare with LinearNode ? DictionaryADT and Trees p. 15/23

16 Balanced binary search trees Why is it important for a tree to be balanced? Example: what happens when we insert the elements 1 2 3 4 5 6 7 In that order into a binary search tree? DictionaryADT and Trees p. 16/23

17 Balanced binary search trees We want the maximum path length in a tree to be as small as possible – but what is the smallest possible path length for a tree of n nodes? DictionaryADT and Trees p. 17/23

18 Balanced binary search trees What is a right rotation? Explain by walking through Figure 10.10 – what is happening there? DictionaryADT and Trees p. 18/23

19 Balanced binary search trees What is a left rotation? Explain by walking through Figure 10.11 – what is happening there? DictionaryADT and Trees p. 19/23

20 Balanced binary search trees What is a rightleft rotation? Explain by walking through Figure 10.12 – what is happening there? DictionaryADT and Trees p. 20/23

21 Balanced binary search trees When do you need: A left rotation A right rotation A leftright rotation A right rotation DictionaryADT and Trees p. 21/23

22 Balanced binary search trees Work with your group to complete: Exercises 1, 2, 3, 5 At the end of chapter 10 Binary Search Trees. DictionaryADT and Trees p. 22/23

23 Coming attractions Next time, we’ll look at a new data structure that implements DictionaryADT in a very different way. Homework: read chapter 14 Hashing (or the equivalent in the earlier edition). DictionaryADT and Trees p. 23/23


Download ppt "DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT."

Similar presentations


Ads by Google