info 3.3. chapter 3.3 recursive data structures part 2 : binary trees

84
Info 3.3. Chapter 3.3 Recursive Data Structures Part 2 : Binary Trees

Upload: jace-turkington

Post on 14-Dec-2015

243 views

Category:

Documents


0 download

TRANSCRIPT

Info 3.3.

Chapter 3.3

Recursive Data Structures

Part 2 :

Binary Trees

2Info 3.3.

Common Recursive

Data StructuresLinear Lists

Binary Trees

3Info 3.3.

A Recursive Data Structure

Example : A Pedigree

Father Mother

Name

Mygrandfather Mygrandmother Mygrandfather Mygrandmother

Myfather Mymother

Myself

4Info 3.3.

Recursive Data Structures

Implementation by variant pointersFather Mother

Name TYPE Link = POINTER TO Person; Person = RECORD Name : String; Father, Mother : Link END

Father Mother

Name

Father Mother

Name

Father Mother

Name

Father Mother

Name

Father Mother

Name

5Info 3.3.

Binary TreesDefinitions

Rightsubtree

Leftsubtree

Leaves

ROOT

Ordered binary tree : when keys of - all elements of left subtree smaller than Root - all elements of right subtree larger dan Root.

6Info 3.3.

Ordered Binary TreesDefinitions

Ordered binary tree : when all elements of left subtree smaller than Root all elements of right subtree larger than Root.

left

binary

largerall

when

than

Search time < tLog2n

7Info 3.3.

Ordered Binary TreesData Declarations for Dictionary

TYPE Link = POINTER TO Node;

Node = RECORD Key : String; (* other data fields *) Left, Right : Link END;

8Info 3.3.

Ordered Binary Trees Dictionary Building

PROCEDURE Build(VAR p:Link;x:String);BEGIN IF p = NIL THEN NEW(p); p^.Key:= x; p^.Left:= NIL; p^.Right := NIL ELSE CASE Compare(x,p^.Key) OF less : Build(p^.Left,x) | equal : | greater: Build(p^.Right,x) END (* CASE *) END; (* IF *)END Build;

9Info 3.3.

Ordered Binary Trees Node Deletion

3

2 6

1 4

5

7

3 different situations :a) not present : 8b) 0 or 1 successor : 1, 2, 4, 5, 7c) 2 successors : 3, 6

10Info 3.3.

Ordered Binary Trees Deletion of node 2

3

2 6

1 4

5

7

3

6

1 4

5

7

11Info 3.3.

Ordered Binary Trees Deletion of node 3

3

2 6

1 4

5

7

2 6

1 4

5

7

Root := Rightmost of left subtree

12Info 3.3.

Ordered Binary Trees Deletion of node 3

3

2 6

1 4

5

7

Root := Leftmost of right subtree

2 6

1 4

5

7

13Info 3.3.

Ordered Binary Trees Node Deletion Procedure (1)

PROCEDURE Delete(VAR p:Link;x:String); VAR q : Link; PROCEDURE RightMost(VAR r:Link); ... END RightMost;BEGIN IF p # NIL THEN CASE Compare(x,p^.Key) OF less : Delete(p^.Left,x) | equal : Remove p^ | greater: Delete(p^.Right,x) END (* CASE *) END; (* IF *) END Delete;

14Info 3.3.

Ordered Binary Trees Node Deletion Procedure (2)

(* Remove p^ *)q := p;IF q^.Right = NIL THEN p:= q^.LeftELSIF q^.Left = NIL THEN p:= q^.RightELSE RightMost(p^.Left)END; (* IF *)DISPOSE(q)

15Info 3.3.

Ordered Binary Trees Node Deletion Procedure (3)

(* Remove p^ *)q := p;IF q^.Right = NIL THEN p:= q^.LeftELSIF q^.Left = NIL THEN p:= q^.RightELSE RightMost(p^.Left)END; (* IF *)DISPOSE(q)

pq

can be NIL

16Info 3.3.

Ordered Binary Trees Node Deletion Procedure (4)

(* Remove p^ *)q := p;IF q^.Right = NIL THEN p:= q^.LeftELSIF q^.Left = NIL THEN p:= q^.RightELSE RightMost(p^.Left)END; (* IF *)DISPOSE(q)

pq

17Info 3.3.

Ordered Binary Trees Node Deletion Procedure (5)

