CS 261 – Winter 2010 Trees. Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: 1.Vectors and Arrays 2.Lists.

Slides:



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

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
CS 171: Introduction to Computer Science II
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Binary Trees Chapter 6.
CS261 Data Structures Tree Traversals. Goals Euler Tours Recursive Implementation Tree Sort Algorithm.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
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.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS261 Data Structures Trees Introduction and Applications.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 CMSC 341 Introduction to Trees Textbook sections:
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Trees Saurav Karmakar
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Recursive Objects (Part 4)
Trees.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
CS223 Advanced Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
Binary Trees.
Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CS 261 – Data Structures Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

CS 261 – Winter 2010 Trees

Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: 1.Vectors and Arrays 2.Lists 3.Trees

Tree Characteristics A tree consists of a collection of nodes connected by directed arcs A tree has a single root node –By convention, the root node is usually drawn at the top A node that points to (one or more) other nodes is the parent of those nodes while the nodes pointed to are the children Every node (except the root) has exactly one parent Nodes with no children are leaf nodes Nodes with children are interior nodes

Tree Characteristics (cont.) Nodes that have the same parent are siblings The descendents of a node consist of its children, and their children, and so on –All nodes in a tree are descendents of the root node (except, of course, the root node itself) Any node can be considered the root of a subtree –Like a subset, a subtree need not be “proper” (i.e., be strictly smaller than the original) A subtree rooted at a node consists of that node and all of its descendents

Tree Characteristics (cont.) There is a single, unique path from the root to any node –Arcs don’t join together A path’s length is equal to the number of arcs traversed A node’s height is equal to the maximum path length from that node to a leaf node: –A leaf node has a height of 0 –The height of a tree is equal to the height of the root A node’s depth is equal to the path length from the root to that node: –The root node has a depth of 0 –A tree’s depth is the maximum depth of all its leaf nodes (which, of course, is equal to the tree’s height)

Tree Characteristics (cont.) Root (depth = 0, height = 4) A BC DE Subtree rooted at node C Nodes D and E are children of node B Node B is the parent of nodes D and E Nodes B, D, and E are descendents of node A (as are all other nodes in the tree…except A) E is an interior node F is a leaf node F Leaf node (depth = 4, height = 0)

Tree Characteristics (cont.) Are these trees? YesNo

Binary Tree Nodes have no more than two children: –Children are generally ordered from left to right Full Binary Tree: every leaf is at the same depth –Every internal node has 2 children –Height of n will have 2 n+1 – 1 nodes –Height of n will have 2 n leaves

Binary Tree Nodes have no more than two children: –Children are generally ordered from left to right Full Binary Tree: every leaf is at the same depth –Every internal node has 2 children –Height of n will have 2 n+1 – 1 nodes –Height of n will have 2 n leaves Complete Binary Tree: full except for the bottom level which is filled from left to right

Relationship of Height to Number of Nodes If a complete binary tree has N nodes, what it its height? We will come back to this when We later have algorithms That run in time Proportional to the Path length

Array Implementation Complete binary tree have structure that is efficiently implemented with an array : –Children of node i are stored at 2i + 1 and 2i + 2 –Parent of node i is at floor((i - 1) / 2) a bc def 0a0a 1b1b 2c2c 3d3d 4e4e 5f5f 67 Root

Array Implementation (cont.) If the tree is not complete (it is thin, unbalanced, etc.), the Array implementation will be full of holes a bc f de 0a0a 1b1b 2c2c 34d4d 56e6e f 1415

Dynamic Memory Implementation struct node { EleType value; struct node * left; // Left child. struct node * right; // Right child. }; Like the Link class in LinkedList : we will use this class in several data structures

Binary Tree Application: Animal Game Purpose: guess an animal using a sequence of questions –Internal nodes contain yes/no questions –Leaf nodes are animals –Initially, tree contains a single animal (e.g., a “cat”) stored in the root node 1.Start at root. 2.If internal node  ask yes/no question Yes  go to left child and repeat step 2 No  go to right child and repeat step 2 3.If leaf node  ask “I know. Is it a …”: If right  done If wrong  “learn” new animal by asking for a yes/no question that distinguishes the new animal from the guess

Binary Tree Traversals Just as a list, it is often necessary to examine every node in a tree A list is a simple linear structure: can be traversed either forward or backward – but usually forward What order do we visit nodes in a tree? Most common traversal orders: –Pre-order –In-order –Post-order

Binary Tree Traversals (cont.) All traversal algorithms have to: –Process node –Process left subtree –Process right subtree Traversal order determined by the order these operations are done. Six possible traversal orders: 1.Node, left, right  Pre-order 2.Left, node, right  In-order 3.Left, right, node  Post-order 4.Node, right, left 5.Right, node, left 6.Right, left, node Subtrees are not  usually analyzed  from right to left.  Most common traversals. 

Process order  Node, Left subtree, Right subtree // Not in the BinaryNode class. void preorder(BinaryNode node) { if (node != null){ process (node.obj); preorder(node.left); preorder(node.rght); } Example result: p s a m a e l r t e e e l s a amr t e e Pre-Order Traversal p

Post-Order Traversal Process order  Left subtree, Right subtree, Node void postorder(BinaryNode node) { if (node != null){ postorder(node.left); postorder(node.rght); process (node.obj); } Example result: a a m s l t e e r e p e l s a mr t e e a p

In-Order Traversal Process order  Left subtree, Node, Right subtree void inorder(BinaryNode node) { if (node != null){ inorder(node.left); process (node.obj); inorder(node.rght); } Example result: a sample tree e l s a mr t p a e e

Binary Tree Traversals: Euler Tour An Euler Tour “walks” around the tree’s perimeter Each node is visited three times: –1 st visit: left side of node –2 nd visit: bottom side of node –3 rd visit: right side of node –Leaf nodes are visited three times in succession Traversal order depends on when node processed: –Pre-order: 1 st visit –In-order: 2 nd visit –Post-order: 3 rd visit e l s a mr t p a e e

Pre-order: + a * + b c d (Polish notation) In-order: a + (b + c) * d (parenthesis added) Post-order: a b c + d * + (reverse Polish notation) * + a d b c Traversal Example +

Traversals Computational complexity: –Each traversal requires constant work at each node (not including the recursive calls) –Order not important –Iterating over all n elements in a tree requires O(n) time Problem with traversal code: –The process function must be rewritten (or replaced) for each new task –Programmer writing new process function sees the internal tree structure representation –Not good information hiding –Solution  Iterator (more on this later)

Questions? Next topic, how do we make a useful data structure out of a tree?