34
1 [email protected] [email protected] Java: Trees / 1 Trees Carlos Delgado Kloos Mª Carmen Fernández Panadero Raquel M. Crespo García Dep. Ingeniería Telemática Univ. Carlos III de Madrid Contents Concept Non recursive definition Recursive definition Examples Terminology Key concepts Properties Implementation Sequence-based Linked structure Basic operations Traversals Particular cases Binary search trees Binary heaps Java: Trees / 2 [email protected]

Trees - UC3M2-ar... · 2015-01-19 · 2 [email protected] Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

1

[email protected]@it.uc3m.es Java: Trees / 1

Trees

Carlos Delgado Kloos

Mª Carmen Fernández Panadero

Raquel M. Crespo García

Dep. Ingeniería Telemática

Univ. Carlos III de Madrid

Contents

ConceptNon recursive definition

Recursive definition

Examples

TerminologyKey concepts

Properties

ImplementationSequence-based

Linked structureBasic operations

Traversals

Particular casesBinary search trees

Binary heaps

Java: Trees / [email protected]

Page 2: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

2

[email protected]@it.uc3m.es Java: Trees / 3

Quote

"The structure of concepts is formally called a hierarchy and since ancient times has been a basic structure for all western knowledge. Kingdoms, empires, churches, armies have all been structured into hierarchies. Tables of contents of reference material are so structured, mechanical assemblies, computer software, all scientific and technical knowledge is so structured..."

-- Robert M. Pirsig: Zen andthe Art of Motorcycle Maintenance

[email protected] Java: Trees / 4

Tress

Concept and characteristics

A tree is a non-linear data structure that stores the elements hyerarchically

Trees can be defined in two ways:

Non-recursive definition

Recursive definitiona

e

i j

k

b

f g

T1 T2

Recursive definition

Page 3: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

3

[email protected]@it.uc3m.es Java: Trees / 5

Non-recursive definition

A tree consists of a set of nodes and a set of edges, such that:There is a special node called root

For each node c, except for the root, there isone edge from another node p (p is parentof c, c is one of the children of p)

For each node there is a unique path(sequence of edges) from the root

[email protected] Java: Trees / 6

Example

p

c

Root (no parent)

Hojas

(sin

hijos)

Hojas

(sin

hijos)

Leaves (no

children)

Hojas

(sin

hijos)

Hojas

(sin

hijos)

Leaves (no

children)

The parent of c

A child of psibling

of c

Page 4: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

4

[email protected] Java: Trees / 7

Example

p

c

Root (no parent)

Hojas

(sin

hijos)

Hojas

(sin

hijos)

Leaves (no

children)

Hojas

(sin

hijos)

Hojas

(sin

hijos)

Leaves (no

children)

The parent of c

A child of psibling

of c

[email protected] Java: Trees / 8

a

b c d e

f g h i j

k

Trees

Recursive definition

Page 5: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

5

[email protected] Java: Trees / 9

a

b c d e

f g h i j

k

Trees

Recursive definition

[email protected] Java: Trees / 10

a

b c d

f g h i j

k

e

Trees

Recursive definition

Page 6: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

6

[email protected] Java: Trees / 11

a

b c d

f g h i j

k

e

Trees

Recursive definition

[email protected] Java: Trees / 12

a

b c d

f g h i j

k

e

Trees

Recursive definition

Page 7: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

7

[email protected] Java: Trees / 13

Recursive definition (1)

A tree is

A node

or a node and subtrees connected to the node by means of an edge to its root

Doesn't include

the empty tree

[email protected] Java: Trees / 14

Recursive definition (2)

A tree is

empty

or a node and zero or more non-empty subtrees connected to the node by means of an edge to its root

Page 8: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

8

[email protected] Java: Trees / 15

Examples

File system

Structure of a book or a document

Decision tree

Arithmetic expressions

[email protected] Java: Trees / 16

Example

Expressions

( ) ( )

( )

( )

( )( )

( ( ) ( ( ) ( ) ) ( ) )

Page 9: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

9

[email protected] Java: Trees / 17

Terminology

A node is external, if it doesn't have children (it is a leaf)

A node is internal, if it has one or more children

A node is ancestor of another one, if it is its parent or an ancestor of its parent

A node is descendent of another one, if the latter is ancestor of the former

The descendents of a node determine a subtree where this node acts as the root

[email protected] Java: Trees / 18

Terminology

A path from one node to another one, is a

sequence of consecutive edges between the nodes.

