Tree Traversals – reminder

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

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Binary Search Trees CSE 331 Section 2 James Daly.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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 Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
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.
David Stotts Computer Science Department UNC Chapel Hill.
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.
1 Trees - Part II © Dave Bockus. 2 Building a Binary Search Tree h i b c e d f m k a Input: i h b m e c f a d k.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Binary Search Trees Chapter 7 Objectives
CSE 373 Data Structures Lecture 7
CC 215 Data Structures Trees
Chapter 25 Binary Search Trees
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Recursive Objects (Part 4)
Representation of an AVL Node
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
16IT201/Data structure/Unit II/Binary Search Tree
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Tree Traversals – reminder
Chapter 21: Binary Trees.
Representation of a Threaded Tree
Trees CSE 373 Data Structures.
Abstract Data Structures
Representation of an AVL Node
Chapter 12: Binary Search Trees
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Tree.
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Binary Search Tree.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first traversals: Preorder: visit root, traverse left, traverse right General case: visit root, then traverse subtrees LR Postorder: traverse left, traverse right, visit root General case: traverse subtrees LR, then visit root Inorder: traverse left, visit root, traverse right COSC 2P03 Week 2

Binary Preorder Traversal Visit root, traverse left subtree, traverse right subtree preOrderTraverse(BinaryNode T) { if(T == null) // base case return; else // recursive case - 2 recursive calls T.displayNode(); preOrderTraverse(T.left); preOrderTraverse(T.right); } COSC 2P03 Week 3

Binary Postorder Traversal Traverse left subtree, traverse right subtree, visit root postOrderTraverse(BinaryNode T) { if(T == null) return; else postOrderTraverse(T.left); postOrderTraverse(T.right); T.displayNode(); } COSC 2P03 Week 3

Inorder Traversal Traverse left subtree, visit root, traverse right subtree inOrderTraverse(BinaryNode T) { if(T == null) return; else inOrderTraverse(T.left); T.displayNode(); inOrderTraverse(T.right); } COSC 2P03 Week 3

Height of a Tree Idea: use a postorder traversal, since we first need the heights of the subtrees int height(BinaryNode T) { if(T == null) return –1; else return 1 + max(height(T.left), height(T.right)); } COSC 2P03 Week 3

Preorder Iterative Traversal Use an initially-empty stack, TS, of type BinaryNode preOrderIterativeTraverse(BinaryNode T) { while(T != null) T.displayNode(); /* after left subtree, need to go to right subtree: so save it on stack */ if(T.right != null) TS.push(T.right); /* traverse left subtree if it exists */ if(T.left != null) T = T.left; /* else go to next node in preorder, which is at top of stack */ else if(!TS.isEmpty()) T = TS.pop(); else // traversal is finished T = null; } COSC 2P03 Week 3

Binary Search Trees – Search (Iterative version) BinaryNode find(BinaryNode T, int key) // find the node with the given key { curr = T; while(curr.info != key) if(key < curr.info) curr = curr.left; else curr = curr.right; if(curr == null) // not found return null; } return curr; COSC 2P03 Week 3

Binary Search Trees – findMin (Iterative version) BinaryNode findMin(BinaryNode T) // find smallest element { if(T != null) while(T.left != null) T = T.left; } return T; COSC 2P03 Week 3

Binary Search Trees – findMax (Recursive version) BinaryNode findMax(BinaryNode T) // find largest element { if(T == null) return null; else if(T.right == null) return T; return findMax(T.right); } COSC 2P03 Week 3

Binary Search Trees – Insert (iterative) void insert(BinaryNode T, BinaryNode newNode) // insert newNode into tree with root T { if(T == null) T = newNode; else curr = T; while(true) parent = curr; if(newNode.info < curr.info) // insert in left subtree curr = curr.left; if(curr == null) // insert here parent.left = newNode; return; } else // insert in right subtree curr = curr.right; parent.right = newNode; } } } } } COSC 2P03 Week 3

Binary Search Trees – Delete (see Weiss, section 4.3) BinaryNode delete(int key) // delete and return node with given key { Search for node with given key; If no children just delete Else if no right child replace with left subtree Else if no left child replace with right subtree Else (2 children) replace with inorder successor } COSC 2P03 Week 3