Trees Carlos Delgado Kloos M Carmen Fernndez Panadero Raquel M. - - PowerPoint PPT Presentation

trees
SMART_READER_LITE
LIVE PREVIEW

Trees Carlos Delgado Kloos M Carmen Fernndez Panadero Raquel M. - - PowerPoint PPT Presentation

Trees Carlos Delgado Kloos M Carmen Fernndez Panadero Raquel M. Crespo Garca Dep. Ingeniera Telemtica Univ. Carlos III de Madrid cdk@it.uc3m.es cdk@it.uc3m.es Java: Trees / 1 Contents Concept Implementation Non


slide-1
SLIDE 1

cdk@it.uc3m.es cdk@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
slide-2
SLIDE 2

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 rcrespo@it.uc3m.es

slide-3
SLIDE 3

cdk@it.uc3m.es cdk@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 and

the Art of Motorcycle Maintenance

slide-4
SLIDE 4

mcfp@it.uc3m.es 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 definition

a e i j k b f g T1 T2

Recursive definition

slide-5
SLIDE 5

cdk@it.uc3m.es cdk@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 is
  • ne edge to another node p (p is parent of

c, c is one of the children of p)

  • For each node there is a unique path

(sequence of edges) from the root

slide-6
SLIDE 6

cdk@it.uc3m.es 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 p sibling

  • f c
slide-7
SLIDE 7

cdk@it.uc3m.es 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 p sibling

  • f c
slide-8
SLIDE 8

mcfp@it.uc3m.es Java: Trees / 8

a b c d e f g h i j k

Trees

Recursive definition

slide-9
SLIDE 9

mcfp@it.uc3m.es Java: Trees / 9

a b c d e f g h i j k

Trees

Recursive definition

slide-10
SLIDE 10

mcfp@it.uc3m.es Java: Trees / 10

a b c d f g h i j k e

Trees

Recursive definition

slide-11
SLIDE 11

mcfp@it.uc3m.es Java: Trees / 11

a b c d f g h i j k e

Trees

Recursive definition

slide-12
SLIDE 12

mcfp@it.uc3m.es Java: Trees / 12

a b c d f g h i j k e

Trees

Recursive definition

slide-13
SLIDE 13

cdk@it.uc3m.es 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

slide-14
SLIDE 14

cdk@it.uc3m.es 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

slide-15
SLIDE 15

cdk@it.uc3m.es Java: Trees / 15

Examples

File system Structure of a book or a document Decision tree Arithmetic expressions

slide-16
SLIDE 16

cdk@it.uc3m.es Java: Trees / 16

Example

Expressions ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ( ) ( ( ) ( ) ) ( ) )

slide-17
SLIDE 17

cdk@it.uc3m.es 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

slide-18
SLIDE 18

cdk@it.uc3m.es 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.

slide-19
SLIDE 19

mcfp@it.uc3m.es Java: Trees / 19

a b c d e f g h i j k

Size of the tree: 11 Height of the tree: 3

Node Height Depth Size Internal / External

a

3 11

Internal

b

1 1 3

Internal

c

1 1

External

d

1 1 2

Internal

e

2 1 4

Internal

f

2 1

External

g

2 1

External

h

2 1

External

i

2 1

External

j

1 2 2

Internal

k

3 1

External

Example

Terminology and properties

slide-20
SLIDE 20

Terminology

Ordered tree

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

a b c

rcrespo@it.uc3m.es Java: Trees / 20

a c b

slide-21
SLIDE 21

cdk@it.uc3m.es 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

slide-22
SLIDE 22

cdk@it.uc3m.es Java: Trees / 22

Basic algorithms

Size (number of nodes) Depth of a node Height Traversals

  • Euler
  • Pre-, in- and post-order

To simplify, we assume binary trees

slide-23
SLIDE 23

cdk@it.uc3m.es Java: Trees / 23

Implementations

Based on a linked structure Sequence-based

slide-24
SLIDE 24

cdk@it.uc3m.es Java: Trees / 24

Implementation based

  • n a linked structure

1 3 2 7 6 5 4

slide-25
SLIDE 25

cdk@it.uc3m.es Java: Trees / 25

Sequence-based implementation

1 3 2 7 6 5 4 1 2 3 4 5 6 7

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

slide-26
SLIDE 26

cdk@it.uc3m.es Java: Trees / 26

public class BTree { protected BNode root; BTree() { root = null; } BTree(Object info){ root = new Bnode(info); }

Class Binary tree...

slide-27
SLIDE 27

…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; }

cdk@it.uc3m.es Java: Trees / 27

slide-28
SLIDE 28

cdk@it.uc3m.es 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

slide-29
SLIDE 29

cdk@it.uc3m.es 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; }

slide-30
SLIDE 30

cdk@it.uc3m.es Java: Trees / 30

int size (){...;} int height (){...;} void preorder (){...;} void inorder (){...;} void postorder (){...;} }

...Class Binary node…

slide-31
SLIDE 31

cdk@it.uc3m.es 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; }

slide-32
SLIDE 32

cdk@it.uc3m.es 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); }

slide-33
SLIDE 33

cdk@it.uc3m.es Java: Trees / 33

Euler traversal

slide-34
SLIDE 34

Preorder traversal

First the node Then its children (recursively)

cdk@it.uc3m.es Java: Trees / 34

1 3 2 6 4 7 5

slide-35
SLIDE 35

cdk@it.uc3m.es Java: Trees / 35

Postorder traversal

7 6 1 5 3 4 2 First the children trees (recursively) Then the node

slide-36
SLIDE 36

cdk@it.uc3m.es Java: Trees / 36

Inorder (symmetric) traversal

