Department of Computer Science University of Maryland, College Park

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
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.
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. 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.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Midterm 2 Overview Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng 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.
Binary Search Trees CMSC 132 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
BST Data Structure A BST node contains: A BST contains
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lecture1 introductions and Tree Data Structures 11/12/20151.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
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.
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.
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.
Heaps & Priority Queues
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
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.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Lecture 9 Binary Trees Trees General Definition Terminology
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
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.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
CSCE 3110 Data Structures & Algorithm Analysis
Recursive Objects (Part 4)
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
BST Trees
Binary search tree. Removing a node
UNIT III TREES.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Data Structures & Algorithm Design
Binary Tree Applications
Binary Trees, Binary Search Trees
CSC 143 Binary Search Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Department of Computer Science University of Maryland, College Park Binary Search Trees Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

Overview Binary trees Binary search trees Find Insert Delete

Hierarchical Data Structures One-to-many relationship between elements Tree Single parent, multiple children Binary tree Tree with 0–2 children per node Tree Binary Tree

Trees Terminology Root  no predecessor Leaf  no successor Interior  non-leaf Height  distance from root to leaf Root node Height Interior nodes Leaf nodes

Degenerate binary tree Types of Binary Trees Degenerate – only one child Balanced – mostly two children Complete – always two children Degenerate binary tree Balanced binary tree Complete binary tree

Binary Trees Properties Degenerate Height = O(n) for n nodes Similar to linear list Balanced Height = O( log(n) ) for n nodes Useful for searches Degenerate binary tree Balanced binary tree

Binary Search Trees Key property Value at node Smaller values in left subtree Larger values in right subtree Example X > Y X < Z X Y Z

Non-binary search tree Binary Search Trees Examples 5 10 10 2 45 5 30 5 45 30 2 25 45 2 25 30 10 25 Binary search trees Non-binary search tree

Binary Tree Implementation Class Node { Value data; Node left, right; // null if empty void insert ( Value data1 ) { … } void delete ( Value data2 ) { … } Node find ( Value data3 ) { … } … }

Polymorphic Binary Tree Implement. Interface Node { Node insert ( Value data1 ) { … } } Class EmptyNode implements Node { Class NonEmptyNode implements Node { Value data; Node left, right; // Either Empty or NonEmpty void insert ( Value data1 ) { … }

Iterative Search of Binary Tree Node Find( Node n, Value key) { while (n != null) { if (n.data == key) // Found it return n; if (n.data > key) // In left subtree n = n.left; else // In right subtree n = n.right; } return null; Find( root, keyValue );

Recursive Search of Binary Tree Node Find( Node n, Value key) { if (n == null) // Not found return( n ); else if (n.data == key) // Found it else if (n.data > key) // In left subtree return Find( n.left, key ); else // In right subtree return Find( n.right, key ); } Find( root, keyValue );

Example Binary Searches Find ( 2 ) 10 5 10 > 2, left 5 > 2, left 2 = 2, found 5 > 2, left 2 = 2, found 5 30 2 45 2 25 45 30 10 25

Example Binary Searches Find ( 25 ) 10 5 10 < 25, right 30 > 25, left 25 = 25, found 5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found 5 30 2 45 2 25 45 30 10 25

Binary Search Properties Time of search Proportional to height of tree Balanced binary tree O( log(n) ) time Degenerate tree O( n ) time Like searching linked list / unsorted array Requires Ability to compare key values

Binary Search Tree Construction How to build & maintain binary trees? Insertion Deletion Maintain key property (invariant) Smaller values in left subtree Larger values in right subtree

Binary Search Tree – Insertion Algorithm Perform search for value X Search will end at node Y (if X not in tree) If X < Y, insert new leaf X as new left subtree for Y If X > Y, insert new leaf X as new right subtree for Y Observations O( log(n) ) operation for balanced tree Insertions may unbalance tree

Example Insertion Insert ( 20 ) 10 10 < 20, right 30 > 20, left Insert 20 on left 5 30 2 25 45 20

Binary Search Tree – Deletion Algorithm Perform search for value X If X is a leaf, delete X Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree Observation O( log(n) ) operation for balanced tree Deletions may unbalance tree

Example Deletion (Leaf) Delete ( 25 ) 10 10 10 < 25, right 30 > 25, left 25 = 25, delete 5 30 5 30 2 25 45 2 45

Example Deletion (Internal Node) Delete ( 10 ) 10 5 5 5 30 5 30 2 30 2 25 45 2 25 45 2 25 45 Replacing 10 with largest value in left subtree Replacing 5 with largest value in left subtree Deleting leaf

Example Deletion (Internal Node) Delete ( 10 ) 10 25 25 5 30 5 30 5 30 2 25 45 2 25 45 2 45 Replacing 10 with smallest value in right subtree Deleting leaf Resulting tree