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.

Slides:



Advertisements
Similar presentations
Binary Search Tree Smt Genap
Advertisements

Treaps.  Good (logarithmic) performance with random data  Linear performance if the data is sorted ◦ Solutions – Splay Trees Amortized O( lg n) performance.
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.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
CS 201 Data Structures and Algorithms Chapter 4: Trees (BST) Text: Read Weiss, §4.3 1Izmir University of Economics.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
Department of Computer Science University of Maryland, College Park
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.
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.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
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.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
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.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
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.
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,
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
Review for Exam 2 Topics covered: –Recursion and recursive functions –General rooted trees –Binary Search Trees (BST) –Splay Tree –RB Tree –K-D Trees For.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
CC 215 Data Structures Trees
AA Trees.
Binary Search Trees A binary search tree is a binary tree
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
CSE 373 Data Structures Lecture 7
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
ITEC 2620M Introduction to Data Structures
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Tree.
Trees CSE 373 Data Structures.
CMSC 341 Binary Search Trees 8/3/2007 CMSC 341 BST.
Trees CSE 373 Data Structures.
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
Binary Trees, Binary Search Trees
Presentation transcript:

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 are larger than items in any node

Items Items must be comparable All items have a unique value Given two distinct items x and y either –value(x) < value(y) –value(x) > value(y) If value(x) = value(y) then x = y It will simplify programming to assume there are no duplicates in our set of items.

Items Need to map Items to a numerical value Integers –Value(x) = x People –Value(x) = ssn –Value(x) = student id

BST Examples

Comparable Interface Want general tree code Requirement of item is that it supports –< –> –= Java uses Interfaces for implementation Similar to abstract method Specify a method that using class is responsible for

Interface Syntax This is an interface

Interface Class that supports an interface

Interface Compiler can verify that class implements an interface Compiler can not verify that implementation is consistent with intent

BST Operations Constructor Insert Find –Findmin –Findmax Remove

BST Operations Generally Recursive BinaryNode operation( Comparable x, BinaryNode t ) { // End of path if( t == null ) return null; if( x.compareTo( t.element ) < 0 ) return operation( x, t.left ); else if( x.compareTo( t.element ) > 0 ) return operation( x, t.right ); else return t; // Match }

BST Find Method private BinaryNode find( Comparable x, BinaryNode t ) { if( t == null ) return null; if( x.compareTo( t.element ) < 0 ) return find( x, t.left ); else if( x.compareTo( t.element ) > 0 ) return find( x, t.right ); else return t; // Match }

BST Remove Operations Remove –Node is leaf Remove node –Node has one child Replace node with child –Node has two children Replace node with smallest child of right subtree.

Removing with value

Remove method private BinaryNode remove( Comparable x, BinaryNode t ) { if( t == null ) return t; // Item not found; do nothing if( x.compareTo( t.element ) < 0 ) t.left = remove( x, t.left ); else if( x.compareTo( t.element ) > 0 ) t.right = remove( x, t.right ); else if( t.left != null && t.right != null ) // Two children { t.element = findMin( t.right ).element; t.right = remove( t.element, t.right ); } else t = ( t.left != null ) ? t.left : t.right; return t; }

Internal Path Length Review depth/height Depth –Depth is number of path segments from root to node –Depth of node is distance from root to that node. –Depth is unique –Depth of root is 0

Internal Path Length Height –Height is maximum distance from node to a leaf. –There can be many paths from a node to a leaf. –The height of the tree is another way of saying height of the root.

Internal Path Length IPL is the sum of the depths of all the nodes in a tree It gives a measure of how well balanced the tree is.

Internal Path Length 11 2 N = 4 IPL = = 4

Internal Path Length 1 2 N = 4 IPL = = 6 3

Average IPL for N nodes N = 4 Calculate IPL of all possible trees

BST Efficiency If tree is balanced O(log(n)) No guarantee that tree will be balanced Analysis in book suggests on IPL = O(nlog(n)) This analysis is based on the assumption that all trees are equally likely Could always get the worst case (a degenerate tree).

Where do BST fit in Simple to understand Works for small datasets Basis for more complicated trees Using inheritance can implement –AVL trees –Splay trees –Red Black trees

Do your Lex