(* Remove p^ *)q := p;IF q^.Right = NIL THEN p:= q^.LeftELSIF q^.Left = NIL THEN p:= q^.RightELSE RightMost(p^.Left)END; (* IF *)DISPOSE(q)

p

q

18Info 3.3.

Ordered Binary Trees Node Deletion Procedure (6)

PROCEDURE RightMost (VAR r:Link);BEGIN IF r^.Right # NIL THEN RightMost(r^.Right) ELSE p^.Key := r^.Key; q := r; r := r^.Left END (* IF *)END RightMost;

p

q

r =

19Info 3.3.

Ordered Binary Trees Node Deletion Procedure (7)

PROCEDURE RightMost (VAR r:Link);BEGIN IF r^.Right # NIL THEN RightMost(r^.Right) ELSE p^.Key := r^.Key; q := r; r := r^.Left END (* IF *)END RightMost;

p

q

r =

20Info 3.3.

Ordered Binary Trees Node Deletion Procedure (8)

PROCEDURE RightMost (VAR r:Link);BEGIN IF r^.Right # NIL THEN RightMost(r^.Right) ELSE p^.Key := r^.Key; q := r; r := r^.Left END (* IF *)END RightMost;

X

p

q

r

X

21Info 3.3.

Ordered Binary Trees Node Deletion Procedure (9)

(* Remove p^ *)q := p;IF q^.Right = NIL THEN p:= q^.LeftELSIF q^.Left = NIL THEN p:= q^.RightELSE RightMost(p^.Left)END; (* IF *)DISPOSE(q)

X

p

q

r

22Info 3.3.

Balanced Trees

Perfectly balanced tree :

| NNLS - NNRS | <= 1NNLS = Nbr Nodes in Left Subtree

NNRS = Nbr Nodes in Right Subtree

AVL tree (Adelson-Velskii & Landis) :

| HLS-HRS | <= 1HLS = Height of Left Subtree

HRS = Height of Right Subtree

23Info 3.3.

Perfectly Balanced TreeAll nodes should be perfectly balanced !

1/0 0/1

2/2

1/1 1/0

3/2

0/0 1/0

1/2

1/1 1/0

3/2

5/6 4/6

12/11

24Info 3.3.

Perfectly Balanced TreeAll nodes should be perfectly balanced !

1/0 0/1

2/2

1/1 1/0

3/2

1/0 1/0

2/2

1/1 1/0

3/2

5/6 5/6

12/12

25Info 3.3.

AVL Balanced TreeAVL condition needs to be satisfied everywhere !

0/0 0/0

1/1

0/0

1/0

1/0 1/0

2/2

1/1

2/0

2/2 3/3

3/4

26Info 3.3.

AVL Balanced TreeAVL condition needs to be satisfied everywhere !

0/0 0/0

1/1

0/0

1/0

1/0 1/0

2/2

1/1 0/0

2/1

2/2 3/3

3/4

27Info 3.3.

AVL Tree Balancing

A

B

2 different situations :

Case 1 :imbalance between outer subtrees

Case 2 :imbalance betweeninner and outer subtrees

28Info 3.3.

AVL Tree Balancing

Case 1 : outer imbalance

A

B A

B

29Info 3.3.

AVL Tree Balancing

Case 2 : inner imbalance (1)

A

B

A

B

C

A

B

C

Inner imbalance can appear in two different situations

30Info 3.3.

AVL Tree Balancing

Case 2 : inner imbalance (2)

A

B

C

AB

C

31Info 3.3.

AVL TreesData Declarations for Dictionary

TYPE Link = POINTER TO Node; Node = RECORD Key : String; (* other data fields *) Left, Right : Link; Bal : [-1,1] (* -1:left side higher *) (* +1:right side higher *) END;

32Info 3.3.

AVL Trees Dictionary Building (1)

PROCEDURE BuildAVL (VAR p:Link;x:String;VAR h:BOOLEAN);BEGIN IF p = NIL THEN Insert node; h:= TRUE ELSE CASE Compare(x,p^.Key) OF less : BuildAVL(p^.Left,x,h); IF h THEN adj.bal.left END| equal : h := FALSE | greater: BuildAVL(p^.Right,x,h); IF h THEN adj.bal.right END END (* CASE *) END; (* IF *)END Build;

33Info 3.3.

AVL Trees Dictionary Building (2)

(* Insert Node *)NEW(p);WITH p^ DO Key := x; Left := NIL; Right := NIL; Bal := 0END;

