Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Info 3.3. Chapter 3.3 Recursive Data Structures Part 2 : Binary Trees."— Presentation transcript:

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

2 Info 3.3. 2 Common Recursive Data Structures Linear Lists Binary Trees

3 Info 3.3. 3 A Recursive Data Structure Example : A Pedigree FatherMother Name MygrandfatherMygrandmotherMygrandfatherMygrandmother MyfatherMymother Myself

4 Info 3.3. 4 Recursive Data Structures Implementation by variant pointers FatherMother Name TYPE Link = POINTER TO Person; Person = RECORD Name : String; Father, Mother : Link END FatherMother Name FatherMother Name FatherMother Name FatherMother Name FatherMother Name

5 Info 3.3. 5 Binary Trees Definitions Right subtree Left subtree Leaves ROOT Ordered binary tree : when keys of - all elements of left subtree smaller than Root - all elements of right subtree larger dan Root.

6 Info 3.3. 6 Ordered Binary Trees Definitions 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 < tLog 2 n

7 Info 3.3. 7 Ordered Binary Trees Data Declarations for Dictionary TYPE Link = POINTER TO Node; Node = RECORD Key : String; (* other data fields *) Left, Right : Link END;

8 Info 3.3. 8 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;

9 Info 3.3. 9 Ordered Binary Trees Node Deletion 3 26 14 5 7 3 different situations : a) not present : 8 b) 0 or 1 successor : 1, 2, 4, 5, 7 c) 2 successors : 3, 6

10 Info 3.3. 10 Ordered Binary Trees Deletion of node 2 3 26 14 5 7 3 6 14 5 7

11 Info 3.3. 11 Ordered Binary Trees Deletion of node 3 3 26 14 5 7 26 14 5 7 Root := Rightmost of left subtree

12 Info 3.3. 12 Ordered Binary Trees Deletion of node 3 3 26 14 5 7 Root := Leftmost of right subtree 26 14 5 7

13 Info 3.3. 13 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;

14 Info 3.3. 14 Ordered Binary Trees Node Deletion Procedure (2) (* Remove p^ *) q := p; IF q^.Right = NIL THEN p:= q^.Left ELSIF q^.Left = NIL THEN p:= q^.Right ELSE RightMost(p^.Left) END; (* IF *) DISPOSE(q)

15 Info 3.3. 15 Ordered Binary Trees Node Deletion Procedure (3) (* Remove p^ *) q := p; IF q^.Right = NIL THEN p:= q^.Left ELSIF q^.Left = NIL THEN p:= q^.Right ELSE RightMost(p^.Left) END; (* IF *) DISPOSE(q) pqpq can be NIL

16 Info 3.3. 16 Ordered Binary Trees Node Deletion Procedure (4) (* Remove p^ *) q := p; IF q^.Right = NIL THEN p:= q^.Left ELSIF q^.Left = NIL THEN p:= q^.Right ELSE RightMost(p^.Left) END; (* IF *) DISPOSE(q) pqpq

17 Info 3.3. 17 Ordered Binary Trees Node Deletion Procedure (5) (* Remove p^ *) q := p; IF q^.Right = NIL THEN p:= q^.Left ELSIF q^.Left = NIL THEN p:= q^.Right ELSE RightMost(p^.Left) END; (* IF *) DISPOSE(q) pqpq

18 Info 3.3. 18 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; pqpq r =

19 Info 3.3. 19 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; pqpq r =

20 Info 3.3. 20 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 pqpq r X

21 Info 3.3. 21 Ordered Binary Trees Node Deletion Procedure (9) (* Remove p^ *) q := p; IF q^.Right = NIL THEN p:= q^.Left ELSIF q^.Left = NIL THEN p:= q^.Right ELSE RightMost(p^.Left) END; (* IF *) DISPOSE(q) X pqpq r

22 Info 3.3. 22 Balanced Trees Perfectly balanced tree : | NNLS - NNRS | <= 1 NNLS = Nbr Nodes in Left Subtree NNRS = Nbr Nodes in Right Subtree AVL tree (Adelson-Velskii & Landis) : | HLS-HRS | <= 1 HLS = Height of Left Subtree HRS = Height of Right Subtree

23 Info 3.3. 23 Perfectly Balanced Tree All nodes should be perfectly balanced ! 1/00/1 2/2 1/11/0 3/2 0/01/0 1/2 1/11/0 3/2 5/6 4/6 12/11

