IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
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, Binary Trees, and Binary Search Trees COMP171.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Trees CS /02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
Trees.
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.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
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.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
Trees trees binary trees traversals of trees template method pattern
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 16: Trees Announcements 1.Programming project.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
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.
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 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.
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.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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.
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.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
CSCE 3110 Data Structures & Algorithm Analysis
Trees Saurav Karmakar
The Tree ADT.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
COSC2100: Data Structures and Algorithms
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Week nine-ten: Trees Trees.
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Presentation transcript:

IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Trees

2 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Outline Tree Data Structure Examples Terminology & Definition Binary Tree Tree Traversal Tree Iterators

3 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Trees: Some Examples A tree represents a hierarchy, for example: Organizational structure of a company

4 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Trees: Some Examples Table of contents of a book

5 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Trees: Some Examples File system (Unix, Windows, Internet)

6 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Trees: Terminology A is the root node B is the parent of D and E C is the sibling of B D and E are the children of B D, E, F, G, I are external nodes, or leaves A, B, C, H are internal nodes The depth, level, or path length of E is 2 The height of the tree is 3 The degree of node B is 2 Property: |edges| = |nodes| - 1

7 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Trees: Viewed Recursively A sub-tree is also a tree!!

8 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Binary Trees Binary tree: tree with all internal nodes of degree 2 Recursive View: a binary tree is either empty an internal node (the root) and two binary trees (left subtree and right subtree)

9 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Binary Trees: An Example Arithmetic expression

10 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Binary Trees: Properties If we restrict that each parent can have two and only two children, then: |external nodes| = |internal nodes| + 1 |nodes at level i|  2 i |external nodes|  2 (height) height  log 2 |external nodes| height  log 2 |nodes| - 1 height  |internal nodes| = (|nodes| - 1)/2

11 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Traversing Trees: Preorder Preorder Traversal Algorithm preOrder(v) “visit” node v for each child w of v do recursively perform preOrder(w) Example: reading a document from beginning to end

12 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Traversing Trees: Postorder Postorder Traversal Algorithm postOrder(v) for each child w of v do recursively perform postOrder(w) “visit” node v Example: du (disk usage) command in Unix

13 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Traversing Trees Algorithm evaluateExpression(v) if v is an external node return the variable stored at v else let o be the operator stored at v x  evaluateExpression(leftChild(v)) y  evaluateExpression(rightChild(v)) return x o y

14 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Traversing Trees: Inorder Inorder traversal of a binary tree Algorithm inOrder(v) recursively perform inOrder(leftChild(v)) “visit” node v recursively perform inOrder(rightChild(v)) Example: printing an arithmetic expression print “(“ before traversing the left subtree print “)” after traversing the right subtree

