CS Data Structures Chapter 15 Trees Mehmet H Gunes

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Chapter 10 Trees. Tree Definition 1. A tree is a connected undirected graph with no simple circuits. Theorem 1. An undirected graph is a tree if and.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
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.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Fundamentals of Python: From First Programs Through Data Structures
Marc Smith and Jim Ten Eyck
Binary Search Trees Chapter 7 Objectives
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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 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.
COSC2007 Data Structures II
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
1 Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Trees.
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.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
Heaps Chapter 17 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
CS 302 Data Structures Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Section 8.1 Trees.
Chapter 16 Tree Implementations
Binary Trees, Binary Search Trees
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Chapter 16 Tree Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees Chapter 10.
Trees.
CSC 143 Java Trees.
Yan Shi CS/SE 2630 Lecture Notes
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

CS 302 - Data Structures Chapter 15 Trees Mehmet H Gunes Modified from authors’ slides

Content Terminology The ADT Binary Tree The ADT Binary Search Tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Terminology Use trees to represent relationships Trees are hierarchical in nature “Parent-child” relationship exists between nodes in tree. Generalized to ancestor and descendant Lines between the nodes are called edges A subtree in a tree is any node in the tree together with all of its descendants Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len

A Tree Has a Root Node Owner Jake ROOT NODE Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len

Leaf Nodes have No Children Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len

A Tree Has Levels Owner LEVEL 0 Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len LEVEL 0

Level One Owner Jake Manager Chef Brad Carol LEVEL 1 Waitress Waiter Cook Helper Joyce Chris Max Len

Level Two Owner Jake Manager Chef Brad Carol LEVEL 2 Waitress Waiter Cook Helper Joyce Chris Max Len LEVEL 2

A Subtree Owner Jake Manager Chef Brad Carol LEFT SUBTREE OF ROOT NODE Waitress Waiter Cook Helper Joyce Chris Max Len LEFT SUBTREE OF ROOT NODE

Another Subtree Owner Jake Manager Chef Brad Carol RIGHT SUBTREE Waitress Waiter Cook Helper Joyce Chris Max Len RIGHT SUBTREE OF ROOT NODE

Terminology (a) A tree; (b) a subtree of the tree in part a Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Terminology (a) An organization chart; (b) a family tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Kinds of Trees General Tree Set T of one or more nodes such that T is partitioned into disjoint subsets A single node r , the root Sets that are general trees, called subtrees of r Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Kinds of Trees n -ary tree set T of nodes that is either empty or partitioned into disjoint subsets: A single node r , the root n possibly empty sets that are n -ary subtrees of r Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Kinds of Trees Binary tree Set T of nodes that is either empty or partitioned into disjoint subsets Single node r , the root Two possibly empty sets that are binary trees, called left and right subtrees of r Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

A Binary Tree V Q L E T A K S

How many leaf nodes? V Q L T E A K S

How many descendants of Q? V Q L T E A K S

How many ancestors of K? Q V T K S A E L

Implementing a Binary Tree with Pointers and Dynamic Data V Q L T E A K S

Node Terminology for a Tree Node

Example: Algebraic Expressions. Binary trees that represent algebraic expressions Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Binary Search Tree For each node n, a binary search tree satisfies the following three properties: n ’s value is greater than all values in its left subtree T L n ’s value is less than all values in its right subtree T R Both T L and T R are binary search trees Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Binary Search Tree A binary search tree of names Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Shape of a binary search tree . . Depends on its key values and their order of insertion. Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order. The first value to be inserted is put into the root node. ‘J’

Inserting ‘F’ into the BST Thereafter, each value to be inserted begins by comparing itself to the value in the root node, moving left if it is less, or moving right if it is greater. This continues at each level until it can be inserted as a new leaf. ‘J’ ‘E’

Inserting ‘F’ into the BST Begin by comparing ‘F’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. ‘J’ ‘E’ ‘F’

Inserting ‘F’ into the BST Begin by comparing ‘T’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. ‘J’ ‘E’ ‘T’ ‘F’

Inserting ‘A’ into the BST Begin by comparing ‘A’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. ‘J’ ‘E’ ‘T’ ‘A’ ‘F’

What binary search tree . . . is obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order? ‘A’

Binary search tree . . . obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order. ‘A’ ‘E’ ‘F’ ‘J’ ‘T’

