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

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
CS 171: Introduction to Computer Science II
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.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Marc Smith and Jim Ten Eyck
Binary Search Trees Chapter 7 Objectives
Data Structures Using C++1 Chapter 11 Binary Trees.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
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.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Data Structures Using Java1 Chapter 10 Binary Trees.
Discrete Mathematics Chapter 5 Trees.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
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/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Foundation of Computing Systems Lecture 4 Trees: Part I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Data Structure and Algorithms
AA Trees.
Trees Chapter 11 (continued)
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Section 8.1 Trees.
Data Structures Using C++ 2E
Data Structures Using Java
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.
AVL Trees CENG 213 Data Structures.
Binary Search Trees.
Binary Search Trees Chapter 7 Objectives
Chapter 20: Binary Trees.
Trees.
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

DictionaryADT and Trees

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

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

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

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

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

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

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

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

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

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

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

Linked trees: the node class Consider the code for BinaryTreeNode p 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

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

Linked tree implementations Consider the code for BinaryTreeNode p 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

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

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

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

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

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

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

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

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