Trees Chapter 15.

Slides:



Advertisements
Similar presentations
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
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.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Fundamentals of Python: From First Programs Through Data Structures
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
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. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
CSCE 210 Data Structures and Algorithms
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Week 6 - Wednesday CS221.
CS 302 Data Structures Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Section 8.1 Trees.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
8.2 Tree Traversals Chapter 8 - Trees.
Binary Trees, Binary Search Trees
Trees Chapter 10.
Trees.
Chapter 20: Binary Trees.
B.Ramamurthy Chapter 9 CSE116A,B
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
A Binary Tree is a tree in which each node has at most 2 children
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

Trees Chapter 15

Trees Lists, stacks, and queues are linear in their organization of data Items are one after another. In this chapter, we organize data in a nonlinear, hierarchical form Item can have more than one immediate successor

Terminology Use trees to represent relationships Diagram represents the recursive calls for the fibonacci sequence as a tree Vertex or node Edges

FIGURE 15-1 A tree and one of its subtrees Terminology Trees are hierarchical in nature Means a parent-child relationship between nodes FIGURE 15-1 A tree and one of its subtrees

Terminology More tree hierarchies

Terminology

Kinds of Trees General tree Binary tree Set T of one or more nodes T is partitioned into disjoint subsets Binary tree Set of T nodes – either empty or partitioned into disjoint subsets Single node r, the root Two (possibly empty) sets – left and right subtrees

Kinds of Trees Binary trees that represent algebraic expressions – different tree traversal algorithms generate infix, prefix or postfix representations of the expression. Can we figure out the traversal strategies?

Kinds of Trees A binary search tree of names. What do we notice about the relationship of the values stored in the tree? Which tree traversal algorithm will give us the values in sorted order?

The Height of Trees Level of a node, n Height of a tree If n is root, level 1 If n not the root, level is 1 greater than level of its parent Height of a tree Number of nodes on longest path from root to a leaf T empty, height 0 T not empty, height equal to max level of nodes

The Height of Trees Binary trees with the same number of nodes but different heights

Full, Complete, and Balanced Binary Trees A full binary tree of height 3 – all parents have two (k) children and all leaves are at the same level.

Full, Complete, and Balanced Binary Trees A complete binary tree (remember our Heap?) – tree is filled from left to right, only lower two levels of leaves differ in height: leaves at height h and height h-1.

The Maximum and Minimum Heights of a Binary Tree Binary tree with n nodes Max height is n To minimize height of binary tree of n nodes Fill each level of tree as completely as possible A complete tree meets this requirement

The Maximum and Minimum Heights of a Binary Tree Binary trees of height 3 A binary tree of height n will have a minimum of ____ nodes? And a maximum of _____ nodes? (Think about a power of 2?)

The Maximum and Minimum Number of Nodes in a Binary Tree Maximum - Counting the nodes in a full binary tree of height h

The ADT Binary Tree Operations of ADT binary tree Add, remove a node Set, retrieve data Test for empty Traversal operations that visit every node Traversal can visit nodes in several different orders

Traversals of a Binary Tree Pseudo-code for general form of a recursive traversal algorithm (this is a preorder traversal – root, then left, then right).

Traversals of a Binary Tree Options for when to visit the root Preorder: before it traverses both subtrees Inorder: after it traverses left subtree, before it traverses right subtree Postorder: after it traverses both subtrees Note traversal is O(n)

Traversals of a Binary Tree Three traversals of a binary tree

Traversals of a Binary Tree Preorder traversal algorithm

Traversals of a Binary Tree Inorder traversal algorithm

Traversals of a Binary Tree Postorder traversal algorithm

Binary Tree Operations UML diagram for the class BinaryTree

Interface Template for the ADT Binary Tree An interface template for the ADT binary tree

Interface Template for the ADT Binary Tree An interface template for the ADT binary tree

Interface Template for the ADT Binary Tree An interface template for the ADT binary tree

Interface Template for the ADT Binary Tree An interface template for the ADT binary tree

The ADT Binary Search Tree Recursive definition of a binary search tree n’s value is greater than all values in its left subtree TL. n’s value is less than all values in its right subtree TR. Both TL and TR are binary search trees.

The ADT Binary Search Tree A binary search tree of names

Binary Search Tree Operations Binary search trees with the same data as in previous figure – tree structure depends on order of node insertion.

Binary Search Tree Operations Operations that define the ADT binary search tree

Searching a Binary Search Tree Search algorithm for a binary search tree? Lets try to construct the algorithm….

Searching a Binary Search Tree – for Finn Empty subtree where the search algorithm terminates when looking for Finn

Traversals of a Binary Search Tree Inorder traversal of a binary search tree visits tree nodes in sorted search-key order

Efficiency of Binary Search Tree Operations Max number of comparisons for retrieval, addition, or removal The height of the tree Adding entries in sorted order Produces maximum-height binary search tree Adding entries in random order Produces near-minimum-height binary search tree

Efficiency of Binary Search Tree Operations The Big O for the retrieval, addition, removal, and traversal operations of the ADT binary search tree. Worst case occurs when tree is unbalanced and skewed to left or right.

End Chapter 15