CS 261 – Data Structures Trees.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
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.
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.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
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,
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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.
Topic 17 Introduction to Trees
Data Structures TREES.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
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.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
1 CMSC 341 Introduction to Trees Textbook sections:
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 Saurav Karmakar
CS 201 Data Structures and Algorithms
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures and Design in Java © Rick Mercer
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.
The Tree Data Structure
Problems with Linked List (as we’ve seen so far…)
Trees.
Trees Tree nomenclature Implementation strategies Traversals
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
CMSC 341 Introduction to Trees.
Data Structures & Algorithm Design
CHAPTER 4 Trees.
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Data Structures and Database Applications Binary Trees in C#
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
Binary Trees.
Trees and Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Binary Trees.
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Week nine-ten: Trees Trees.
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Binary Trees, Binary Search Trees
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Priority Queues & Heaps
Binary Trees, Binary Search Trees
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

CS 261 – Data Structures Trees

Trees Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: Arrays/Vectors Lists 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.) 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 Root (depth = 0, height = 4) B C Subtree rooted at node C D E F Leaf node (depth = 4, height = 0)

Tree Characteristics (cont.) Are these trees? Yes No No

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

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

Complete Tree: Height and Node Count What is the height of a complete binary tree that has n nodes? We will come back to this later when we have algorithms whose time complexity is proportional to the path length

Dynamic Array Implementation Complete binary tree has structure that is efficiently implemented with a DynArr: Children of node i are stored at 2i + 1 and 2i + 2 Parent of node i is at floor((i - 1) / 2) a Root b c a 1 b 2 c 3 d 4 e 5 f 6 7 d e f Why is this a bad idea if tree is not complete?

Dynamic Array Implementation (cont.) If the tree is not complete (it is thin, unbalanced, etc.), the DynArr implementation will be full of holes a b c d e f a 1 b 2 c 3 4 d 5 6 e 7 8 9 10 11 12 13 f 14 15 Big gaps where the level is not filled!

Dynamic Memory Implementation struct Node { TYPE val; struct Node *left; /* Left child. */ struct Node *rght; /* Right child. */ }; Like the Link structure in a linked list: we will use this structure 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 Start at root. 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 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 Application: Animal Game Swim? Fish Yes No Cat Fly? Bird Cat Cat Swim? Fish Yes No

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: Node, left, right  Pre-order Left, node, right  In-order Left, right, node  Post-order Node, right, left Right, node, left Right, left, node Most common traversals.  Subtrees are not usually analyzed  from right to left.

Pre-Order Traversal Process order  Node, Left subtree, Right subtree void preorder(struct Node *node) { if (node != 0){ process (node->val); preorder(node->left); preorder(node->rght); } Example result: p s a m a e l r t e e p s e a m l r a t e e

Post-Order Traversal Process order  Left subtree, Right subtree, Node void postorder(struct Node *node) { if (node != 0){ postorder(node->left); postorder(node->rght); process (node->val); } Example result: a a m s l t e e r e p p s e a m l r a t e e

In-Order Traversal Process order  Left subtree, Node, Right subtree void inorder(struct Node *node) { if (node != 0){ inorder(node->left); process(node->val); inorder(node->rght); } Example result: a sample tree p s e a m l r a t e e

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

Traversal Example 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

Traversals Computational complexity: Problems with traversal code: Each traversal requires constant work at each node (not including recursive calls) Order not important Iterating over all n elements in a tree requires O(n) time Problems with traversal code: If internal: ties (task dependent) process method to tree implementation If external: exposes internal structure (access to nodes)  Not good information hiding Recursive function can’t return single element at a time Solution  Iterator (more on this later)

Level-Order Enumeration Haven’t seen this traversal yet: Traverse nodes a level at a time from left to right Start with root level and then traverse its children and then their children and so on Example result: p s e a m l r a t e e p s e a m l r a t e e

Representing General Trees Binary trees are the most common type found in CS: However, it is sometimes useful to allow more than two children per node General trees (any number of children per node): In-order traversal is not well defined Pre-order, post-order, and level-order will still work Any general tree can be represented using a binary tree: A node’s left data member refers to its first child A node’s rght data member refers to its next sibling

General Tree: Binary Tree Representation left rght rght rght left left left left

General Trees: Pre- and Post-Order Traversal The pre-order traversal of the general tree corresponds to a pre-order traversal of the binary tree The post-order traversal of the general tree corresponds to an in-order traversal of the binary tree

Questions Lesson on tree Next topic: implementing a useful tree data structure