Its length is the number of edges it is composed of.

The depth of a node is the length of the path from

the root to this node.

The height of a tree is the depth of the deepest

node.

The size of a tree is the number of nodes.

Page 10: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

10

[email protected] Java: Trees / 19

a

b c d e

f g h i j

kSize of the tree: 11

Height of the tree: 3

Node Height Depth Size Internal /

External

a 3 0 11 Internal

b 1 1 3 Internal

c 0 1 1 External

d 1 1 2 Internal

e 2 1 4 Internal

f 0 2 1 External

g 0 2 1 External

h 0 2 1 External

i 0 2 1 External

j 1 2 2 Internal

k 0 3 1 External

Example

Terminology and properties

Terminology

Ordered tree

A tree is ordered, if for each node there exists a linear ordering for its children.

a

b c

[email protected] Java: Trees / 20

a

c b

Page 11: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

11

[email protected] Java: Trees / 21

A binary tree is an ordered tree, where each node has 0, 1 or 2 children (the left and the right child).

Terminology

Binary tree

[email protected] Java: Trees / 22

Basic algorithms

Size (number of nodes)

Depth of a node

Height

TraversalsEuler

Pre-, in- and post-order

To simplify, we assume binary trees

Page 12: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

12

[email protected] Java: Trees / 23

Implementations

Based on a linked structure

Sequence-based

[email protected] Java: Trees / 24

Implementation based

on a linked structure

1

32

7654

Page 13: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

13

[email protected] Java: Trees / 25

Sequence-based

implementation

1

32

7654

1 2 3 4 5 6 7

p(rot)=1

p(x.left)=2*p(x)

p(x.right)=2*p(x)+1

[email protected] Java: Trees / 26

public class BTree {

protected BNode root;

BTree() {

root = null;

}

BTree(Object info){

root = new Bnode(info);

}

Class Binary tree...

Page 14: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

14

…Class Binary tree...

public int size() {

int size = 0;

if (root != null)

size=root.size();

return size;

}

public int height(){

int h = -1;

if (root != null)

h=root.height();

return h;

}

[email protected] Java: Trees / 27

[email protected] Java: Trees / 28

public void preorder() {

if (root!=null) root.preorder();

}

public void inorder() {

if (root!=null) root.inorder();

}

public void postorder() {

if (root!=null) root.postorder();

}

}

...Class Binary tree

Page 15: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

15

[email protected] Java: Trees / 29

Class Binary node...

class BNode {

private Object info;

private BNode left;

private BNode right;

BNode() {

this(null);

}

BNode(Object info) {

this(info,null,null);

}

BNode(Object info, BNode l, BNode r) {

this.info=info;

left=l;

right=r;

}

[email protected] Java: Trees / 30

int size (){...;}

int height (){...;}

void preorder (){...;}

void inorder (){...;}

void postorder (){...;}

}

...Class Binary node…

Page 16: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

16

[email protected] Java: Trees / 31

BNode Class: Size

int size (){

int size = 1;

if (left != null)

size += left.size();

if (right != null)

size += right.size();

return size;

}

[email protected] Java: Trees / 32

BNode Class: Height

int height() {

int hl = -1;

int hr = -1;

if (left != null)

hl = left.height();

if (right != null)

hr = right.height();

return 1 + Math.max(hl, hr);

}

Page 17: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

17

[email protected] Java: Trees / 33

Euler traversal

Preorder traversal

First the node

Then its children (recursively)

[email protected] Java: Trees / 34

1

32

64

75

Page 18: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

18

[email protected] Java: Trees / 35

Postorder traversal

7

61

53

42

First the children trees (recursively)

Then the node

[email protected] Java: Trees / 36

Inorder (symmetric)

traversal

2

51

73

64

First the left tree (recursively)

Then the node

Finally, the right tree (recursively)

Page 19: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

19

[email protected] Java: Trees / 37

(A+B)*(C–D)

*

–+

DCBA

[email protected] Java: Trees / 38

Example

Infix Prefix Postfix

A+B +AB AB+

A+B–C –+ABC AB+C–

(A+B)*(C–D) *+AB–CD AB+CD–*

Page 20: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

20

Activity

Visualize expressions as treeshttp://www.cs.jhu.edu/~goodrich

/dsa/05trees/Demo1/

[email protected] Java: Trees / 39

[email protected] Java: Trees / 40

Postfix notation

HP calculators, RPN=Reverse Polish Not.

Stack to store objects