Another binary search tree ‘J’ ‘E’ ‘T’ ‘A’ ‘H’ ‘M’ ‘K’ ‘P’ Add nodes containing these values in this order: ‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’

Is ‘F’ in the binary search tree? ‘J’ ‘E’ ‘T’ ‘A’ ‘H’ ‘M’ ‘V’ ‘D’ ‘K’ ‘Z’ ‘P’ ‘B’ ‘L’ ‘Q’ ‘S’

The Height of Trees Definition of the level of a node n : If n is the root of T , it is at level 1 If n is not the root of T , its level is 1 greater than the level of its parent Height of a tree T in terms of the levels of its nodes If T is empty, its height is 0 If T is not empty, its height is equal to the maximum level of its nodes Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Height of Trees Binary trees with the same nodes but different heights Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Full, Complete, and Balanced Binary Trees A full binary tree of height 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Full, Complete, and Balanced Binary Trees Definition of a full binary tree If T is empty, T is a full binary tree of height 0 If T is not empty and has height h > 0, T is a full binary tree if its root’s subtrees are both full binary trees of height h – 1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Full, Complete, and Balanced Binary Trees A complete binary tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Definitions Full Binary Tree: A binary tree in which all of the leaves are on the same level and every nonleaf node has two children

Definitions (cont.) Complete Binary Tree: A binary tree that is either full or full through the next-to-last level, with the leaves on the last level as far to the left as possible

Examples of Different Types of Binary Trees

The Maximum and Minimum Heights of a Binary Tree The maximum height of an n -node binary tree is n Binary trees of height 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Maximum and Minimum Heights of a Binary Tree Counting the nodes in a full binary tree of height h Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Facts about Full Binary Trees A full binary tree of height h ≥ 0 has 2 h – 1 nodes You cannot add nodes to a full binary tree without increasing its height The maximum number of nodes that a binary tree of height h can have is 2 h – 1 The minimum height of a binary tree with n nodes is [log 2 ( n + 1)] Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Maximum and Minimum Heights of a Binary Tree Filling in the last level of a tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Traversals of a Binary Tree General form of recursive transversal algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Traversals of a Binary Tree Preorder traversal Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Traversals of a Binary Tree Three traversals of a binary tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Printing all the Nodes in Order

Three Tree Traversals

Traversals of a Binary Tree Inorder traversal. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Traversals of a Binary Tree Postorder traversal. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Binary Tree Operations Test whether a binary tree is empty. Get the height of a binary tree. Get the number of nodes in a binary tree. Get the data in a binary tree’s root. Set the data in a binary tree’s root. Add a new node containing a given data item to a binary tree. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Binary Tree Operations Remove the node containing a given data item from a binary tree. Remove all nodes from a binary tree. Retrieve a specific entry in a binary tree. Test whether a binary tree contains a specific entry. Traverse the nodes in a binary tree in preorder, inorder, or postorder. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Binary Tree Operations UML diagram for the class BinaryTree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Binary Tree Operations Formalized specifications demonstrated in interface template for binary tree Listing 15-1 Note method names matching UML diagram Figure 15-12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The ADT Binary Search Tree ADT binary tree ill suited for search for specific item Binary search tree solves problem Properties of each node, n n ’s value greater than all values in left subtree TL n ’s value less than all values in right subtree TR Both TR and TL are binary search trees Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The ADT Binary Search Tree A binary search tree of names Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The ADT Binary Search Tree Binary search trees with the same data Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The ADT Binary Search Tree Binary search trees with the same data Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The ADT Binary Search Tree Binary search trees with the same data Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Binary Search Tree Operations Test whether binary search tree is empty. Get height of binary search tree. Get number of nodes in binary search tree. Get data in binary search tree’s root. Insert new item into binary search tree. Remove given item from binary search tree. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Binary Search Tree Operations Remove all entries from binary search tree. Retrieve given item from binary search tree. Test whether binary search tree contains specific entry. Traverse items in binary search tree in Preorder Inorder Postorder. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Searching a Binary Search Tree Search algorithm for binary search tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Searching a Binary Search Tree An array of names in sorted order Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Creating a Binary Search Tree Empty subtree where the search algorithm terminates when looking for Frank Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Traversals of a Binary Search Tree Algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Deleting a Leaf Node Delete Z

Deleting a Node with One Child Delete R

Deleting a Node with Two Children Delete Q

Efficiency of Binary Search Tree Operations The Big O for the retrieval, insertion, removal, and traversal operations of the ADT binary search tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013