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

Slides:



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

Introduction to Trees Chapter 6 Objectives
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.
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.
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 CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Fundamentals of Python: From First Programs Through Data Structures
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
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.
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 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.
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 tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
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.
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.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
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.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
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.
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.
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.
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
Chapter 25 Binary Search Trees
Section 8.1 Trees.
Binary Trees, Binary Search Trees
8.1 Tree Terminology and Applications
8.2 Tree Traversals Chapter 8 - Trees.
Find in a linked list? first last 7  4  3  8 NULL
Trees Chapter 6.
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
8.1 Tree Terminology and Applications
8.2 Tree Traversals Chapter 8 - Trees.
Introduction to Trees Chapter 6 Objectives
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

TREES Chapter 6

Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor  Accessing all elements in a linear sequence is O(n)  Trees are nonlinear and hierarchical  Tree nodes can have multiple successors (but only one predecessor)

Trees - Introduction (cont.)  Trees can represent hierarchical organizations of information:  class hierarchy  disk directory and subdirectories  family tree  Trees are recursive data structures because they can be defined recursively  Many methods to process trees are written recursively

Trees - Introduction (cont.)  This chapter focuses on the binary tree  In a binary tree each element has two successors  Binary trees can be represented by arrays and by linked data structures  Searching a binary search tree, an ordered tree, is generally more efficient than searching an ordered list—O(log n) versus O(n)

Section 6.1 Tree Terminology and Applications

Tree Terminology dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors

Tree Terminology (cont.) dog cat wolf canine 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

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors The links from a node to its successors are called branches

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors The successors of a node are called its children

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors The predecessor of a node is called its parent

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors Each node in a tree has exactly one parent except for the root node, which has no parent

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors Nodes that have the same parent are siblings

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors A node that has no children is called a leaf node

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors A node that has no children is called a leaf node Leaf nodes also are known as external nodes, and nonleaf nodes are known as internal nodes

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors A generalization of the parent-child relationship is the ancestor-descendant relationship

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors dog is the parent of cat in this tree A generalization of the parent-child relationship is the ancestor-descendant relationship

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors cat is the parent of canine in this tree A generalization of the parent-child relationship is the ancestor-descendant relationship

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors canine is a descendant of cat in this tree A generalization of the parent-child relationship is the ancestor-descendant relationship

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors dog is an ancestor of canine in this tree A generalization of the parent-child relationship is the ancestor-descendant relationship

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors A subtree of a node is a tree whose root is a child of that node

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors A subtree of a node is a tree whose root is a child of that node

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors A subtree of a node is a tree whose root is a child of that node

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors The level of a node is determined by its distance from the root

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors The level of a node is its distance from the root plus 1 Level 1 Level 2 Level 3

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors The level of a node is defined recursively Level 1 Level 2 Level 3

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors The level of a node is defined recursively Level 1 Level 2 Level 3 If node n is the root of tree T, its level is 1 If node n is not the root of tree T, its level is 1 + the level of its parent

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors The height of a tree is the number of nodes in the longest path from the root node to a leaf node

Tree Terminology (cont.) dog cat wolf canine A tree consists of a collection of elements or nodes, with each node linked to its successors The height of a tree is the number of nodes in the longest path from the root node to a leaf node The height of this tree is 3

Binary Trees  In a binary tree, each node has 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, T L and T R, such that T L and T R are binary trees (T L = left subtree; T R = right subtree)

Expression Tree  Each node contains an operator or an operand  Operands are stored in leaf nodes  Parentheses are not stored in the tree because the tree structure dictates the order of operand evaluation  Operators in nodes at higher tree levels are evaluated after operators in nodes at lower tree levels (x + y) * ((a + b) / c)

Huffman Tree  A Huffman tree represents Huffman codes for characters that might appear in a text file  As opposed to ASCII or Unicode, Huffman code uses different numbers of bits to encode letters; more common characters use fewer bits  Many programs that compress files use Huffman codes