34Info 3.3.

Adjust Balance Left

-1p^.Bal =

rearange

0 +1

p^.Bal :=

h :=

0

FALSE

-1

TRUE

0

FALSE

35Info 3.3.

Rearange

-1

-1

-1

+1

p^Bal =

p^.Left^.Bal =

to be updated

updated

rearange case 1 rearange case 2

36Info 3.3.

AVL Trees Dictionary Building (3)

(* Adjust Balance Left *)CASE p^.Bal OF +1: p^.Bal := 0; h := FALSE | 0: p^.Bal := -1 (* h := TRUE *) | -1: p1 := p^.Left; (* Rearange *) IF p1^.Bal = -1 THEN rearange, case 1 ELSE rearange, case 2 END; (* IF *) p^.Bal := 0; h := FALSEEND; (* CASE *)

37Info 3.3.

AVL Trees Rearange, case 1 (1)

A

B

PP1

38Info 3.3.

AVL Trees Rearange, case 1 (2)

p^.Left := p1^.Right

A

B

PP1

39Info 3.3.

AVL Trees Rearange, case 1 (3)

p^.Left := p1^.Right;p1^.Right := p;

A

B

PP1

40Info 3.3.

AVL Trees Rearange, case 1 (4)

p^.Left := p1^.Right;p1^.Right := p;p^.Bal := 0;

A

B

PP1

41Info 3.3.

AVL Trees Rearange, case 1 (5)

p^.Left := p1^.Right;p1^.Right := p;p^.Bal := 0;p := p1;A

B

PP1

42Info 3.3.

AVL Trees Rearange, case 1 (6)

p^.Left := p1^.Right;p1^.Right := p;p^.Bal := 0;p := p1;A

B

P

43Info 3.3.

AVL Trees Rearange, case 2 (1)

p2 := p1^.Right;

A

B

C

PP1P2

44Info 3.3.

AVL Trees Rearange, case 2 (2)

p2 := p1^.Right;p1^.Right := p2^.Left

A

B

C

PP1P2

45Info 3.3.

AVL Trees Rearange, case 2 (3)

p2 := p1^.Right;p1^.Right := p2^.Left;p2^.Left := p1;A

B

C

PP1P2

46Info 3.3.

AVL Trees Rearange, case 2 (4)

p2 := p1^.Right;p1^.Right := p2^.Left;p2^.Left := p1;p^.Left := p2^.Right;

A

B

C

PP1P2

47Info 3.3.

AVL Trees Rearange, case 2 (4)

p2 := p1^.Right;p1^.Right := p2^.Left;p2^.Left := p1;p^.Left := p2^.Right;p2^.Right := p;

A

B

C

PP1P2

48Info 3.3.

AVL Trees Rearange, case 2 (5)

p2 := p1^.Right;p1^.Right := p2^.Left;p2^.Left := p1;p^.Left := p2^.Right;p2^.Right := p;CASE p2^.Bal OF -1: P^.Bal := +1; p1^.Bal := 0; | +1: p^.Bal := 0; p1^.Bal := -1END; (* CASE *)

A

B

C

PP1P2

49Info 3.3.

AVL Trees Rearange, case 2 (6)

p2 := p1^.Right;p1^.Right := p2^.Left;p2^.Left := p1;p^.Left := p2^.Right;p2^.Right := p;CASE p2^.Bal OF -1: P^.Bal := +1; p1^.Bal := 0; | +1: p^.Bal := 0; p1^.Bal := -1END; (* CASE *)p := p2;

A

B

C

PP1P2

50Info 3.3.

AVL Trees Rearange, case 2 (7)

p2 := p1^.Right;p1^.Right := p2^.Left;p2^.Left := p1;p^.Left := p2^.Right;p2^.Right := p;CASE p2^.Bal OF -1: P^.Bal := +1; p1^.Bal := 0; | +1: p^.Bal := 0; p1^.Bal := -1END; (* CASE *)p := p2;

AB

C

P

51Info 3.3.

Tree TraversalInOrder

a c

b

e g

f

i k

j

m o

n

d l

h

(left subtree) root (right subtree)abcdefghijklmno

52Info 3.3.

Tree TraversalInOrder

PROCEDURE InOrder(p:link);BEGIN IF p # NIL THEN InOrder(p^.Left); DoWhateverYouWant(p^.data); InOrder(p^.Right) END (* IF *)END InOrder;