2 5 1 7 3 6 4 First the left tree (recursively) Then the node Finally, the right tree (recursively)

slide-37
SLIDE 37

cdk@it.uc3m.es Java: Trees / 37

(A+B)*(C–D)

* – + D C B A

slide-38
SLIDE 38

cdk@it.uc3m.es 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–*

slide-39
SLIDE 39

Activity

Visualize expressions as trees http://www.cs.jhu.edu/~goodrich /dsa/05trees/Demo1/

cdk@it.uc3m.es Java: Trees / 39

slide-40
SLIDE 40

cdk@it.uc3m.es Java: Trees / 40

Postfix notation

HP calculators, RPN=Reverse Polish Not. Stack to store objects Eg: 3 5 + 6 2 – * 3 5 8 6 2 4 32

slide-41
SLIDE 41

cdk@it.uc3m.es Java: Trees / 41

Class BNode: preorder

void preorder (){ System.out.println(info); if (left != null) left.preorder(); if (right != null) right.preorder(); }

slide-42
SLIDE 42

cdk@it.uc3m.es Java: Trees / 42

Class BNode: postorder

void postorder (){ if (left != null) left.postorder(); if (right != null) right.postorder(); System.out.println(info); }

slide-43
SLIDE 43

cdk@it.uc3m.es Java: Trees / 43

Class BNode: inorder

void inorder (){ if (left != null) left.inorder(); System.out.println(info); if (right != null) right.inorder(); }

slide-44
SLIDE 44

cdk@it.uc3m.es 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
slide-45
SLIDE 45

cdk@it.uc3m.es 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 are

smaller (or equal) than the key of n

  • and all those of the right subtree larger

(or equal)

slide-46
SLIDE 46

cdk@it.uc3m.es Java: Trees / 46

Example

4 8 2 9 6 3 1 7 5

1 2 3

3

slide-47
SLIDE 47

cdk@it.uc3m.es Java: Trees / 47

Example

4 8 2 9 6 3 1 7 5

4 3 2 1

slide-48
SLIDE 48

cdk@it.uc3m.es Java: Trees / 48

Operations

Search Insertion Extraction

slide-49
SLIDE 49

cdk@it.uc3m.es Java: Trees / 49

Search

Searching “3”:

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

4 8 2 9 6 3 1 7 5 4 2 3

http://www.cosc.canterbury.ac.nz/ mukundan/dsal/BST.html

slide-50
SLIDE 50

cdk@it.uc3m.es Java: Trees / 50

Insertion

Inserting “6”:

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

7 9 2 5 1 3 6 7 2 5

slide-51
SLIDE 51

cdk@it.uc3m.es Java: Trees / 51

Extraction (1)

Extracting “5”:

  • if leaf: extract
  • if degenerate: replace by child

7 9 2 5 1 3 3

slide-52
SLIDE 52

cdk@it.uc3m.es Java: Trees / 52

Extraction (2)

Extracting “2”:

  • if 2 children: replace by
  • largest at left, or
  • smallest at right subtree

7 9 2 5 1 3 3

slide-53
SLIDE 53

Activity

See animation of binary search trees

http://www.ibr.cs. tu-bs.de/courses/ ss98/audii/applets/ BST/BST-Example.html

cdk@it.uc3m.es Java: Trees / 53

slide-54
SLIDE 54

cdk@it.uc3m.es Java: Trees / 54

Heaps

A heap is a binary tree, where for each node n (except for the root), its key is larger or equal than the one of its parent. Utitity

Priority queues. Sorting algorithm

slide-55
SLIDE 55

cdk@it.uc3m.es Java: Trees / 55

Example

1 4 2 9 6 5 3 7 8

slide-56
SLIDE 56

cdk@it.uc3m.es Java: Trees / 56

Complete heap

1 4 2 9 6 5 3 7 8

slide-57
SLIDE 57

cdk@it.uc3m.es Java: Trees / 57

Sequence-based implementation

1 3 2 7 6 5 4 1 2 3 4 5 6 7

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

slide-58
SLIDE 58

cdk@it.uc3m.es Java: Trees / 58

Insert

4 6 5 20 7 9 15 25 16 12 14 8 11

slide-59
SLIDE 59

cdk@it.uc3m.es Java: Trees / 59

Insert

4 6 5 20 7 9 15 25 16 12 14 8 11 2

slide-60
SLIDE 60

cdk@it.uc3m.es Java: Trees / 60

Insert

4 6 5 2 7 9 15 25 16 12 14 8 11 20

slide-61
SLIDE 61

cdk@it.uc3m.es Java: Trees / 61

Insert

4 2 5 6 7 9 15 25 16 12 14 8 11 20

slide-62
SLIDE 62

cdk@it.uc3m.es Java: Trees / 62

Insert

2 4 5 6 7 9 15 25 16 12 14 8 11 20

slide-63
SLIDE 63

cdk@it.uc3m.es Java: Trees / 63

Extract

4 6 5 20 7 9 15 25 16 12 14 8 11

slide-64
SLIDE 64

cdk@it.uc3m.es Java: Trees / 64

Extract

8 6 5 20 7 9 15 25 16 12 14 11

slide-65
SLIDE 65

cdk@it.uc3m.es Java: Trees / 65

Extract

5 6 8 20 7 9 15 25 16 12 14 11

slide-66
SLIDE 66

Activity

Try out the form in

http://www.csse.monash.edu.au/~lloyd /tildeAlgDS/Priority-Q/

cdk@it.uc3m.es Java: Trees / 66

slide-67
SLIDE 67

Activity

Try out the applet in

http://www.cosc.canterbury.ac.nz/ mukundan/dsal/MinHeapAppl.html

cdk@it.uc3m.es Java: Trees / 67