systems programming - uc3m · 2014-05-07 · trees systems programming julio villena román...

64
Trees Systems Programming Julio Villena Román (LECTURER) <[email protected]> CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos Delgado Kloos, M.Carmen Fernández Panadero and Raquel M.Crespo García

Upload: others

Post on 12-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Trees

Systems Programming

Julio Villena Román (LECTURER)

<[email protected]>

CONTENTS ARE MOSTLY BASED ON THE WORK BY:

Carlos Delgado Kloos, M.Carmen Fernández Panadero

and Raquel M.Crespo García

Page 2: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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

1

Page 3: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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

2

Page 4: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Tress Concept and characteristics

A tree is a non-linear

data structure that

stores the elements

hyerarchically

(Generalization of lists)

3

Page 5: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

• File system

• Structure of a book or a document

• Arithmetic expressions

Examples

4

Page 6: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Tress Concept and characteristics

• Trees can be defined in two ways:

– Non-recursive definition

– Recursive definition

a

e

i j

k

b

f g

T1 T2

Recursive definition 5

a

e

i j

k

b

f g

Non-recursive definition

Page 7: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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 one edge from 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

– Nodes without children are called leaves

6

Page 8: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Example

p

c

Root (no parent)

Hojas

(sin h

Leaves (no

children)

Hojas

(sin hijos) Leaves (no

children)

The parent of c

A child of p sibling

of c

7

Page 9: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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

8

Page 10: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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

9

Page 11: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

a

b c d e

f g h i j

k

Trees Recursive definition

10

Page 12: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

a

b c d e

f g h i j

k

Trees Recursive definition

11

Page 13: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

a

b c d

f g h i j

k

e

Trees Recursive definition

12

Page 14: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

a

b c d

f g h i j

k

e

Trees Recursive definition

13

Page 15: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

a

b c d

f g h i j

k

e

Trees Recursive definition

14

Page 16: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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

15

Page 17: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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.

16

Page 18: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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 0 11 Internal

b

c 0 1 1 External

d 1 1 2 Internal

e

f 0 2 1 External

g 0 2 1 External

h

i

j

k

Example Terminology and properties

17

Page 19: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Terminology Ordered tree

• A tree is ordered, if for each node there exists a

linear ordering for its children.

a

b c

a

c b

18

Page 20: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

• A binary tree is an ordered tree, where

each node has 0, 1 or 2 children (the

left and the right child).

– Full binary tree: each node is either a leaf

or possesses exactly 2 children

– Complete binary tree: all levels except

possibly the last are full, and the last level

has all its nodes to the left side

Terminology Binary tree

19

Page 21: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

BTree interface

public interface BTree<E> {

static final int LEFT = 0;

static final int RIGHT = 1;

boolean isEmpty();

E getInfo() throws BTreeException;

BTree<E> getLeft() throws BTreeException;

BTree<E> getRight() throws BTreeException;

void insert(BTree<E> tree, int side) throws BTreeException;

BTree<E> extract(int side) throws BTreeException;

String toStringPreOrder();

String toStringInOrder();

String toStringPostOrder();

String toString(); // preorder

int size();

int height();

boolean equals(BTree<E> tree);

boolean find(BTree<E> tree);

} 20

Page 22: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Implementations

• Array-based implementation

• Based on a linked structure

21

Page 23: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Array-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

22

Page 24: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Implementation based

on a linked structure

1

3 2

7 6 5 4

23

Linked Binary Node (LBNode)

Linked Binary Tree (LBTree)

• Each tree (LBTree) has a root node (LBNode

attribute)

• Each root node LBNode contains two trees (LBTree

attributes), which can be empty (null)

Page 25: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

LBNode class

(recursive tree)

public class LBNode<E> {

private E info;

private BTree<E> left;

private BTree<E> right;

public LBNode(E info, BTree<E> left, BTree<E> right) {…}

public E getInfo() {…}

public void setInfo(E info) {…}

public BTree<E> getLeft() {…}

public void setLeft(BTree<E> left) {…}

public BTree<E> getRight() {…}

public void setRight(BTree<E> right){…}

}

24

Page 26: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

public class LBTree<E> implements BTree<E> {

private LBNode<E> root;

public LBTree() {

root = null;

}

public LBTree(E info) {

root = new LBNode<E>(info, new LBTree<E>, new

LBTree<E>);

}

public boolean isEmpty() {

return (root==null);

}

LBTree class...

(recursive tree)

25

Page 27: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

public E getInfo() throws BTreeException {

if (isEmpty()) {

throw new BTreeException("empty trees do not have info");

}

return root.getInfo();

}

public BTree<E> getLeft() throws BTreeException {

if (isEmpty()) {

throw new BTreeException("empty trees do not have a left child");

}

return root.getLeft();

}

public BTree<E> getRight() throws BTreeException {

if (isEmpty()) {

throw new BTreeException("empty trees do not have a right child");

}

return root.getRight();

}

… LBTree class...

(recursive tree)

26

Page 28: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

public class LBNode<E> {

private E info;

private LBNode<E> left;

private LBNode<E> right;

public LBNode() {

this(null);

}

public LBNode(E info) {

this(info,null,null);

}

public LBNode(E info, LBNode<E> l, LBNode<E> r) {

this.info = info;

left = l;

right = r;

}

}

Binary node...

(non-recursive)

27

Page 29: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Basic algorithms

• Size (number of nodes)

• Depth of a node

• Height

• Traversals

– Euler

– Pre-, in- and post-order

(To simplify, we assume binary trees)

28

Page 30: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

… LBTree class...

(recursive tree)

public int size() {

if (isEmpty())

return 0;

else

return 1 + root.getLeft().size() + root.getRight().size();

}

public int height() {

if (isEmpty())

return -1;

else {

int leftHeight = root.getLeft().height();

int rightHeight = root.getRight().height();

if (leftHeight > rightHeight)

return 1 + leftHeight;

else

return 1 + rightHeight;

}

}

29

1+Math.max(leftHeight, rightHeight);

Page 31: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Euler traversal

30

Page 32: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Preorder traversal

• First the node

• Then its children

(recursively)

1

3 2

6 4

7 5

31

Page 33: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Postorder traversal

7

6 1

5 3

4 2

• First the children trees

(recursively)

• Then the node

32

Page 34: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Inorder (symmetric)

traversal

2

5 1

7 3

6 4

• First the left tree

(recursively)

• Then the node

• Finally, the right tree

(recursively)

33

Page 35: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

(A+B)*(C–D)

*

– +

D C B A

34

Page 36: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Example

Infix Prefix Postfix

A+B +AB AB+

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

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

35

Page 37: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Activity

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

36

Page 38: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Postfix notation

• HP calculators, RPN=Reverse Polish

Notation

• Stack to store objects

• Eg: 3 5 + 6 2 – *

3

5 8 6 2 4 32

37

Page 39: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

LBTree class: inorder

(recursive tree)

public String toStringInOrder() {

if (isEmpty()) {

return "";

} else {

return root.getLeft().toStringInOrder() +

root.getInfo().toString() + " " +

root.getRight().toStringInOrder(); }

}

38

Page 40: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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

39

Page 41: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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)

40

Page 42: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Example

4

8 2

9 6 3 1

7 5

1

2

3

3

41

Page 43: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Example

4

8

2

9 6

3 1

7

5

4

3

2

1

42

Page 44: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Operations

• Search

• Insertion

• Extraction

43

Page 45: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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

44

Page 46: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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

45

Page 47: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Extraction (1)

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

9 2

5 1

3

3

46

Page 48: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Extraction (2)

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

• largest at left, or • smallest at right subtree

7

9 2

5 1

3

3

47

Page 50: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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)

