InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
CSC 205 – Java Programming II Lecture 35 April 17, 2002.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
CS 171: Introduction to Computer Science II
A balanced life is a prefect life.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Trees part 2.. Full Binary Trees A BC GDE F Full binary tree A binary tree is full if all the internal nodes (nodes other than leaves) has two children.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Fundamentals of Python: From First Programs Through Data Structures
Marc Smith and Jim Ten Eyck
Binary Search Trees Chapter 7 Objectives
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Data Structure & Algorithm 09 – Binary Search Tree JJCAO.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
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 ),
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
2-3 Trees, Trees Red-Black Trees
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
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.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
CS 367 Introduction to Data Structures Lecture 8.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Binary Search Trees Chapter 7 Objectives
AA Trees.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Search Trees < > = Binary Search Trees
Tree data structure.
Binary Trees, Binary Search Trees
Binary Search Trees < > =
Tree data structure.
Binary Search Trees < > = Binary Search Trees
Find in a linked list? first last 7  4  3  8 NULL
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Search Trees Chapter 7 Objectives
Search Sorted Array: Binary Search Linked List: Linear Search
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight()); }

Examples Iterative version of in-order traversal Option 1: using Stack Option 2: with references to parents in TreeNodes Iterative version of height() method

Binary Tree Implementation The binary tree ADT can be implemented using a number of data structures Reference structures (similar to linked lists), as we have seen Arrays – either simulating references or complete binary trees allow for a special very memory efficient array representation (called heaps) We will look at 3 applications of binary trees Binary search trees (references) Red-black trees (references) Heaps (arrays)

Problem: Design a data structure for storing data with keys Consider maintaining data in some manner (data structure) The data is to be frequently searched on the search key e.g. a dictionary, records in database Possible solutions might be: A sorted array (by the keys) Access in O(log n) using binary search Insertion and deletion in linear time An sorted linked list Access, insertion and deletion in linear time

Dictionary Operations The data structure should be able to perform all these operations efficiently Create an empty dictionary Insert Delete Look up (by the key) The insert, delete and look up operations should be performed in O(log n) time Is it possible?

Data with keys For simplicity we will assume that keys are of type long, i.e., they can be compared with operators, <=, ==, etc. All items stored in a container will be derived from KeyedItem. public class KeyedItem { private long key; public KeyedItem(long k) { key=k; } public getKey() { return key; }

Binary Search Trees (BSTs) A binary search tree is a binary tree with a special property For all nodes v in the tree: All the nodes in the left subtree of v contain items less than the item in v and All the nodes in the right subtree of v contain items greater than or equal to the item in v

BST Example

BST InOrder Traversal inOrder(n.leftChild) visit(n) inOrder(n.rightChild) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) Conclusion: in-Order traversal of BST visit elements in order.