53Info 3.3.

Tree TraversalPreOrder

a c

b

e g

f

i k

j

m o

n

d l

h

root (left subtree) (right subtree)hdbacfegljiknmo

54Info 3.3.

Tree TraversalPreOrder

PROCEDURE PreOrder(p:link);BEGIN IF p # NIL THEN DoWhateverYouWant(p^.data); PreOrder(p^.Left); PreOrder(p^.Right) END (* IF *)END PreOrder;

55Info 3.3.

Tree TraversalPostOrder

a c

b

e g

f

i k

j

m o

n

d l

h

(left subtree) (right subtree) rootacbegfdikjmonlh

56Info 3.3.

Tree TraversalPostOrder

PROCEDURE PostOrder(p:link);BEGIN IF p # NIL THEN PostOrder(p^.Left); PostOrder(p^.Right); DoWhateverYouWant(p^.data); END (* IF *) END PostOrder;

57Info 3.3.

Expression Trees

a

b c

/ d

e f

*

+ -

*

(a+b/c)*(d-e*f)

For evaluation on a stack machine : Postorder = abc/+def*-*

58Info 3.3.

Expression Syntax

Term Expression+

-

Factor Term*

/

Expression( )

Letter

Expression

Term

Factor

59Info 3.3.

Procedure Expression

Term Expression+

-

Expression

PROCEDURE Expression (VAR p: Link); VAR q:Link;BEGIN NEW(p);Term(p^Left); IF (Sy = '+') OR (Sy = '-') THEN p^.Op := Sy; GetSy; Expression(p^.Right) ELSE q:= p; p:= p^.Left; DISPOSE(q) END (* IF *)END Expression;

60Info 3.3.

Procedure Term

Factor Term*

/

Term

PROCEDURE Term (VAR p: Link); VAR q:Link;BEGIN NEW(p);Factor(p^Left); IF (Sy = '*') OR (Sy = '/') THEN p^.Op := Sy; GetSy; Term(p^.Right) ELSE q:= p; p:= p^.Left; DISPOSE(q) END (* IF *)END Term;

61Info 3.3.

Procedure Factor

Expression( )

LetterFactor

PROCEDURE Factor (VAR p: Link);BEGIN IF Sy = '(' THEN GetSy; Expression(p); GetSy ELSE NEW(p); p^.Op:= Sy; p^.Left:= NIL; p^.Right:= NIL GetSy END (* IF *)END Factor;

62Info 3.3.

(a+b/c)*(d-e/f)^

Expression

PROCEDURE Expression (VAR p: Link);... NEW(p);Term(p^Left);...

63Info 3.3.

(a+b/c)*(d-e/f)^

TermExpression

PROCEDURE Term (VAR p: Link);... NEW(p);Factor(p^Left);...

64Info 3.3.

(a+b/c)*(d-e/f).^

FactorTerm

Expression

PROCEDURE Factor (VAR p: Link);... IF Sy = '(' THEN GetSy; Expression(p); GetSy...

65Info 3.3.

(a+b/c)*(d-e/f).^

ExpressionFactorTerm

Expression

PROCEDURE Expression (VAR p: Link);... NEW(p);Term(p^Left);...

66Info 3.3.

(a+b/c)*(d-e/f).^

TermExpression

FactorTerm

Expression

PROCEDURE Term (VAR p: Link);... NEW(p);Factor(p^Left);...

67Info 3.3.

(a+b/c)*(d-e/f)..^

FactorTerm

ExpressionFactorTerm

Expression

a

PROCEDURE Factor (VAR p: Link);... IF Sy = '(' ... ELSE NEW(p); p^.Op:= Sy; p^.Left:= NIL; p^.Right:= NIL GetSy END (* IF *)END Factor;

68Info 3.3.

(a+b/c)*(d-e/f)..^

TermExpression

FactorTerm

Expression

a

PROCEDURE Term (VAR p: Link); ... NEW(p);Factor(p^Left); IF (Sy = '*') OR (Sy = '/') ... ELSE q:= p; p:= p^.Left; DISPOSE(q) END (* IF *)END Term;

69Info 3.3.

(a+b/c)*(d-e/f)...^

ExpressionFactorTerm

Expression

+

a

PROCEDURE Expression (VAR p: Link); ... NEW(p);Term(p^Left); IF (Sy = '+') OR (Sy = '-') THEN p^.Op := Sy; GetSy; Expression(p^.Right) ...