• Utitity

– Priority queues

– Sorting algorithm

49

Page 51: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Heaps properties

• A heap fulfils two properties:

– Heap property: for each node n (except for

the root), its key is larger or equal than the

one of its parent.

– Completeness

50

Page 52: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Example: heap property

1

4 2

9 6 3

7 8

But not complete

51

5

Page 53: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Example: complete heap

1

4 2

9 6 5 3

7 8

52

Page 54: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

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

53

Page 55: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Insert

4

6 5

20 7 9 15

25 16 12 14 8 11

54

Page 56: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Insert

4

6 5

20 7 9 15

25 16 12 14 8 11 2

55

Page 57: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Insert

4

6 5

2 7 9 15

25 16 12 14 8 11 20

56

Page 58: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Insert

4

2 5

6 7 9 15

25 16 12 14 8 11 20

57

Page 59: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Insert

2

4 5

6 7 9 15

25 16 12 14 8 11 20

58

Page 60: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Extract

4

6 5

20 7 9 15

25 16 12 14 8 11

59

Page 61: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Extract

8

6 5

20 7 9 15

25 16 12 14 11

60

Page 62: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Extract

5

6 8

20 7 9 15

25 16 12 14 11

61

Page 64: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER)  CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos

Activity

• Try out the applet in http://www.cosc.canterbury.ac.nz/mukundan/dsal/MinHea

pAppl.html

63