Trees Chapter 10.

Slides:



Advertisements
Similar presentations
CSC 205 – Java Programming II Lecture 35 April 17, 2002.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Trees. Terminology Trees are hierarchical –“parent-child” relationship A is the parent of B B is a child of A B and C are siblings Generalized to ancestor.
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.
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.
1 Trees & More Tables (Walls & Mirrors - Chapter 10 & Beginning of 11)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
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.
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
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.
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.
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.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Data Structure and Algorithms
CSCE 210 Data Structures and Algorithms
Binary Trees.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
CS 302 Data Structures Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Csc 2720 Instructor: Zhuojun Duan
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Chapter 16 Tree Implementations
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
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.
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
Binary Trees.
Binary Trees.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Chapter 16 Tree Implementations
Binary Trees, Binary Search Trees
Trees.
Binary Trees.
CSC 143 Java Trees.
Chapter 20: Binary Trees.
B.Ramamurthy Chapter 9 CSE116A,B
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Trees.
CSC 205 – Java Programming II
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.
COSC2007 Data Structures II
8.2 Tree Traversals Chapter 8 - Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Trees Chapter 10

Preview: The data organizations presented in previous chapters are linear, in that items are one after another. The ADTs in this chapter organize the data in a nonlinear, hierarchical form. In particular, this chapter discusses the specifications, implementations, and relative efficiency of the ADT binary tree and the ADT binary search tree. CS 308 Chapter 10 -- Trees

Terminology Def: vertex, edge parent, child, sibling root, leaf ancestor, descendant CS 308 Chapter 10 -- Trees

Subtree: any node and it’s descendants CS 308 Chapter 10 -- Trees

The primary focus of this chapter will be on binary trees. Formally a binary tree is a set T of nodes such that either: T is empty T is partitioned into three disjoint subsets: A single node r, the root Two possibly empty sets that are binary trees, called left and right subtrees CS 308 Chapter 10 -- Trees

Here are some examples of how to use binary trees to store data in a hierarchical form. CS 308 Chapter 10 -- Trees

A binary search tree is a binary tree that is in a sense sorted according to the values of its nodes. 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 TL n’s value is less than all values in its right subtree TR Both TL and TR are binary search trees CS 308 Chapter 10 -- Trees

This figure is an example of a binary search tree CS 308 Chapter 10 -- Trees

The height of trees The height of any tree is the number of nodes on the longest path from the root to a leaf For example: consider the following trees: CS 308 Chapter 10 -- Trees

You can also define height recursively: height (T) = 1 + max( height(TL), height(TR) ) CS 308 Chapter 10 -- Trees

Full, complete, and balanced binary trees In a full binary tree of height h, all nodes that are at a level less than h have two children each. Here is a full binary tree of height 3 CS 308 Chapter 10 -- Trees

A complete binary tree of height h is a tree that is full down to level h-1, with level h filled from left to right. CS 308 Chapter 10 -- Trees

The ADT Binary Tree As an abstract data type, the binary tree has operations that add and remove nodes and subtrees. By using these basic operations, you can build any binary tree. Other operations set or retrieve the data in the root of the tree and determine whether the tree is empty. CS 308 Chapter 10 -- Trees

Traversal operations that visit every node in a binary tree are typical. “Visiting” a node means “doing something with or to” the node. We saw traversals for linked lists in Chapter 4. Traversal of a binary tree, however, visits the tree’s nodes in one of several different orders. The three standard orders are called preorder, inorder, and postorder CS 308 Chapter 10 -- Trees

In summary, the ADT binary tree has the following UML diagram CS 308 Chapter 10 -- Trees

Traversals of a Binary Tree A traversal algorithm for a binary tree visits each node in the tree. For purpose of discussion, assume that visiting a node simply means displaying the data portion of the node. CS 308 Chapter 10 -- Trees

Examples of preorder, inorder, and postorder traversals of the same tree. CS 308 Chapter 10 -- Trees

Code: preorder(Node *ptr) { if(ptr!=NULL) cout << ptr->data; preorder(ptr->left); preorder(ptr->right) } CS 308 Chapter 10 -- Trees

Possible Representations of a Binary Tree. You can implement a binary tree by using the constructs of C++ in one of three general ways. Two of these approaches use arrays, but the typical implementation uses pointers. In order to illustrate these three ways we will implement a binary tree of names each way CS 308 Chapter 10 -- Trees

An Array-based representation: const int MAX_NODES = 100; typedef string TreeIremType; class TreeNode{ private: TreeNode(); TreeNode(const TreeItemType & nodeItem, int left, int right); TreeItemType item; int leftChild; int rightChild; friend class BinaryTree; }; CS 308 Chapter 10 -- Trees

We now know the root of the tree, as well a the head of a free list. TreeNode[MAX_NODES] tree; int root; int free; We now know the root of the tree, as well a the head of a free list. CS 308 Chapter 10 -- Trees

An array-based representation of a complete tree. complete binary trees have special attributes: given a node i, you can easily locate both of its children and its parent The left child (if it exists) is tree[2*i+1] its right child (if it exists) is tree[2*i+2] and its parent (if tree[i] is not the root) is tree[(i-1)/2] CS 308 Chapter 10 -- Trees

A Complete binary tree, and its array-based implementation: CS 308 Chapter 10 -- Trees

A pointer-based representation You can use C++ pointers to link the nodes in the tree. Thus you can represent a tree by using the following C++ statements typedef string TreeItemType; class TreeNode{ private: TreeNode(){ }; TreeNode(const TreeItemType& nodeItem, TreeNode *left = NULL, TreeNode *right = NULL): item(nodeItem), leftChildPtr(left), rightChildPtr(right) { } TreeItemType item; TreeNode * leftChildPtr; TreeNode * rightChildPtr; friend class BinaryTree; }; CS 308 Chapter 10 -- Trees

Here is an illustration of this representation: CS 308 Chapter 10 -- Trees

A Pointer-Based Implementation of the ADT Binary Tree This section gives the header and implementation of a binary tree in C++ CS 308 Chapter 10 -- Trees

The ADT Binary Search Tree Searching for a particular item is one operation for which a general binary tree is ill suited. The binary search tree is a binary tree that corrects this deficiency by organizing the data by value. CS 308 Chapter 10 -- Trees

The UML diagram for a Binary Search Tree is given below: CS 308 Chapter 10 -- Trees

Algorithms for the ADT Binary Search Tree Operations if the tree is empty the desired record is not found else if(search_key == root’s item) the desired record is found else if (search_key < root’s item) search(left subtree) else serach (right subtree) CS 308 Chapter 10 -- Trees

Insertion CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

The Efficiency of Binary Search Tree Operations CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees

CS 308 Chapter 10 -- Trees