Huffman Tree (cont.) To form a code, traverse the tree from the root to the chosen character, appending 0 if you branch left, and 1 if you branch right.

Huffman Tree (cont.) Examples: d : e : 010 Examples: d : e : 010

Binary Search Tree  Binary search trees  All elements in the left subtree precede those in the right subtree  A formal definition: A set of nodes T is a binary search tree if either of the following is true:  T is empty  If T is not empty, its root node has two subtrees, T L and T R, such that T L and T R are binary search trees and the value in the root node of T is greater than all values in T L and is less than all values in T R dog cat wolf canine

Binary Search Tree (cont.)  A binary search tree never has to be sorted because its elements always satisfy the required order relationships  When new elements are inserted (or removed) properly, the binary search tree maintains its order  In contrast, a sorted array must be expanded whenever new elements are added, and compacted whenever elements are removed—expanding and contracting are both O(n)

Binary Search Tree (cont.)  When searching a BST, each probe has the potential to eliminate half the elements in the tree, so searching can be O(log n)  In the worst case, searching is O(n)

Recursive Algorithm for Searching a Binary Tree 1. if the tree is empty 2. return null (target is not found) else if the target matches the root node's data 3. return the data stored at the root node else if the target is less than the root node's data 4. return the result of searching the left subtree of the root else 5. return the result of searching the right subtree of the root

Full, Perfect, and Complete Binary Trees  A full binary tree is a binary tree where all nodes have either 2 children or 0 children (the leaf nodes)

Full, Perfect, and Complete Binary Trees (cont.)  A perfect binary tree is a full binary tree of height n with exactly 2 n – 1 nodes  In this case, n = 3 and 2 n – 1 =

Full, Perfect, and Complete Binary Trees (cont.)  A complete binary tree is a perfect binary tree through level n - 1 with some extra leaf nodes at level n (the tree height), all toward the left

General Trees  We do not discuss general trees in this chapter, but nodes of a general tree can have any number of subtrees

General Trees (cont.)  A general tree can be represented using a binary tree  The left branch of a node is the oldest child, and each right branch is connected to the next younger sibling (if any)

Section 6.2 Tree Traversals

 Often we want to determine the nodes of a tree and their relationship  We 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 common kinds of tree traversal  Inorder  Preorder  Postorder

Tree Traversals (cont.)  Preorder: visit root node, traverse T L, traverse T R  Inorder: traverse T L, visit root node, traverse T R  Postorder: traverse T L, traverse T R, visit root node

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  The Euler tour is the path traced in blue in the figure on the right

Visualizing Tree Traversals (cont.)  If we record a node as the mouse visits each node before traversing its subtrees (shown by the downward pointing arrows) we get a preorder traversal  The sequence in this example is a b d g e h c f i j

Visualizing Tree Traversals (cont.)  If we record a node as the mouse returns from traversing its left subtree (shown by horizontal black arrows in the figure) we get an inorder traversal  The sequence is d g b h e a i f j c

Visualizing Tree Traversals (cont.)  If we record each node as the mouse last encounters it, we get a postorder traversal (shown by the upward pointing arrows)  The sequence is g d h e b i j f c a

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 canine, cat, dog, wolf dog cat wolf canine

Traversals of Binary Search Trees and Expression Trees (cont.)  An inorder traversal of an expression tree results in the sequence x + y * a + b / c  If we insert parentheses where they belong, we get the infix form: (x + y) * ((a + b) / c) * * / / + + c c + + y y x x b b a a

Traversals of Binary Search Trees and Expression Trees (cont.)  A postorder traversal of an expression tree results in the sequence x y + a b + c / *  This is the postfix or reverse polish form of the expression  Operators follow operands * * / / + + c c + + y y x x b b a a

Traversals of Binary Search Trees and Expression Trees (cont.)  A preorder traversal of an expression tree results in the sequence * + x y / + a b c  This is the prefix or forward polish form of the expression  Operators precede operands * * / / + + c c + + y y x x b b a a