Binary Search Trees (BST) Let’s look at some pics …and some code. www.javabeat.net/binary-search-tree-traversal-java/

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
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
BST Data Structure A BST node contains: A BST contains
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
CS 2133: Data Structures Binary Search Trees.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
F453 Computing Searches. Binary Trees Not this kind of tree!
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
Lecture 17 Non-Linear data structures Richard Gesick.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Tree Traversal.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
CS261 Data Structures Binary Search Trees II Bag Implementation.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
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.
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 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
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.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
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.
Reading data into sorted list Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Binary Trees and Binary Search Trees
CS 3013 Binary Search Trees.
BST Trees
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
slides created by Alyssa Harding
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Chapter 20: Binary Trees.
AVL Tree Chapter 6 (cont’).
Non-Linear data structures
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Binary Search Trees (BST) Let’s look at some pics …and some code.

BST A binary tree with data arranged such that data in each node >= data in its left child node AND < the data in its right child node Why use them? – one can search, insert, delete items quickly, vs. linked lists

Pictoral Representation

Building a BST Similar to the way sorted linked list was built!

Adding a node Drawing a picture is always a good idea!

Adding a node public class BinarySearchTree { private Node root; public void insert(int value){ Node node = new Node<>(value); if ( root == null ) { root = node; return; } insertRec(root, node); } private void insertRec(Node latestRoot, Node node){ if ( latestRoot.value > node.value){ if ( latestRoot.left == null ){ latestRoot.left = node; return; } else{ insertRec(latestRoot.left, node); } } else{ if (latestRoot.right == null){ latestRoot.right = node; return; } else{ insertRec(latestRoot.right, node); } } } }

Finding the min… What is the big-O order ?

Finding the max… What is the big-O order ?

Source code… public int findMinimum(){ if ( root == null ){ return 0; } Node currNode = root; while(currNode.left != null){ currNode = currNode.left; } return currNode.value; }

Let’s tiptoe through the BST Three ways to traverse the tree: – Inorder Traversal – Preorder Traversal – Postorder Traversal Each public method: calls a private recursive helper method (similar to what we did for add() in the sorted linked list)

Inorder Traversal

What is the result here?

Code for InOrder Traversal public void printInorder() { printInOrder(root); System.out.println(""); } private void printInOrder(Node currentRoot) { if ( currentRoot == null ){ return; } printInOrder(currentRoot.left); System.out.print(currentRoot.data + ", "); printInOrder(currentRoot.right); }

Preorder Traversal Pictorally:

Preorder Traversal public void printPreorder() { printPreOrder(root); System.out.println(""); } private void printPreOrder(Node currentRoot) { if (currentRoot == null) { return; } System.out.print(currentRoot.data + ", "); printPreOrder(currentRoot.left); printPreOrder(currentRoot.right); }

Postorder Traversal Pictorally:

PostOrder Traversal public void printPostorder() { printPostOrderRec(root); System.out.println(""); } private void printPostOrder (Node currentRoot) { if (currentRoot == null) { return; } printPostOrder (currentRoot.left); printPostOrder (currentRoot.right); System.out.print(currentRoot.data + ", "); }