Binary Search Trees Definition (recursive):

Slides:



Advertisements
Similar presentations
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Advertisements

Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
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.
Binary Search Trees CSE 331 Section 2 James Daly.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
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.
Building Java Programs Binary Search Trees; TreeSet.
24/3/00SEM107 - © Kamin & ReddyClass 16 - Searching - 1 Class 16 - Searching r Linear search r Binary search r Binary search trees.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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 :
Data Structures and Algorithms TREE. Searching Sequential Searches Time is proportional to n We call this time complexity O(n) Pronounce this “big oh”
David Stotts Computer Science Department UNC Chapel Hill.
CSE 143 Lecture 22 Binary Search Trees continued; Tree Sets read slides adapted from Marty Stepp and Hélène Martin
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
TreeBag a BST implementation. Example class - TreeBag  remember Bag?  collection of items, order does not matter  repeated items allowed public void.
Binary Search Trees CS340. Overview of a Binary Search Tree A set of nodes T is a binary search tree if either of the following is true T is empty If.
CMSC 341 Binary Search Trees. 8/3/2007 UMBC CMSC 341 BST 2 Binary Search Tree A Binary Search Tree is a Binary Tree in which, at every node v, the values.
2/11/ IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
AVL Tree 1. is a binary search tree that For each node: –The height of its left and right subtree can only differ at most 1. –If a tree is an empty tree,
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
Binary Search Trees (BST) Let’s look at some pics …and some code.
Lecture 9 Binary Trees Trees General Definition Terminology
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSE 373 Binary search trees; tree height and balance
Recursive Definition of Tree Structures
Recursive Objects (Part 4)
BST Trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Binary Search Trees.
Trees.
Binary Search Trees.
Binary Search Trees.
Depict the Tree Structure in a picture
Topic 19 Binary Search Trees
Rick Mercer, Allison Obourn, Marty Stepp
Building Java Programs
Binary Search Tree AVL Tree
Node Removal From BST Source:
CSE 373: Data Structures and Algorithms
יסודות מבני נתונים תרגול 6: עץ (Tree), עץ בינארי (Binary Tree),
Binary Search Trees (BSTs)
CSE 373: Data Structures and Algorithms
Binary Search Trees (BSTs)
Ch. 12 Tables and Priority Queues
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
Lecture 21: Binary Search Trees; TreeSet
CSE 373 Data Structures and Algorithms
Building Java Programs
Podcast Ch18d Title: Binary Search Tree Iterator
Basic Data Structures - Trees
Lecture 21: Binary Search Trees; TreeSet
Hashing based on slides by Marty Stepp
Tree.
Trees.
Trees Trees.
Binary Search Trees (BSTs)
Presentation transcript:

Binary Search Trees Definition (recursive): A binary tree is said to be a binary search tree if it is the empty tree or, if there is a left-child, then the data in the left-child is less than the data in the root, if there is a right-child, then the data in the right-child is no less than the data in the root, and every sub-tree is a binary search tree. Therefore, for any node in the a binary search tree, every data in the left-sub-tree is less than every data in the right-sub-tree. 9/21/2018 IT 179

A Binary Search Tree < 19 10 25 1 17 23 30 5 14 18 22 4 7 11 21 3 13 20 9/21/2018 IT 179

Binary Search Trees data < data Compare to We can search the tree “efficiently” < data data 9/21/2018 IT 179

Insert a data into a Binary Search Tree: compare data data data data T <  data < data data data data data 9/21/2018 IT 179

Construct a Binary Search Tree 1 4 11 22 13 3 18 14 30 17 13 20 5 21 10 7 25 23 19 9/21/2018 IT 179