24 Info 3.3. 24 Perfectly Balanced Tree All nodes should be perfectly balanced ! 1/00/1 2/2 1/11/0 3/2 1/0 2/2 1/11/0 3/2 5/6 12/12

25 Info 3.3. 25 AVL Balanced Tree AVL condition needs to be satisfied everywhere ! 0/0 1/1 0/0 1/0 2/2 1/1 2/0 2/2 3/3 3/4

26 Info 3.3. 26 AVL Balanced Tree AVL condition needs to be satisfied everywhere ! 0/0 1/1 0/0 1/0 2/2 1/10/0 2/1 2/2 3/3 3/4

27 Info 3.3. 27 AVL Tree Balancing A B 2 different situations : Case 1 : imbalance between outer subtrees Case 2 : imbalance between inner and outer subtrees

28 Info 3.3. 28 AVL Tree Balancing Case 1 : outer imbalance A B A B

29 Info 3.3. 29 AVL Tree Balancing Case 2 : inner imbalance (1) A B A B C A B C Inner imbalance can appear in two different situations

30 Info 3.3. 30 AVL Tree Balancing Case 2 : inner imbalance (2) A B C AB C

31 Info 3.3. 31 AVL Trees Data 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;

32 Info 3.3. 32 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;

33 Info 3.3. 33 AVL Trees Dictionary Building (2) (* Insert Node *) NEW(p); WITH p^ DO Key := x; Left := NIL; Right := NIL; Bal := 0 END;

34 Info 3.3. 34 Adjust Balance Left p^.Bal = rearange 0+1 p^.Bal := h := 0 FALSE TRUE 0 FALSE

35 Info 3.3. 35 Rearange +1 p^Bal = p^.Left^.Bal = to be updated updated rearange case 1rearange case 2

36 Info 3.3. 36 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 := FALSE END; (* CASE *)

37 Info 3.3. 37 AVL Trees Rearange, case 1 (1) A B P P1

38 Info 3.3. 38 AVL Trees Rearange, case 1 (2) p^.Left := p1^.Right A B P P1

39 Info 3.3. 39 AVL Trees Rearange, case 1 (3) p^.Left := p1^.Right; p1^.Right := p; A B P P1

40 Info 3.3. 40 AVL Trees Rearange, case 1 (4) p^.Left := p1^.Right; p1^.Right := p; p^.Bal := 0; A B P P1

41 Info 3.3. 41 AVL Trees Rearange, case 1 (5) p^.Left := p1^.Right; p1^.Right := p; p^.Bal := 0; p := p1; A B P P1

42 Info 3.3. 42 AVL Trees Rearange, case 1 (6) p^.Left := p1^.Right; p1^.Right := p; p^.Bal := 0; p := p1; A B P

43 Info 3.3. 43 AVL Trees Rearange, case 2 (1) p2 := p1^.Right; A B C P P1 P2

44 Info 3.3. 44 AVL Trees Rearange, case 2 (2) p2 := p1^.Right; p1^.Right := p2^.Left A B C P P1 P2

45 Info 3.3. 45 AVL Trees Rearange, case 2 (3) p2 := p1^.Right; p1^.Right := p2^.Left; p2^.Left := p1; A B C P P1 P2

46 Info 3.3. 46 AVL Trees Rearange, case 2 (4) p2 := p1^.Right; p1^.Right := p2^.Left; p2^.Left := p1; p^.Left := p2^.Right; A B C P P1 P2

47 Info 3.3. 47 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 P P1 P2

48 Info 3.3. 48 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 := -1 END; (* CASE *) A B C P P1 P2

49 Info 3.3. 49 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 := -1 END; (* CASE *) p := p2; A B C P P1 P2

50 Info 3.3. 50 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 := -1 END; (* CASE *) p := p2; AB C P

51 Info 3.3. 51 Tree Traversal InOrder ac b eg f ik j mo n d l h (left subtree) root (right subtree) abcdefghijklmno

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

53 Info 3.3. 53 Tree Traversal PreOrder ac b eg f ik j mo n d l h root (left subtree) (right subtree) hdbacfegljiknmo

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

55 Info 3.3. 55 Tree Traversal PostOrder ac b eg f ik j mo n d l h (left subtree) (right subtree) root acbegfdikjmonlh

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

57 Info 3.3. 57 Expression Trees a bc /d ef * + - * (a+b/c)*(d-e*f) For evaluation on a stack machine : Postorder = abc/+def*-*

