• No results found

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

N/A
N/A
Protected

Academic year: 2021

Share "Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees"

Copied!
9
0
0

Loading.... (view fulltext now)

Full text

(1)

Binary Search Trees

• Last lecture:

– Tree terminology

– Kinds of binary trees

– Size and depth of trees

• This time:

– binary search tree ADT

– Java implementation

Keys and records

• So far most examples assumed that we sort,

insert, delete and search for integers.

• In fact in most applications we manipulate

more complicated items.

• In general, assume that we manipulate

records which have keys which can be

compared, searched for etc.

Example

• Record: employee’s name, address,

telephone, position, NI number, payroll

number.

• Key: payroll number.

• Instead of sorting an array of integers in

ascending order, could sort an array of

records in the ascending order of keys.

• Binary search for a record given a key, etc.

Data structures for storing data

• How easy it is to insert a new record

• How easy it is to remove a record

• How easy it is to find a record given a key

Motivation

• Binary search on ordered arrays is efficient:

O(log

2

N)

• 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.

Binary Search Trees

• Binary trees

• Store data sorted in

order of keys: all

values stored in the

left subtree of a node

with key value K have

key values < K, those

in the right subtree

have values >= K.

25 10 30 35 28 2

(2)

BST Search

• Searching for a record with key K in a tree: • if tree is empty return null • else check if K = key of

the root (if yes return the found record) • if the K < key of the root,

search the left subtree • otherwise search the right

subtree 25 10 30 35 28 2

Example

• Searching for a record

with key 27

25 10 30 35 28 2

Example

• Searching for a record

with key 27

25 10 30 35 28 2

Example

• Searching for a record

with key 27

• Item not found.

25

10 30

35 28 2

Binary Search Tree ADT

• Logical domain: ordered binary trees which

hold items of the form (key,record).

• Selected methods:

– BST() : create an empty binary search tree

– void insert(item) : insert item in BST, in key

order

– void remove(key) : remove item with the given

key

– item find(key): find and return item with the

given key

BST Insertion

Inserting an item with key K (not checking for

duplicates) in a tree:

• If tree is empty, make this item the root

• Else compare K to the key of the root, R

• If K < R:

– insert in the left subtree

• If K >= R:

(3)

Example

• Inserting a record with

key 15:

25

10 30

35 2

Example

• Inserting a record with

key 15:

25

10 30

35 2

Example

• Inserting a record with

key 15:

25

10 30

35 2

Example

• Inserting a record with

key 15:

25

10 30

35

2 15

BST Deletion

• First, find the item

• If it is a leaf, just delete it

• If it is not a leaf:

– if it has just one daughter, replace it by

that daughter

– if it has two daughters, replace it by the

leftmost node of its right subtree

Example 1

• Easy case: delete a

leaf (2)

25

10 30

35 28 2

(4)

Example 1

• Easy case: delete a

leaf (2)

• Find it

25 10 30 35 28 2

Example 1

• Easy case: delete a

leaf (2)

• Find it

25 10 30 35 28 2

Example 1

• Easy case: delete a

leaf (2)

• Find it

25 10 30 35 28 2

Example 1

• Easy case: delete a

leaf (2)

• Find it

• Remove it

25 10 30 35 28

Example 2

• More difficult case:

delete 10

25 10 30 35 28 2

Example 2

• More difficult case:

delete 10

• Find it

25 10 30 35 28 2

(5)

Example 2

• More difficult case:

delete 10

• Find it

• Replace it by its

daughter (2)

25 30 35 28 2

Example 3

• Last case: remove 25

25 30 35 28 2

Example 3

• Last case: remove 25

• Find it

25 30 35 28 2

Example 3

• Last case: remove 25

• Find it

• Find its replacement

(leftmost node in the

right subtree: next

larger key value)

25 30 35 28 2

Example 3

• Last case: remove 25

• Find it

• Find its replacement

(leftmost node in the

right subtree: next

larger key value)

• Replace

28 30 35 2

Exercise

• Remove 40

20 40 50 10 30 60 35

(6)

Exercise

• Remove 40

• Find it

20 40 50 10 30 60 35

Exercise

• Remove 40

• Find it

• Find its replacement

(leftmost node in the

right subtree: next

larger key value)

