Trees Chapter 10. CS 308 2Chapter 10 -- Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.

Slides:



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

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
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
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
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.
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
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
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
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
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++
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
Binary Search Trees (BST)
© 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.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
(c) University of Washington20-1 CSC 143 Java 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.
Trees Chapter 15.
Data Structure and Algorithms
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
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Chapter 21: Binary Trees.
Chapter 16 Tree Implementations
Binary Trees, Binary Search Trees
Trees Chapter 10.
Trees.
CSC 143 Java Trees.
Chapter 20: Binary Trees.
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.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Trees Chapter 10

CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one after another. 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. 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. In particular, this chapter discusses the specifications, implementations, and relative efficiency of the ADT binary tree and the ADT binary search tree.

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

CS 308 4Chapter Trees Subtree: any node and it’s descendants Subtree: any node and it’s descendants Tree: Tree: Subtree Subtree

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

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

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

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

CS 308 9Chapter Trees The height of 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 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: For example: consider the following trees:

CS Chapter Trees You can also define height recursively: You can also define height recursively: height (T) = 1 + max( height(T L ), height(T R ) ) height (T) = 1 + max( height(T L ), height(T R ) )

CS Chapter Trees Full, complete, and balanced binary 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. 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 Here is a full binary tree of height 3

CS Chapter 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. 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 Chapter Trees The ADT Binary Tree As an abstract data type, the binary tree has operations that add and remove nodes and subtrees. 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. 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. Other operations set or retrieve the data in the root of the tree and determine whether the tree is empty.

CS Chapter Trees Traversal operations that visit every node in a binary tree are typical. Traversal operations that visit every node in a binary tree are typical. “Visiting” a node means “doing something with or to” the node. “Visiting” a node means “doing something with or to” the node. We saw traversals for linked lists in Chapter 4. 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. 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 The three standard orders are called preorder, inorder, and postorder

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

CS Chapter Trees Traversals of a Binary Tree Traversals of a Binary Tree A traversal algorithm for a binary tree visits each node in the 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. For purpose of discussion, assume that visiting a node simply means displaying the data portion of the node.

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

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

CS Chapter Trees Possible Representations of a Binary Tree. Possible Representations of a Binary Tree. You can implement a binary tree by using the constructs of C++ in one of three general ways. 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. 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 In order to illustrate these three ways we will implement a binary tree of names each way

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

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

CS Chapter Trees An array-based representation of a complete tree. 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 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] The left child (if it exists) is tree[2*i+1] its right child (if it exists) is tree[2*i+2] 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] and its parent (if tree[i] is not the root) is tree[(i-1)/2]

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

CS Chapter Trees A pointer-based representation 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 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; typedef string TreeItemType; class TreeNode{ class TreeNode{ private: private: TreeNode(){ }; TreeNode(){ }; TreeNode(const TreeItemType& nodeItem, TreeNode(const TreeItemType& nodeItem, TreeNode *left = NULL, TreeNode *left = NULL, TreeNode *right = NULL): TreeNode *right = NULL): item(nodeItem), leftChildPtr(left), item(nodeItem), leftChildPtr(left), rightChildPtr(right) { } rightChildPtr(right) { } TreeItemType item; TreeItemType item; TreeNode * leftChildPtr; TreeNode * leftChildPtr; TreeNode * rightChildPtr; TreeNode * rightChildPtr; friend class BinaryTree; friend class BinaryTree; }; };

CS Chapter Trees Here is an illustration of this representation: Here is an illustration of this representation:

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

CS Chapter Trees The ADT Binary Search Tree Searching for a particular item is one operation for which a general binary tree is ill suited. 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. The binary search tree is a binary tree that corrects this deficiency by organizing the data by value.

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

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

CS Chapter Trees Insertion Insertion

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees The Efficiency of Binary Search Tree Operations The Efficiency of Binary Search Tree Operations

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees

CS Chapter Trees