58 Info 3.3. 58 Expression Syntax Term Expression + - Factor Term * / Expression () Letter Expression Term Factor

59 Info 3.3. 59 Procedure Expression Term 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;

60 Info 3.3. 60 Procedure Term Factor 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;

61 Info 3.3. 61 Procedure Factor Expression () Letter Factor 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;

62 Info 3.3. 62 (a+b/c)*(d-e/f) ^ Expression PROCEDURE Expression (VAR p: Link);... NEW(p);Term(p^Left);...

63 Info 3.3. 63 (a+b/c)*(d-e/f) ^ Term Expression PROCEDURE Term (VAR p: Link);... NEW(p);Factor(p^Left);...

64 Info 3.3. 64 (a+b/c)*(d-e/f).^ Factor Term Expression PROCEDURE Factor (VAR p: Link);... IF Sy = '(' THEN GetSy; Expression(p); GetSy...

65 Info 3.3. 65 (a+b/c)*(d-e/f).^ Expression Factor Term Expression PROCEDURE Expression (VAR p: Link);... NEW(p);Term(p^Left);...

66 Info 3.3. 66 (a+b/c)*(d-e/f).^ Term Expression Factor Term Expression PROCEDURE Term (VAR p: Link);... NEW(p);Factor(p^Left);...

67 Info 3.3. 67 (a+b/c)*(d-e/f)..^ Factor Term Expression Factor Term 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;

68 Info 3.3. 68 (a+b/c)*(d-e/f)..^ Term Expression Factor Term 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;

69 Info 3.3. 69 (a+b/c)*(d-e/f)...^ Expression Factor Term Expression + a PROCEDURE Expression (VAR p: Link);... NEW(p);Term(p^Left); IF (Sy = '+') OR (Sy = '-') THEN p^.Op := Sy; GetSy; Expression(p^.Right)...

70 Info 3.3. 70 (a+b/c)*(d-e/f)...^ Expression Factor Term Expression PROCEDURE Expression (VAR p: Link);... NEW(p);Term(p^Left);... + a

71 Info 3.3. 71 (a+b/c)*(d-e/f)...^ Term Expression Factor Term Expression PROCEDURE Term (VAR p: Link);... NEW(p);Factor(p^Left);... + a

72 Info 3.3. 72 (a+b/c)*(d-e/f)....^ Factor Term Expression Factor Term 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;

73 Info 3.3. 73 (a+b/c)*(d-e/f).....^ Term Expression Factor Term 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)...

74 Info 3.3. 74 (a+b/c)*(d-e/f).....^ Term Expression Factor Term Expression + / a b PROCEDURE Term (VAR p: Link);... NEW(p);Factor(p^Left);...

75 Info 3.3. 75 (a+b/c)*(d-e/f)......^ Factor Term Expression Factor Term 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

76 Info 3.3. 76 (a+b/c)*(d-e/f)......^ Term Expression Factor Term 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;

77 Info 3.3. 77 (a+b/c)*(d-e/f)......^ Term Expression Factor Term Expression + / a b c PROCEDURE Term (VAR p: Link);... IF THEN Term(p^.Right)... END (* IF *) END Term;

78 Info 3.3. 78 (a+b/c)*(d-e/f)......^ Expression Factor Term 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;

79 Info 3.3. 79 (a+b/c)*(d-e/f)......^ Expression Factor Term 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;

80 Info 3.3. 80 (a+b/c)*(d-e/f)......^ Expression Factor Term Expression + a / b c PROCEDURE Expression (VAR p: Link);... IF THEN Expression(p^.Right)... END (* IF *) END Expression;

81 Info 3.3. 81 (a+b/c)*(d-e/f).......^ Factor Term Expression + a / b c PROCEDURE Factor (VAR p: Link);... IF Sy = '(' THEN GetSy; Expression(p); GetSy END (* IF *) END Factor;

82 Info 3.3. 82 (a+b/c)*(d-e/f).......^ Term Expression + 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)...

83 Info 3.3. 83 (a+b/c)*(d-e/f)........^ Term Expression + a * / b c PROCEDURE Term (VAR p: Link);... NEW(p);Factor(p^Left);...

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


Download ppt "Info 3.3. Chapter 3.3 Recursive Data Structures Part 2 : Binary Trees."

Similar presentations


Ads by Google