20 40 50 10 30 60 35

Exercise

• Remove 40

• Find it

• Find its replacement

(leftmost node in the

right subtree: next

larger key value)

• Replace

20 50 60 10 30 35

Java Implementation

• Similar to linked lists:

• Define tree items (should have

key()

method)

• Define a tree node class (should have value

and links to left and right daughters)

• Define a tree class which uses it (keeps the

root value and has

insert(),

remove()

and

find()

methods)

• Implementation taken from Shaffer’s book.

BinNode interface

interface BinNode {

public Object element();

public Object setElement(Object v); public BinNode left();

public BinNode setLeft(BinNode p); public BinNode right();

public BinNode setRight(BinNode p); public boolean isLeaf();

}

BinNodePtr class

class BinNodePtr implements BinNode { private Object element;

private BinNode left; private BinNode right;

public BinNodePtr(){};

public BinNodePtr(Object val){ left=right=null;

element = val; }

(7)

BinNodePtr class

public Object element(){ return element; }

public Object setElement(Object v){ element = v; return v; }

BinNodePtr class

BinNode left(){ return left; }

public BinNode setLeft(BinNode p){ left = p;

return p; }

BinNodePtr class

public boolean isLeaf() {

return((left==null)&&(right==null)); }

}

Items in the search tree

Assume that items implement the following interface

(have integer keys):

interface Elem { public int key(); }

BST class

class BST {

private BinNode root; public BST(){}

public Elem find(int key){ return findhelp(root, key); }

BST class

private Elem findhelp(BinNode r, int k){ if (r == null) return null;

Elem it = (Elem)r.element(); if (it.key() > k) {

return findhelp(r.left(), k); } else {

if (it.key()==k) return it; else return findhelp(r.right(),k); }

(8)

BST class

public void insert(Elem val){ root = inserthelp(root, val); }

private BinNode inserthelp(BinNode r,

. Elem v){

if (r==null) return new BinNodePtr(v); Elem it = (Elem)r.element(); if (it.key() > v.key()){ r.setLeft(inserthelp(r.left(),v)); }

BST class

else { r.setRight(inserthelp(r.right(),v); } return r; }

BST class

public void remove(int key){ root = removehelp(root, key); }

BST class

private BinNode removehelp(BinNode r, int k){ if (r==null) return null;

Elem it = (Elem)r.element(); if (k < it.key()){ setLeft(removehelp(r.left(),k)); } else if (k > it.key()){ setRight(removehelp(r.right(),k)); }

BST class

else { // Found the node to be deleted if (r.left()==null) r = r.right(); else if (r.right()==null) r=r.left(); else { // the node has two children Elem temp = getmin(r.right()); r.setElement(temp); r.setRight(deletemin(r.right())); } } return r;

BST class

private Elem getmin(BinNode r){ if (r.left() == null){ return(Elem)r.element(); } else { return getmin(r.left()); } }

(9)

BST class

private BinNode deletemin(BinNode r){ if (r.left() == null){ return r.right(); } else { r.setLeft(deletemin(r.left()); return r; } }

Reading

• Shaffer, Chapter 5 (5.1, 5.2, 5.3, 5.5)

References

Related documents

insecurity, hunger and famines (UNECA, 2017). In most countries, pastoral populations are a minority and are located in the periphery zones and areas that are difficult to

ROW ROW ROW ROW ROW ROW PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDC PRDCPRDC PRDC PRDC PRDCPRDC PRDC

The more so do I hesitate to believe and accept the above letter because, although my sainted mother had been in America for almost fifty years, she could

See Nielsen and Hunter’s (this issue) discussion of “sequential and informational complementarity”. The issue of performance management and evaluation as competing management tools

 Revenues from product sales. Upfront payments are received from licensee pharmaceutical companies when the agreement is signed, while milestone payments are generated in

the SNR required for the LTE 3GPP baseband transceiver based wavelet signals when using (MMSE) is about 21.25 dB, while when using (LS) the SNR about 22.5 dB, from Figure 4

Accepts Out of District In District Only Website: https://essdack.org/LC/thomas-county-learning-center.html Curriculum Used: Odysseyware Registration Deadline: None Partner

To append data, a client first determines the next available position in the shared log – using a sequencer node as an optimization for avoiding contention with other appending