Using an inner class for the internal Tree nodes public class BinarySearchTree <E extends Comparable<E>> { /********** This is an inner class for tree nodes************/ private static class TNode<E> { private E data; private TNode<E> left,right; private TNode(E data, TNode<E> left, TNode<E> right) { //Construct a node with two children this.data = data; this.left = left; this.right = right; } /********** This is the end of the inner class Node<E> *******/ private TNode<E> root; private boolean found=false; public BinarySearchTree() { root = null; ..... 9/21/2018 IT 179

Add a new data to the BST Overload the add method root is private; //add data to this BST, return false if data is already in the tree public boolean add(E data) { if (root != null) return add(root, data); root = new TNode<E>(data, null, null); return true; } Overload the add method root is private; 9/21/2018 IT 179

data t x 19 40 10 // add data to BST t private boolean add(TNode<E> t, E data){ if (data.compareTo(t.data) == 0) return false; if (data.compareTo(t.data) < 0) { // data is smaller if (t.left == null) t.left = new TNode<E>(data, null, null); else return add(t.left, data); } else { // data is bigger if (t.right == null) t.right = new TNode<E>(data, null, null); return add(t.right, data); } return true; ..... data x t 19 40 10 9/21/2018 IT 179

t x 19 40 10 public boolean search(E data) { if (root != null) return search(root, data); return false; } private boolean search(TNode<E> t, E data){ if (data.compareTo(t.data) == 0) return true; if (data.compareTo(t.data) < 0) { // data is smaller if (t.left == null) return false; return search(t.left, data); if (t.right == null) return false; return search(t.right, data); ..... x t 19 40 10 9/21/2018 IT 179

toString (inorder) 2 1 3 2 1 5 4 7 3 public String toString() { return toString(root); } private String toString(TNode<E> t) { if (t == null) return ""; return toString(t.left)+ " "+t.data.toString()+" "+ toString(t.right); ..... 2 2 1 1 5 4 3 7 3 9/21/2018 IT 179

Max and Min min max 2 1 5 4 7 3 public E max() { if (root == null) throw new NoSuchElementException(); return max(root); } public E min() { return min(root); private E max(TNode<E> t) { if (t.right != null) return max(t.right); return t.data; private E min(TNode<E> t) { if (t.left != null) return min(t.left); ..... min max 2 1 5 4 7 3 9/21/2018 IT 179

Size 1 2 2+4+1=7 4 2 1 5 4 7 3 public int size() { return size(root); } private int size(TNode<E> t) { if (t == null) return 0; return size(t.left)+size(t.right)+1; ..... 1 2 2 1 5 2+4+1=7 4 4 7 3 9/21/2018 IT 179

Height 2 4 3+1=4 3 2 1 5 4 7 3 public int height() { return height(root); } private int height(TNode<E> t) { if (t == null) return 0; int L = height(t.left); int R = height(t.right); return (L < R ? R : L)+1; }..... ..... 2 1 5 2 4 3+1=4 3 4 7 3 9/21/2018 IT 179

Remove a data from a Binary Search Tree: = T < < Remove data from the left-sub-tree Remove data from the right-sub-tree data data 9/21/2018 IT 179

L R Remove data from a BST delete Find the min in R, 2. If there is no R, Find the man in L and do the same L 3. If there is no L and R, simply delete the node R delete Rmin 9/21/2018 IT 179

Remove a data to the BST remove 19 root copy remove 10 8 19 10 22 //remove data from this BST, return false if data is not in this BST public boolean remove(E data) { found = false; root = remove(root, data); return found; } remove 19 root 8 19 copy 10 22 9/21/2018 remove 10 IT 179

Remove a new data from the BST //remove data from this BST, return false if data is not in this BST private TNode<E> remove(TNode<E> t, E data) { if (t==null) return null; if (data.compareTo(t.data)==0){ // data is found at T if (t.left == null && t.right == null) return null; E x; if (t.left != null) { x = max(t.left); t.left = remove(t.left, x); } else { x = min(t.right); t.right = remove(t.right, x); t.data=x; return t; if (data.compareTo(t.data)< 0) t.left = remove(t.left, data); else t.right = remove(t.right, data); 9/21/2018 IT 179

What is the benefit of using BST? Search 17, 29, 3 19 10 28 4 14 23 35 1 7 12 17 26 32 21 37 3 5 11 13 15 18 20 22 25 27 30 34 36 38 log230 = 5 9/21/2018 IT 179

What is the real benefit of using BST? O(log2n) Search 18, 2, 26 20 10 30 1 17 25 35 5 14 19 28 34 38 23 4 7 12 15 18 22 27 32 36 3 11 13 21 26 37 9/21/2018 IT 179