Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Efficiency of Ordered Data Structures: Binary Search and Binary Search Trees, Study notes of Computers and Information technologies

The importance of order in data structures and the efficiency gains of searching in ordered sets. It covers the concept of binary search and binary search trees, providing examples and code snippets in java. The text also touches upon generic methods and the comparable interface.

Typology: Study notes

2010/2011

Uploaded on 09/09/2011

asdlol2
asdlol2 🇬🇧

4.4

(8)

233 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Data Structures G5029
Kingsley Sage
Room 5C16, Pevensey III
khs20@sussex.ac.uk
© University of Sussex 2006
Lecture 5
Lecture 5
Order
Arrays vs binary search
Generic methods
The Comparable interface
Binary search trees
pf3
pf4
pf5
pf8

Partial preview of the text

Download Efficiency of Ordered Data Structures: Binary Search and Binary Search Trees and more Study notes Computers and Information technologies in PDF only on Docsity!

Data Structures G

Kingsley Sage

Room 5C16, Pevensey III

khs20@sussex.ac.uk

© University of Sussex 2006

Lecture 5

Lecture 5

 Order

– Arrays vs binary search

 Generic methods

– The Comparable interface

 Binary search trees

Order

 Many data structures provide sorting and searching

routines.

 The idea of sorting pre-supposes that the data items are

ordered in some way.

 Searching does not pre-suppose an order, but it is quicker

to search for an item if the items have been previously

sorted.

 Many data types have an intrinsic ordering (like integers).

Other orderings can be created e.g. ASCII values for

characters, lexicographical values for strings.

 Order affects searching efficiency …

Efficiency of search

 Let’s say we have an un-ordered set of N

integers. What is the computational complexity

associated with finding the largest one?

– We need to examine each one, so order N , or O(N).

 And if the set of integers is ordered?

– It will be at one end of the set, so for array,

computational complexity is constant time k , but we do

need to factor in sorting the integers in the first place.

– What about for a linked list?

Generic methods

public interface Comparable {

public int compareTo(Object o);

 The wrapper classes for Java primitive types all implement this

interface.

 compareTo returns a negative integer, zero, or a positive integer

according to whether this object is less than, equal to, or greater

than the specified object o.

 See the on-line course notes for information on implementing

compareTo for you own particular class.

Generic binary search

public static final boolean contains(Comparable item, Comparable[] array,int n) { int low = 0; int high = n-1; while (low <= high) { int mid = (low+high) / 2; int compare = item.compareTo(array[mid]); if (compare < 0) { high = mid – 1; } else if (compare > 0) { low = mid + 1; } else { return true; } } return false; }

 We can use this generic approach for binary search …

Binary search trees

 A binary tree is an important linked data structure.

  • A binary tree is either empty, or else consist of some data item together with a left and right descendant binary tree.
  • A binary search tree is a binary tree in which the value stored at each node of the tree is greater than the values stored in the left sub tree, and less than the values stored in the right sub tree.

Binary search trees

 Could organise using lexicographical values

coldplay busted pink madonna U macy gray shakira LHS: value is “smaller” RHS: value is “larger” The concepts of “smaller” and “larger” are defined according to the data you are processing …

Empty trees and node trees

 We can now start to implement a Java class for binary trees …

public abstract class SearchTree {

public static SearchTree empty() {

return new EmptyTree();

public abstract boolean isEmpty();

…more methods

 From this base abstract class, we can build EmptyTree and a

NodeTree …

Empty trees and node trees

class EmptyTree extends SearchTree { protected boolean isEmpty() {} public boolean isEmpty() { return true; } … more methods } class NodeTree extends SearchTree { private Comparable data; private SearchTree left; private SearchTree right; protected NodeTree(Comparable item) { data = item; left = empty(); right = empty(); } public boolean isEmpty() { return false; } … more methods }

Next time …

 Binary heaps and priority queues