Docsity

Trees - Object-Oriented Programming and Data Structures - Lecture Sl, Lecture notes of Object Oriented Programming

Partial preview of the text

Download Trees - Object-Oriented Programming and Data Structures - Lecture Sl and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Trees

Tree

Overview

2

Tree

:^ recursive

data

structure

(similar

to

list)

Each

cell

may

have

zero

or

more

successors

(children)

Each

cell

has

exactly

one

predecessor

(parent)

except

the

root

,^ which

has

none

All

cells

are

reachable

from

root

Binary

tree

:^ tree

in

which

each

cell

can

have

at

most

two

children:

a

left

child

and

a^

right

child

General tree

Binary tree

Not a tree

8 List-like tree

Class

for

Binary

Tree

Cells

class

TreeCell

private

T

datum;

private

TreeCell

left,

right;

public

TreeCell(T

x)

datum

x;

public

TreeCell(T

x,

TreeCell

lft,

TreeCell

rgt)

datum

x;

left

lft;

right

rgt;

} more

methods:

getDatum,

setDatum,

getLeft,

setLeft,

getRight,

setRight

4

new

TreeCell("hello")

Points

to

left

subtree

Points

to

right subtree

Binary

versus

general

tree

•^

In

a

binary

tree

each

node

has

exactly

two

pointers:

to

the

left

subtree,

and

to

the

right

one^ –

Of

course

one

or

both

could

be

null

•^

In

a

general

tree

a

node

can

have

any

number

of

child

nodes

–^

Very

useful

in

some

situations...

–^

one

of

which

will

be

our

assignments!

Applications

of

Trees

Most

languages

(natural

and

computer)

have

a

recursive,

hierarchical

structure

This

structure

is

implicit

in

ordinary

textual

representation

Recursive

structure

can

be

made

explicit

by

representing

sentences

in

the

language

as

trees:

Abstract

Syntax

Trees

(ASTs)

ASTs

are

easier

to

optimize,

generate

code

from,

etc.

than

textual

representation

A^

parser

converts

textual

representations

to

AST

Example

8

-^

Expression

grammar:

–^

E^ →

integer

–^

E^ →

(E

+^ E)

-^

In

textual

representation

–^

Parentheses

show

hierarchical

structure

-^

In

tree

representation

-^

Hierarchy

is^

explicit

in^

the

structure

of

the

tree

Text

AST Representation

Searching

in

a

Binary

Tree

10

public static boolean treeSearch(Object x,

TreeCell node) {

if (node == null) return false;if (node.datum.equals(x)) return true;return treeSearch(x, node.left) ||

treeSearch(x, node.right);

ļ‚Ÿ^ Analog of linear search in lists:given tree and an object, find out ifobject is stored in tree ļ‚Ÿ^ Easy to write recursively, harder towrite iteratively

Binary

Search

Tree

(BST)

11

If^

the

tree

data

are

ordered

in

any

subtree,^ 

All

left

descendents

of^ node

come

before

node

^

All^ right

descendents

of^

node

come

after

node

This

makes

it

much

faster

to

search

public static boolean treeSearch (Object x, TreeCell node) {

if (node == null) return false;if (node.datum.equals(x)) return true;if (node.datum.compareTo(x) > 0)

return treeSearch(x, node.left); else return treeSearch(x, node.right); }

What

Can

Go

Wrong?

13

A^

BST

makes

searches

very

fast,

unless

Nodes

are

inserted

in

alphabetical

order

In

this

case,

we’re

basically

building

a^

linked

list

(with

some

extra

wasted

space

for

the

left

fields

that

aren’t

being

used)

BST

works

great

if

data

arrives

in

random

order

jan feb

mar

apr

may

jun jul

Printing

Contents

of

BST

14

Because

of

the

ordering

rules

for

a

BST,

it’s

easy

to

print

the

items

in

alphabetical

order

-^ Recursively

print

everything

in

the

left

subtree – Print

the

node

-^ Recursively

print

everything

in

the

right

subtree

/* Show the contents of the BST in* alphabetical order./public void show () {*

show(root);System.out.println(); } private static void show(TreeNode node) {

if (node == null) return;show(node.lchild);System.out.print(node.datum + " ");show(node.rchild); }

Some

Useful

Methods

16

//determine

if

a

node

is

a

leaf

public

static boolean

isLeaf(TreeCell

node)

{

return

(node

!=

null)

&&

(node.left

==

null)

&&

(node.right

==

null);

} //compute

height

of

tree

using

postorder

traversal

public

static int

height(TreeCell

node)

{

if

(node

==

null)

return

-1;

//empty

tree

if

(isLeaf(node))

return

0;

return

1

+

Math.max(height(node.left),

height(node.right));

} //compute

number

of

nodes

using

postorder

traversal

public

static int

nNodes(TreeCell

node)

{

if

(node

==

null)

return

0;

return

1

+

nNodes(node.left)

+

nNodes(node.right);

}

Useful

Facts

about

Binary

Trees

d 2

maximum

number

of

nodes

at

depth

d

If

height

of

tree

is

h

Minimum

number

of

nodes

in tree

=^ h

+^

Maximum

number

of

nodes

in tree

=^2

0 +^

(^1 ) +^ …

+^2

h^ =

h+1 2

-^1

Complete

binary

tree^ 

All

levels

of

tree

down

to

a^ certain

depth

are

completely

filled

17

depth 0 1 2

Height 2,maximum number of nodesHeight 2,minimum number of nodes

Things

to

Think

About

•^

What

if

we

want

to

delete

data

from

a

BST?

•^

A

BST

works

great

as

long

as

it’s

balanced

–^

How

can

we

keep

it

balanced?

19

jan feb

mar

apr

may

jun jul

Suffix

Trees

20

•^ Given a string s, a suffix tree for s is a tree such that•^ each edge has a unique label, which is a nonnull substring of s•^ any two edges out of the same node have labels beginning withdifferent characters•^ the labels along any path from the root to a leaf concatenatetogether to give a suffix of s•^ all suffixes are represented by some path•^ the leaf of the path is labeled with the index of the first characterof the suffix in s•^ Suffix trees can be constructed in linear time