Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.

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 Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 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
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Binary Trees Chapter 6.
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.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
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 Chapter 6 Common final examinations When: Thursday, 12/12, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131.
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 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
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.
Starting at Binary 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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree 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.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
Chapter 25 Binary Search Trees
Section 8.1 Trees.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
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.
Binary Trees, Binary Search Trees
Heaps and Priority Queues
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
8.2 Tree Traversals Chapter 8 - Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Spring 2010CS 2251 Trees Chapter 6

Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information Learn 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 Learn to use a binary search tree to store information for efficient retrieval Learn how to use a Huffman tree to encode characters compactly

Spring 2010CS 2253 Trees Trees are nonlinear –useful for hierarchical organization of data Trees are recursive

Spring 2010CS 2254 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

Spring 2010CS 2255 Tree Terminology 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 subtree of a node is a tree whose root is a child of that node

Spring 2010CS 2256 Tree Terminology The level of a node is a measure of its distance from the root –If a node is the root, it has level 1 –Otherwise, a nodes level is one more than the level of its parent The height of a tree is the number of nodes in the longest path from the root to a leaf node –An empty tree has height 0 –The height of a tree is the same as the largest level that any node has

Spring 2010CS 2257 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

Spring 2010CS 2258 Example 1:Expression tree Each node contains an operator or an operand –operator nodes have children –operand nodes are leaves

Spring 2010CS 2259 Example 2: 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

Spring 2010CS Example 3: Binary Search Trees All elements in the left subtree precede those in the right subtree

Spring 2010CS Fullness and Completeness A binary tree of height n is perfect if it has (2 n -1) nodes A binary tree is full if every node has two children except for the leaves A tree is complete if only the rightmost node(s) in the next to last row don't have two children

Spring 2010CS General Trees Nodes of a general tree can have any number of subtrees A general tree can be represented using a binary tree

Spring 2010CS Tree Traversals Often we want to determine the nodes of a tree and their relationship –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 –Itarators need to visit all nodes of a tree exactly once Three kinds of tree traversal –Inorder –Preorder –Postorder

Spring 2010CS Preorder Tree Traversal Visit root node, traverse TL, traverse TR Algorithm if the tree is empty return else visit the root do preorder traversal of left subtree do preorder traversal of right subtree

Spring 2010CS Inorder Tree Traversals Traverse TL, visit root node, traverse TR Algorithm if the tree is empty return else do preorder traversal of left subtree visit the root do preorder traversal of right subtree

Spring 2010CS Postorder Tree Traversals Traverse TL, Traverse TR, visit root node Algorithm if the tree is empty return else do preorder traversal of left subtree do preorder traversal of right subtree visit the root

Spring 2010CS 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

Spring 2010CS Visualizing Tree Traversals

Spring 2010CS Traversals of Binary Search Trees An inorder traversal of a binary search tree results in the nodes being visited in sequence by increasing data value

Spring 2010CS Traversals of Expression Trees 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 A preorder traversal of an expression results in prefix form

Spring 2010CS Practice Traverse the following trees in all three orders

Spring 2010CS Practice 2 Draw an expression tree whose inorder traversal is x / y + 3 * b / c Draw an expression tree whose postorder traversal is x y z + a b - c * / - Draw an expression tree whose preorder traversal is * + a - x y / c d

Spring 2010CS Implementing trees Trees are generally implemented with linked structure similar to what we used for linked lists Nodes need to have references to at least two child nodes as well as the data A node may also have a parent reference.

Spring 2010CS 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 E A binary tree node must have links to both its left and right subtrees

Spring 2010CS The BinaryTree Class

Spring 2010CS The BinaryTree Class

Spring 2010CS Binary Search Trees 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

Spring 2010CS Binary Search Tree mes/thisisthehousethatjackbuilt.html

Spring 2010CS Searching a Binary Tree Searching for kept or jill

Spring 2010CS Class Search Tree

Spring 2010CS BinarySearchTree Class

Spring 2010CS BinarySearchTreeData

Spring 2010CS Binary Search Tree Insertion if the root is null create new node containing item to be the root else if item is the same as root data item is already in tree, return false else if item is less than root data search left subtree else search right subtree

Spring 2010CS Binary Search Tree Delete if root is null return null else if item is less than root data return result of deleting from left subtree else if item is greater than root data return result of deleting from right subtree else // need to replace the root save data in root to return replace the root (see next slide)

Spring 2010CS Replacing root of a subtree if root has no children set parent reference to local root to null else if root has one child set parent reference to root to child else // find the inorder predecessor if left child has no right child set parent reference to left child else find rightmost node in right child of left subtree and move its data to root

Spring 2010CS Delete Example

Spring 2010CS Practice Show the tree that would be created from the following items in the given order happy, depressed, manic, sad, ecstatic What happens if you add them in a different order? What happens if we add mad to the tree?

Spring 2010CS 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

Spring 2010CS Inserting an Item into a Heap

Spring 2010CS Removing from a Heap

Spring 2010CS 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

Spring 2010CS Computing Positions Parent of a node at position c is parent = (c - 1) / 2 Children of node at position p are leftchild = 2 p + 1 rightchild = 2 p + 2

Spring 2010CS Inserting into a Heap Implemented as an ArrayList

Spring 2010CS Removing from a Heap Implemented as an ArrayList

Spring 2010CS Using Heaps 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 The heap is used to implement a special kind of queue called a priority queue

Spring 2010CS Priority Queues Sometimes a FIFO queue may not be the best way to implement a waiting line –What if some entries have higher priority than others and need to be moved ahead in the line? A priority queue is a data structure in which only the highest-priority item is accessible

Spring 2010CS Insertion into a Priority Queue Imagine a print queue that prints the shortest documents first

Spring 2010CS The PriorityQueue Class Java provides a PriorityQueue class that implements the Queue interface given in Chapter 6. Peek, poll, and remove methods return the smallest item in the queue rather than the oldest item in the queue.

Spring 2010CS Design of a KWPriorityQueue Class

Spring 2010CS 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

Spring 2010CS Huffman Tree Example

Spring 2010CS Huffman Trees

Spring 2010CS 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

Spring 2010CS Chapter Review 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 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