Eg: 3 5 + 6 2 – *

3

5862432

Page 21: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

21

[email protected] Java: Trees / 41

Class BNode: preorder

void preorder (){

System.out.println(info);

if (left != null)

left.preorder();

if (right != null)

right.preorder();

}

[email protected] Java: Trees / 42

Class BNode: postorder

void postorder (){

if (left != null)

left.postorder();

if (right != null)

right.postorder();

System.out.println(info);

}

Page 22: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

22

[email protected] Java: Trees / 43

Class BNode: inorder

void inorder (){

if (left != null)

left.inorder();

System.out.println(info);

if (right != null)

right.inorder();

}

[email protected] Java: Trees / 44

Properties of binary trees

Let

E=Number of external nodes

I=Number of internal nodes

N=Size=E+I

H=Height

then

E=I+1

H+1≤E≤2H H≤I≤2H-1 2*H+1≤N≤2H+1-1

log2(N+1)-1≤H≤(N-1)/2

Page 23: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

23

[email protected] Java: Trees / 45

Binary search trees

A binary search tree is a binary tree where for each node n,

all the keys in the left subtree aresmaller (or equal) than the key of n

and all those of the right subtree larger(or equal)

[email protected] Java: Trees / 46

Example

4

82

9631

75

1

2

3

3

Page 24: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

24

[email protected] Java: Trees / 47

Example

4

8

2

96

31

7

5

4

3

2

1

[email protected] Java: Trees / 48

Operations

Search

Insertion

Extraction

Page 25: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

25

[email protected] Java: Trees / 49

Search

Searching “3”:• 3<4: go to left subtree• 3>2: go to right subtree• 3=3: element found

4

82

9631

75

4

2

3

http://www.cosc.canterbury.ac.nz/

mukundan/dsal/BST.html

[email protected] Java: Trees / 50

Insertion

Inserting “6”:• 6<7: go to left subtree• 6>2: go to right subtree• when hole: insert

7

92

51

3 6

7

2

5

Page 26: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

26

[email protected] Java: Trees / 51

Extraction (1)

Extracting “5”:• if leaf: extract• if degenerate: replace by child 7

92

51

3

3

[email protected] Java: Trees / 52

Extraction (2)

Extracting “2”:• if 2 children: replace by

• largest at left, or• smallest at right subtree

7

92

51

3

3

Page 27: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

27

Activity

See animation of binary search treeshttp://www.ibr.cs.tu-

bs.de/courses/ss98/audii/applets/BST/BST-

Example.html

[email protected] Java: Trees / 53

[email protected] Java: Trees / 54

Heaps

A binary heap is a complete binary tree where every node has a key greater(*) than or equal to the key of its parent.Usually, heaps refer to binary heaps

* It could also be defined as less or equal (the order criteria is arbitrary)

UtitityPriority queues

Sorting algorithm

Page 28: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

28

[email protected] Java: Trees / 55

Heaps properties

A heap fulfils two properties:

Heap property: for each node n (except forthe root), its key is larger or equal than theone of its parent.

Completeness

[email protected] Java: Trees / 56

Example: heap property

1

42

9653

78

But not complete

Page 29: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

29

[email protected] Java: Trees / 57

Example: complete heap

1

42

9653

78

[email protected] Java: Trees / 58

Sequence-based

implementation

1

32

7654

1 2 3 4 5 6 7

p(root)=1

p(x.left)=2*p(x)

p(x.right)=2*p(x)+1

Page 30: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

30

[email protected] Java: Trees / 59

Insert

4

65

207915

2516 1214 811

[email protected] Java: Trees / 60

Insert

4

65

207915

2516 1214 811 2

Page 31: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

31

[email protected] Java: Trees / 61

Insert

4

65

27915

2516 1214 811 20

[email protected] Java: Trees / 62

Insert

4

25

67915

2516 1214 811 20

Page 32: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

32

[email protected] Java: Trees / 63

Insert

2

45

67915

2516 1214 811 20

[email protected] Java: Trees / 64

Extract

4

65

207915

2516 1214 811

Page 33: Trees - UC3M2-ar... · 2015-01-19 · 2 cdk@it.uc3m.es Java: Trees / 3 Quote "The structure of concepts is formally called a hierarchyand since ancient times has been a basic structure

33

[email protected] Java: Trees / 65

Extract

8

65

207915

2516 1214 11

[email protected] Java: Trees / 66

Extract

5

68

207915

2516 1214 11