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.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
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.
Trees Briana B. Morrison Adapted from Alan Eugenio.
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.
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
© 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.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Fundamentals of Python: From First Programs Through Data Structures
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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.
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 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
TREES Chapter 6 Common final examinations When: Thursday, 12/12, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Lecture Objectives  To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Trees. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. Nonlinear Containers root node leaf edge.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
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.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
Section 8.1 Trees.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
Binary Tree and General Tree
8.1 Tree Terminology and Applications
8.2 Tree Traversals Chapter 8 - Trees.
Chapter 8 – Binary Search Tree
Find in a linked list? first last 7  4  3  8 NULL
Trees Chapter 6.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
Heaps and Priority Queues
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
8.2 Tree Traversals Chapter 8 - Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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 to use recursion to process trees To understand the different ways of traversing a tree To understand the difference between binary trees, binary search trees, and heaps To learn how to implement binary trees, binary search trees, and heaps using linked data structures and arrays

Chapter 8: Trees3 Chapter Objectives (continued) To learn how to use a binary search tree to store information so that it can be retrieved in an efficient manner To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced storage requirements

Chapter 8: Trees4 Tree Terminology A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the top of a tree is called its root The links from a node to its successors are called branches The successors of a node are called its children The predecessor of a node is called its parent

Chapter 8: Trees5 Tree Terminology (continued) Each node in a tree has exactly one parent except for the root node, which has no parent Nodes that have the same parent are siblings A node that has no children is called a leaf node A generalization of the parent-child relationship is the ancestor-descendent relationship

Chapter 8: Trees6 Tree Terminology (continued) A subtree of a node is a tree whose root is a child of that node The level of a node is a measure of its distance from the root

Chapter 8: Trees7 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is a binary tree if either of the following is true T is empty Its root node has two subtrees, TL and TR, such that TL and TR are binary trees

Chapter 8: Trees8 Some Types of Binary Trees Expression tree Each node contains an operator or an operand Huffman tree Represents Huffman codes for characters that might appear in a text file Huffman code uses different numbers of bits to encode letters as opposed to ASCII or Unicode Binary search trees All elements in the left subtree precede those in the right subtree

Chapter 8: Trees9 Some Types of Binary Trees (continued)

Chapter 8: Trees10 Fullness and Completeness Trees grow from the top down Each new value is inserted in a new leaf node A binary tree is full if every node has two children except for the leaves

Chapter 8: Trees11 General Trees Nodes of a general tree can have any number of subtrees A general tree can be represented using a binary tree

Chapter 8: Trees12 Tree Traversals Often we want to determine the nodes of a tree and their relationship Can do this by walking through the tree in a prescribed order and visiting the nodes as they are encountered This process is called tree traversal Three kinds of tree traversal Inorder Preorder Postorder

Chapter 8: Trees13 Tree Traversals (continued) Preorder: Visit root node, traverse TL, traverse TR Inorder: Traverse TL, visit root node, traverse TR Postorder: Traverse TL, Traverse TR, visit root node

Chapter 8: Trees14 Visualizing Tree Traversals You can visualize a tree traversal by imagining a mouse that walks along the edge of the tree If the mouse always keeps the tree to the left, it will trace a route known as the Euler tour Preorder traversal if we record each node as the mouse first encounters it Inorder if each node is recorded as the mouse returns from traversing its left subtree Postorder if we record each node as the mouse last encounters it

Chapter 8: Trees15 Traversals of Binary Search Trees and Expression Trees An inorder traversal of a binary search tree results in the nodes being visited in sequence by increasing data value An inorder traversal of an expression tree inserts parenthesis where they belong (infix form) A postorder traversal of an expression tree results in postfix form

Chapter 8: Trees16 The Node Class Just as for a linked list, a node consists of a data part and links to successor nodes The data part is a reference to type Object A binary tree node must have links to both its left and right subtrees

Chapter 8: Trees17 The BinaryTree Class

Chapter 8: Trees18 The BinaryTree Class (continued)

Chapter 8: Trees19 Overview of a Binary Search Tree Binary search tree definition A set of nodes T is a binary search tree if either of the following is true T is empty Its root has two subtrees such that each is a binary search tree and the value in the root is greater than all values of the left subtree but less than all values in the right subtree

Chapter 8: Trees20 Searching a Binary Tree

Chapter 8: Trees21 Class TreeSet and Interface Search Tree

Chapter 8: Trees22 BinarySearchTree Class

Chapter 8: Trees23 Insertion into a Binary Search Tree

Chapter 8: Trees24 Removing from a Binary Search Tree

Chapter 8: Trees25 Removing from a Binary Search Tree (continued)

Chapter 8: Trees26 Heaps and Priority Queues In a heap, the value in a node is les than all values in its two subtrees A heap is a complete binary tree with the following properties The value in the root is the smallest item in the tree Every subtree is a heap

Chapter 8: Trees27 Removing an Item from a Heap

Chapter 8: Trees28 Implementing a Heap Because a heap is a complete binary tree, it can be implemented efficiently using an array instead of a linked data structure First element for storing a reference to the root data Use next two elements for storing the two children of the root Use elements with subscripts 3, 4, 5, and 6 for storing the four children of these two nodes and so on

Chapter 8: Trees29 Inserting into a Heap Implemented as an ArrayList

Chapter 8: Trees30 Inserting into a Heap Implemented as an ArrayList (continued)

Chapter 8: Trees31 Priority Queues The heap is used to implement a special kind of queue called a priority queue The heap is not very useful as an ADT on its own Will not create a Heap interface or code a class that implements it Will incorporate its algorithms when we implement a priority queue class and Heapsort Sometimes a FIFO queue may not be the best way to implement a waiting line A priority queue is a data structure in which only the highest-priority item is accessible

Chapter 8: Trees32 Insertion into a Priority Queue

Chapter 8: Trees33 The PriorityQueue Interface Effectively the same as the Queue interface provided in chapter six

Chapter 8: Trees34 Design of a HeapPriorityQueue Class

Chapter 8: Trees35 HeapPQwithComparator

Chapter 8: Trees36 Huffman Trees A Huffman tree can be implemented using a binary tree and a PriorityQueue A straight binary encoding of an alphabet assigns a unique binary number to each symbol in the alphabet Unicode for example The message “go eagles” requires 144 bits in Unicode but only 38 using Huffman coding

Chapter 8: Trees37 Huffman Trees (continued)

Chapter 8: Trees38 Chapter Review A tree is a recursive, nonlinear data structure that is used to represent data that is organized as a hierarchy A binary tree is a collection of nodes with three components: a reference to a data object, a reference to a left subtree, and a reference to a right subtree In a binary tree used for arithmetic expressions, the root node should store the operator that is evaluated last A binary search tree is a tree in which the data stored in the left subtree of every node is less than the data stored in the root node, and the data stored in the right subtree is greater than the data stored in the root node

Chapter 8: Trees39 Chapter Review (continued) A heap is a complete binary tree in which the data in each node is less than the data in both its subtrees Insertion and removal in a heap are both O(log n) A Huffman tree is a binary tree used to store a code that facilitates file compression