Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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.
Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Search Trees Comp 550.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
2IL50 Data Structures Spring 2015 Lecture 8: Augmenting Data Structures.
Binary Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
Department of Computer Science University of Maryland, College Park
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
Binary Search Trees Chapter 7 Objectives
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 Trees Chapter 6.
Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
Binary Search Tree Qamar Abbas.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
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.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
2IL05 Data Structures Spring 2010 Lecture 9: Augmenting Data Structures.
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
CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Binary Search Trees Chapter 7 Objectives
Binary Search Trees What is a binary search tree?
Binary search tree. Removing a node
Balancing Binary Search Trees
Binary Search Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Design & Analysis of Algorithm n-ary Tree & Binary Tree
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CS200: Algorithms Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Ch. 12: Binary Search Trees Ming-Te Chi
Binary Search Trees (13.1/12.1)
Topic 6: Binary Search Tree Data structure Operations
Design and Analysis of Algorithms
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Presentation transcript:

Binary Search Trees

A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree and smaller than all elements in its right sub-tree

Binary Search Trees Binary search tree is a data structure that efficiently supports all dictionary operations. Each node has at most two Childs that can be identified as the left and right, and a data field. Implementing a binary tree is like implementing a linked list, where each node has two next nodes Sometimes it will be helpful to have a pointer from each node to its parent

BST Vs. Heap

Motivation Binary search on ordered arrays is efficient, However insertion of an item in an ordered array is slow: O(N) Ordered arrays are best suited for static searching, where search space does not change. Binary search trees can be used for efficient dynamic searching.

Searching in a BST Begin in the root. Go left or right depending if the value you are looking for is than the current node. treeSearch (Node x, int k) { if (x == null or k = x.key) return x if (k < x.key) return treeSearch(x.left, k) else return treeSearcg (x.right, k)

Searching a BST Searching time is proportional to the height of the tree. The height is O(logn) if the tree is balanced and could be as worse as O(n) for an unbalanced tree.

Minimum element treeMinimum (Node x) { while (x.left != null) x = x.left return x }

Maximum element treeMaximum (Node x) { while (x.right != null) x = x.right return x }

Predecessor If x has two children then the predecessor is the largest child in x left sub tree. Go left and then right all the way down If it does not have a left child, a node's predecessor is its first left ancestor.

Predecessor I II III

Successor If x has two children then the successor is the smallest child in x right sub tree. Go right and then left all the way down If it does not have a right child, a node's successor is its first right ancestor.

I III II Successor

treeSuccessor(Node x) { if (x.right != null) return treeMinimum(x.right) else y = x.parent while (y!= null && x= y.right) x = y y = y.parent return y

Inserting an element Find the element location using the search operation. Replace the null value with the new node.

Insert

Inserting an element treeInsert(Tree t, Node z) y = null x = t.root while (x != null) { y = x if (z.key < x.key) x = x.left else x = x.right } z.setParent (y) if(y = null){ t.setRoot(z) } else if z.key < y.key y.setLeft(z) else y.setRight(z)

Deleting a key from a BST Deleting is more complicated, because the node that should be deleted may have children, which could be effected. Case 1: if the node is a leaf, just set the references from it’s parent to null

Case 1 – Delete leaf Delete 20

Deleting a key from a BST Case 2: if the deleted node has one child, just cut off the node Case 3: if the node has two Childs then we replace the contents of the node with it’s successor (which has at most one child) and remove the successor

Case 2: node with single child Delete 7

Case 3 – node with two children Delete 6

Questions 1.Draw binary search trees of height 2,3,4,5 and 6 on the set of keys {1,4,5,10,16,17,21}

Questions 2. Suppose have numbers between 1 and 1000 in a binary search tree and we are searching for 363. Which of the following sequences could not be a search sequence? a.2, 252, 401, 398, 330, 344, 397, 363 b. 924, 220, 911, 244, 898, 258, 362, 363 c. 925, 202, 911, 240, 912, 245, 363 d. 2, 399, 387, 219, 266, 382, 381, 278, 363 e. 935, 267, 347, 621, 299, 392, 358, 363

Questions 3. Show the result of inserting 3, 1, 4, 6, 9, 2, 5, 7 into an initially empty binary search tree. Show the result of deleting the root.

Questions 4. Write a method that returns the number of elements in a binary search tree.

Interval trees The intent of interval trees is to maintain data dealing with intervals, where an interval [t1,t2] is defined as an event from t1 to t2 where t1<t2 Interval trees support insert, delete and search operations

Interval Node An interval node has the following properties: T1, T2 the start and end of the interval A max value, storing the max value of the sub tree rooted from this node T1,T2 max

Example 1,

Insertion to an interval tree T1 is used as the key for insertion. So the basic insertion step is the same as in a regular binary tree. The difference is in updating the maximum field

Insertion to an interval tree insert [10,12] [5,10] [15,16] [13,30] [20,22] 10,

Interval search Searching is done by providing an interval and looking for an overlapping interval (which may be more than one) in the tree Two intervals x,y overlap if

Interval Search intervalSearch(Tree t, Interval y) x  root(T) while(x != null && !x.overlap(y)) if left(x) != null && max(left(x)) >= low(y)) x  left(x) else x  right(x) return x;

Interval Search Go right when: –No left child –Max(left(x)) < low(y) Go left when: –There is a left child and max(left(x)) >= low(y)