1 Trees 5 37 128. 2 General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

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.
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Trees, Binary Trees, and Binary Search Trees COMP171.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
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.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
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,
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.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Compiled by: Dr. Mohammad Omar Alhawarat
Starting at Binary Trees
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Search Trees Data Structures Ananda Gunawardena
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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.
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.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
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.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 CMSC 341 Introduction to Trees Textbook sections:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees Saurav Karmakar
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures and Design in Java © Rick Mercer
Binary Search Tree (BST)
Section 8.1 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Trees 7/14/2009.
CS212: Data Structures and Algorithms
slides created by Alyssa Harding
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CMSC 202 Trees.
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

1 Trees

2 General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.  Recursive definition: Either a tree is empty or it consists of a root and zero or more nonempty subtrees T 1, T 2, … T k, each of whose roots are connected by an edge from the root. A B D F G H E C Root T2T2 T1T1 TkTk subtrees

3 Rooted Trees In this class, we consider only rooted trees. A rooted tree has the following properties:  One node is distinguished as the root.  Every node c, except the root, is connected by an edge from exactly one other node p. Node p is c’s parent, and c is one of p’s children. – acyclic property  A unique path traverses from the root to each node.

4 General Terms Path length: the number of edges on the path from a node to another. Depth of a node: the length of the path from the root to the node. Height of a node: the length of the path form the node to the deepest leaf. Siblings: Nodes with the same parent. Size of a Node: the number of descendants the node has (including the node itself). The size of root is the size of a tree. The size of a leaf is 1. A B D F G H E C Node Height Depth Size A30 8 B11 3 C01 1 D21 3 E02 1 F02 1 G12 2 H03 1

5 Tree example: Directory

6 Representation Of a General Tree -- first child/next sibling Example for this tree: A B D F G H E C A null First child Next sibling B E null H C D F G Cannot directly access D from A. ParentPtr Key value sibling1st child

7 Binary tree (BT) A binary tree is either empty, or it consists of a node called the root together with TWO binary trees called the left subtree and the right subtree of the root. A binary tree is a tree in which no node can have more than two children.

8 Representation of Binary Trees leaves root Leaves are nodes that have no children. Parent Node: is the one between the node and the root of the tree. parent right child left child Child Node: is the one between the node and the leaves of the tree. ParentPtr Key value Right C Left C

9 Small binary trees Empty tree Tree of size 1 Tree of size 2 Tree of size 3

10 Binary Tree Applications Expression tree A central data structure in compiler design. The leaves of an expression tree are operands; the other nodes contain operators. Huffman coding tree Implement a simple but relatively effective data compression algorithm. Binary Search Tree (BST) Will discuss in chapter 19. * -d b c a +

11 Traversal Three standard traversal order  preorder - V L R  inorder - L V R  postorder - L R V Preorder: traverse the node itself first, then all nodes in the LEFT subtree, then all nodes in the RIGHT subtree. Inorder: traverse all nodes in the LEFT subtree first, then the node itself, then all nodes in the RIGHT subtree. Postorder: traverse all nodes in the LEFT subtree first, then all nodes in the RIGHT subtree, then the node itself, V RL

12 Recursive Traversal Implementation Void PrintInorder (root) if root != null PrintInorder(root->left); print(root->data); PrintInorder(root->right); endif; Void PrintInorder (root) if root != null PrintInorder(root->left); print(root->data); PrintInorder(root->right); endif; The difference is the order of the three statements in the ‘IF’ preorder : inorder : postorder : preorder : inorder : postorder : Void PrintPreorder (root) if root != null print(root->data); PrintPreorder(root->left); PrintPreorder(root->right); endif; Void PrintPreorder (root) if root != null print(root->data); PrintPreorder(root->left); PrintPreorder(root->right); endif; Void PrintPostorder (root) if root != null PrintPostorder(root->left); PrintPostorder(root->right); print(root->data); endif; Void PrintPostorder (root) if root != null PrintPostorder(root->left); PrintPostorder(root->right); print(root->data); endif;

13 Traversal preorder : inorder : postorder : preorder : inorder : postorder : preorder : 1 …... inorder : … 1... postorder : … … 1 preorder : 1 …... inorder : … 1... postorder : … … 1

14 Designing a Nonrecursive Traversal Consider the algorithm for an inorder traversal If the current node is not null traverse the left subtree process the current node traverse the right subtree End if When traversing the left subtree, the stack of activation records remembers the postponed obligations of processing the current node and traversing the right subtree A nonrecursive version of the algorithm would have to use an explicit stack to remember these obligations

15 A Nonrecursive Preorder Traversal Recursion is a convenient way to postpone tasks that will be completed at a later time For example, in a preorder traversal, the task of traversing the right subtree is postponed while the left subtree is being traversed To eliminate recursion, you must use a stack to remember postponed obligations

16 A non-recursive preorder traversal Stack S push root onto S repeat until S is empty v = top S pop S If v is not NULL visit v push v’s right child onto S push v’s left child onto S preorder : inorder : postorder : preorder : inorder : postorder :

17 A non-recursive inorder traversal Stack S Initialize all nodes to white push root onto S repeat until S is empty v = top S pop S If v is black visit v else if v is not NULL push v’s right child onto S change v to black push (black) v onto S push v’s left child onto S preorder : inorder : postorder : preorder : inorder : postorder :

18 Level-Order Traversal -- Breadth First Search (BFS) Level order: 1,2,3,4,5,6 Queue Q enqueue root onto Q repeat until Q is empty v = front Q dequeue Q If v is not NULL visit v enqueue v’s left child onto Q enqueue v’s right child onto Q