David Stotts Computer Science Department UNC Chapel Hill.

Slides:



Advertisements
Similar presentations
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Advertisements

Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
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.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
1 Trees 3: The Binary Search Tree Section Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
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:
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
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.
David Stotts Computer Science Department UNC Chapel Hill.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
David Stotts Computer Science Department UNC Chapel Hill.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Trees, Binary Trees, and Binary Search Trees COMP171.
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.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Search Trees: BSTs and B-Trees David Kauchak cs161 Summer 2009.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
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.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Binary Search Trees … From
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
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.
Binary Search Trees Chapter 7 Objectives
Data Structures and Analysis (COMP 410)
BCA-II Data Structure Using C
Chapter 25 Binary Search Trees
Recursive Objects (Part 4)
BST Trees
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Data Structures and Analysis (COMP 410)
Binary Search Tree Chapter 10.
Tree.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Data Structures and Analysis (COMP 410)
Lecture 12 CS203 1.
Binary Search Trees.
Data Structures and Analysis (COMP 410)
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Non-Linear data structures
Binary Trees, Binary Search Trees
Presentation transcript:

David Stotts Computer Science Department UNC Chapel Hill

Tree Data Structures binary tree binary search tree (BST)

Tree with arity 2 Every node has max of two children “lo” “re” “ok” “hi” “ya” “mi” “so” “fa” “ti” “ad” “go” “zz” “no” “ok” arity 2, a binary tree “tu”

Linked structure BinCell { root: string left: BinCell right: BinCell }

Binary tree with extra conditions  val > all vals in  val < all vals in let’s assume no duplicates for now  and are both BST We can use a BST when the values can be ordered Ex: int, real, string, char Won’t work for organizing images, files, functions, etc. unless you can define some lessThan, Eq functions for the data val

Binary, but not BST Binary, and is BST

is BST 7 elements in each height is 6 height is 3

Log base 2 of 1,000,000 is about 20 guesses Random (linear) guessing takes avg of 500,000 guesses

Signature  new:  BST  insert: BST x Elt  BST  remove: BST x Elt  BST  findMin: BST  Elt  findMax: BST  Elt  contains: BST x Elt  Boolean (searching)  get: BST x Elt  BST (return a cell)  val: BST  Elt (get root value)  size: BST  Nat (natural number)  empty: BST  Boolean

contains(3) Start at root… is root val 3 ? no, so is root val > 3 ? Yes, so go left Is left root val = 3 ? No, so is val > 3 ? No, so go right Is right root val = 3 ? No, so is val > 3 ? yes, so go left Is right root val = 3 ? yes, so we got it val: 2 val: 4 val: 3 val: 6

insert(5) Write code like “contains”… at “4” we see no R link… so “5” is not there… and we have the right spot to put it in

Follow left links until hit null findMax: follow right links

 “contains” the node of interest  If leaf, just unlink it  If has 1 child, just make parent point to child remove(4)

 If has 2 children, then ◦ findMin in R subtree ◦ Replace node val being deleted with this min val ◦ Recursive delete the min node (it will not have a L subtree so now it’s a one child case) remove(2) 4 5

 BST get more linear as we do more deletes  BST very useful for largely static data sets… like lexicons (OED)

Depth depends on order of inserts Insert(1) Insert(2) Insert(3) Insert(5) Insert(6) Insert(7) Insert(9) height is 6 “unlucky” arrival order

Depth depends on order of inserts Insert(6) Insert(2) Insert(9) Insert(5) Insert(1) Insert(7) Insert(3) height is 3 tree is more balanced

Linked: Time complexity of operations insert worst: O(n), avg: O(log n) remove worst: O(n), avg: O(log n) findMin worst: O(n), avg: O(log n) findMax worst: O(n), avg: O(log n) contains worst: O(n), avg: O(log n) get worst: O(n), avg: O(log n) empty O(1) size O(1) (keep counter) val O(1) (root access)

 Worst case is the pathological data set… insert order that leads to linear structure  Pathological data sets are nearly in order already  Average behavior happens when data arrival order is randomly (uniformly) distributed throughout data value range

 How?  Big-Oh complexity?

 First build a BST that contains all elements you wish to sort  Then an in-order traversal (L then R) will produce sorted order, smallest to largest  In-order (LR): 1,2,5,6,7,9,10,11,12,14,17 Note: In-order but R then L will give sorted order high to low

 Building a BST of N elements:  N inserts  Each insert is O(log N) O(height of the BST)  O(N) * O(log N)  O( N log N )  In-order traversal… visit each N nodes… O(N)  Build tree and then traverse: O( N log N ) + O( N )  O( N + N log N )  O( N * ( 1+ log N ) )  O ( N log N )

So if a computer does 1 million ops per second 20 secs 1 million secs 1 million secs is 277 hrs is 11.5 days

These two BSTs contain the same elements In-Order traversal gives same order (sorted) for each , 3, 4, 8, 9, 10, 12

 Any two BSTs with the same elements in them will have the same sequence generated by In-Order traversal  Post-Order: root is always last, so different root means different sequence  Pre-Order: root is always first, so different root means different sequence

Beyond this is just templates