70Info 3.3.

(a+b/c)*(d-e/f)...^

ExpressionExpression

FactorTerm

Expression

PROCEDURE Expression (VAR p: Link); ... NEW(p);Term(p^Left); ...

+

a

71Info 3.3.

(a+b/c)*(d-e/f)...^

TermExpressionExpression

FactorTerm

Expression

PROCEDURE Term (VAR p: Link);... NEW(p);Factor(p^Left);...

+

a

72Info 3.3.

(a+b/c)*(d-e/f)....^

FactorTerm

ExpressionExpression

FactorTerm

Expression

+

a

b

PROCEDURE Factor (VAR p: Link);... IF Sy = '(' ... ELSE NEW(p); p^.Op:= Sy; p^.Left:= NIL; p^.Right:= NIL GetSy END (* IF *)END Factor;

73Info 3.3.

(a+b/c)*(d-e/f).....^

TermExpressionExpression

FactorTerm

Expression

+

/

a

b

PROCEDURE Term (VAR p: Link); ... NEW(p);Factor(p^Left); IF (Sy = '*') OR (Sy = '/') THEN p^.Op := Sy; GetSy; Term(p^.Right) ...

74Info 3.3.

(a+b/c)*(d-e/f).....^

TermTerm

ExpressionExpression

FactorTerm

Expression

+

/

a

b

PROCEDURE Term (VAR p: Link);... NEW(p);Factor(p^Left);...

75Info 3.3.

(a+b/c)*(d-e/f)......^

FactorTermTerm

ExpressionExpression

FactorTerm

Expression

+

/

a

b

PROCEDURE Factor (VAR p: Link);... IF Sy = '(' ... ELSE NEW(p); p^.Op:= Sy; p^.Left:= NIL; p^.Right:= NIL GetSy END (* IF *)END Factor;

c

76Info 3.3.

(a+b/c)*(d-e/f)......^

TermTerm

ExpressionExpression

FactorTerm

Expression

+

/

a

b c

PROCEDURE Term (VAR p: Link); ... NEW(p);Factor(p^Left); IF (Sy = '*') OR (Sy = '/') ... ELSE q:= p; p:= p^.Left; DISPOSE(q) END (* IF *)END Term;

77Info 3.3.

(a+b/c)*(d-e/f)......^

TermExpressionExpression

FactorTerm

Expression

+

/

a

b c

PROCEDURE Term (VAR p: Link); ... IF THEN Term(p^.Right) ... END (* IF *)END Term;

78Info 3.3.

(a+b/c)*(d-e/f)......^

ExpressionExpression

FactorTerm

Expression

+

/

a

b c

PROCEDURE Expression (VAR p: Link); ... IF (Sy = '+') OR (Sy = '-') ... ELSE q:= p; p:= p^.Left; DISPOSE(q) END (* IF *)END Expression;

79Info 3.3.

(a+b/c)*(d-e/f)......^

ExpressionExpression

FactorTerm

Expression

+

a /

b c

PROCEDURE Expression (VAR p: Link); ... IF (Sy = '+') OR (Sy = '-') ... ELSE q:= p; p:= p^.Left; DISPOSE(q) END (* IF *)END Expression;

80Info 3.3.

(a+b/c)*(d-e/f)......^

ExpressionFactorTerm

Expression

+

a /

b c

PROCEDURE Expression (VAR p: Link); ... IF THEN Expression(p^.Right) ... END (* IF *)END Expression;

81Info 3.3.

(a+b/c)*(d-e/f).......^

FactorTerm

Expression

+

a /

b c

PROCEDURE Factor (VAR p: Link);... IF Sy = '(' THEN GetSy; Expression(p); GetSy END (* IF *)END Factor;

82Info 3.3.

(a+b/c)*(d-e/f).......^

TermExpression

+

a

*

/

b c

PROCEDURE Term (VAR p: Link); ... NEW(p);Factor(p^Left); IF (Sy = '*') OR (Sy = '/') THEN p^.Op := Sy; GetSy; Term(p^.Right) ...

83Info 3.3.

(a+b/c)*(d-e/f)........^

TermTerm

Expression

+

a

*

/

b c

PROCEDURE Term (VAR p: Link); ... NEW(p);Factor(p^Left);...

84Info 3.3.

(a+b/c)*(d-e/f)...............^

*

+

a /

b c

-

d /

e f