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.

Slides:



Advertisements
Similar presentations
Binary Search Tree Smt Genap
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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.
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.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
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
1 Chapter 25 Trees Iterators Heaps Priority Queues.
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.
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.
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.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Tree Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
 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 :
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Searching and Binary Search Trees CSCI 3333 Data Structures.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
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 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
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.
Lecture 9 Binary Trees Trees General Definition Terminology
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.
Chapter 25 Binary Search Trees
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Recursive Objects (Part 4)
Binary search tree. Removing a node
16IT201/Data structure/Unit II/Binary Search Tree
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
CSE 373 Data Structures Lecture 7
Tree Traversals – reminder
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
Chapter 20: Binary Trees.
Trees CSE 373 Data Structures.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

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 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 32 Binary Preorder Traversal Visit root, traverse left subtree, traverse right subtree preOrderTraverse(BinaryNode T) { if(T == null) return; else { T.displayNode(); preOrderTraverse(T.left); preOrderTraverse(T.right); }

COSC 2P03 Week 33 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 34 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 35 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 36 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 37 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 38 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 39 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 310 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; if(curr == null)// insert here { parent.right = newNode; return; } } } } }

COSC 2P03 Week 311 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 }