23
DictionaryADT and Trees

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

  • View
    222

  • Download
    2

Embed Size (px)

Citation preview

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

DictionaryADT and Trees

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

OverviewWhat is the DictionaryADT?What are trees?Implementing DictionaryADT with binary treesBalanced trees

DictionaryADT and Trees 2/23

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

Characteristics of sorting algorithmsConsider the following code:import java.util.*;

Public interface DictionaryADT<T> {

public void insert (String key, T value);

public T find (String key);

public boolean isEmpty();

public int size();

public Iterator<T> iterator();} DictionaryADT and trees p. 3/23

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

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 itemStore the informationLet you search for an item by name (or id)Let you delete items

DictionaryADT and Trees p. 4/23

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

TreesConsider 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

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

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

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

Balanced trees: definitionsA tree with minimum heightA 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

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

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 EB.A B D E CC.D B E A CD.D E B C AE.None of the above

DictionaryADT and Trees p. 8/23

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

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 EB.A B D E CC.D B E A CD.D E B C AE.None of the above

DictionaryADT and Trees p. 9/23

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

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 EB.A B D E CC.D B E A CD.D E B C AE.None of the above

DictionaryADT and Trees p. 10/23

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

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 EB.A B D E CC.D B E A CD.D E B C AE.None of the above

DictionaryADT and Trees p. 11/23

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

Array tree implementationsConsider Figure 10.10 on p. 310 (left side). That tree

would be represented in an array as:A.13 7 15 5 10 3B.13 7 15 5 10 null null 3C.3 5 10 7 15 13D.3 5 7 10 13 null 15E.None of the above

DictionaryADT and Trees p. 12/23

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

Linked trees: the node classConsider 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

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

Linked trees: the tree classConsider 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

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

Linked tree implementationsConsider 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

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

Balanced binary search treesWhy is it important for a tree to be balanced?Example: what happens when we insert the

elements1 2 3 4 5 6 7

In that order into a binary search tree?

DictionaryADT and Trees p. 16/23

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

Balanced binary search treesWe 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

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

Balanced binary search treesWhat is a right rotation? Explain by walking

through Figure 10.10 – what is happening there?

DictionaryADT and Trees p. 18/23

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

Balanced binary search treesWhat is a left rotation? Explain by walking

through Figure 10.11 – what is happening there?

DictionaryADT and Trees p. 19/23

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

Balanced binary search treesWhat is a rightleft rotation? Explain by walking

through Figure 10.12 – what is happening there?

DictionaryADT and Trees p. 20/23

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

Balanced binary search treesWhen do you need:A left rotationA right rotationA leftright rotationA right rotation

DictionaryADT and Trees p. 21/23

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

Balanced binary search treesWork with your group to complete:Exercises 1, 2, 3, 5At the end of chapter 10 Binary Search Trees.

DictionaryADT and Trees p. 22/23

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

Coming attractionsNext 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