15 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 The BinaryNode in Java A tree is a collection of nodes: class BinaryNode { T element; /* Item stored in node */ BinaryNode left; BinaryNode right; } The tree stores a reference to the root node, which is the starting point. public class BinaryTree { private BinaryNode root; // Root node public BinaryTree() // Default constructor { root = null; }

16 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Think Recursively Computing the height of a tree is complex without recursion. The height of a tree is one more than the maximum of the heights of the subtrees. H T = max (H L +1, H R +1) HLHL HRHR H L +1 H R +1

17 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Routine to Compute Height Handle base case (empty tree) Use previous observation for recursive case. public static int height (BinaryNode t) { if (t == null) return 0; else return 1 + max(height (t.left), height (t.right)); }

18 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Running Time This strategy is a postorder traversal: information for a tree node is computed after the information for its children is computed. The running time of tree traversal is N times the cost of processing each node. Thus, the running time is linear because we do constant work for each node in the tree.

19 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Print Preorder class BinaryNode { void printPreOrder( ) { System.out.println( element ); // Node if( left != null ) left.printPreOrder( ); // Left if( right != null ) right.printPreOrder( ); // Right } class BinaryTree { public void printPreOrder( ) { if( root != null ) root.printPreOrder( ); }

20 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Print Postorder class BinaryNode { void printPostOrder( ) { if( left != null ) left.printPostOrder( ); // Left if( right != null ) right.printPostOrder( ); // Right System.out.println( element ); // Node } class BinaryTree { public void printPostOrder( ) { if( root != null ) root.printPostOrder( ); }

21 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Print Inorder class BinaryNode { void printInOrder( ) { if( left != null ) left.printInOrder( ); // Left System.out.println( element ); // Node if( right != null ) right.printInOrder( ); // Right } class BinaryTree { public void printInOrder( ) { if( root != null ) root.printInOrder( ); }

22 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Traversing Tree Pre-Order Post-OrderInOrder

23 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Exercise A tree contains Integer objects. Find the maximum value Find the total value

24 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Tree Iterator Class Can we implement traversal non-recursively? Recursion is implemented by using a stack. We can traverse non-recursively by maintaining the stack ourselves (emulate stack of activation records). Is a non-recursive approach faster than recursive approach? Possibly:we can place only the essentials, while the compiler places an entire activation record.

25 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Postorder Traversal using Stack Use stack to store the current state (nodes we have traversed but not yet completed) Similar to PC (program counter) in the activation record We “pop” each node three times, when: 0. about to make a recursive call to left subtree 1. about to make a recursive call to right subtree 2. about to process the current node itself

26 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Postorder Algorithm init: push root onto stack with state 0 advance: while (true) node X = pop from the stack switch (state X): case 0: push node X with state 1 push left child of X (if it exists) with state 0 case 1: push node X with state 2 push right child of X (if it exists) with state 0 case 2: visit/process node X

27 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Postorder Traversal: Stack States

28 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Exercise Create a non-recursive algorithm for inorder traversal using stack. Create a non-recursive algorithm for preorder traversal using stack.

29 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Inorder Traversal using Stack What are the states for inorder traversal? 0. about to make a recursive call to left subtree 1. about to process the current node 2. about to make a recursive call to right subtree

30 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Inorder Algorithm init: push root onto the stack with state 0 advance: while (true) node X = pop from the stack switch (state X): case 0: push node X with state 1 push left child of X (if it exists) with state 0 case 1: push node X with state 2 visit/process node X case 2: push right child of X (if it exists) with state 0

31 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Inorder Algorithm (improved) init: push root onto the stack with state 0 advance (optimize): while (true) node X = pop from the stack switch (state X): case 0: push node X with state 1 push left child of X (if it exists) with state 0 case 1: visit/process node X push right child of X (if it exists) with state 0

32 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Preorder Traversal using Stack What are the states for preorder traversal? 0. about to process the current node 1. about to make a recursive call to left subtree 2. about to make a recursive call to right subtree

33 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Preorder Algorithm init: push root onto the stack with state 0 advance: while (true) node X = pop from the stack switch (state X): case 0: push node X with state 1 visit/process the node X case 1: push node X with state 2 push left child of X (if it exists) with state 0 case 2: push right child of X (if it exists) with state 0

34 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Preorder Algorithm (improved) init: push root onto the stack advance (optimized): while (true) node X = pop from the stack visit/process node X push right child of X (if it exists) with state 0 push left child of X (if it exists) with state 0

35 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Euler Tour Traversal Generic traversal of a binary tree The preorder, inorder, and postorder traversals are special cases of the Euler tour traversal “walk around” the tree and visit each node three times: on the left from below on the right

36 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Level-Order Traversal Visit root followed by its children from left to right and followed by their children. So we go down the tree level by level. Sequence: A - B - C - D - E - F - G - H - I

37 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Level-Order Traversal: Idea Using a queue instead of a stack Algorithm (similar to pre-order) init: enqueue the root into the queue advance: node X = dequeue from the queue “visit”/”set current to” the node X; enqueue left child node X (if it exists); enqueue right child node X (if it exists);

38 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Binary Tree: Properties Maximum number of nodes in a binary tree of height k is 2 k A full binary tree with height k is a binary tree which has 2 k nodes. A complete binary tree with height k is a binary tree which has maximum number of nodes possible in levels 0 through k -1, and in (k -1)’th level all nodes with children are selected from left to right. Complete binary tree with n nodes can be shown by using an array, then for any node with index i, we have: Parent (i) is at  i/2  if i  1; for i =1, we have no parent. Left-child (i ) is at 2i if 2i  n. (else no left-child) Right-child (i ) is at 2i+1 if 2i +1  n (else no right-child)

39 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 8 Summary Tree, Binary Tree In order to process the elements of a tree, we consider accessing the elements in certain order Tree traversal is a tree operation that involves "visiting” (or" processing") all the nodes in a tree. Depth First Search (DFS): Pre-order: Visit node first, pre-order all its subtrees from leftmost to rightmost. Inorder: Inorder the node in left subtree and then visit the root following by inorder traversal of all its right subtrees. Post-order: Post-order the node in left subtree and then post-order the right subtrees followed by visit to the node. Breadth First Search (BFS): Level-order: Visit root followed by its children from left to right and followed by their children. So we go down the tree level by level.