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 Common Recursive Data Structures Linear Lists Binary Trees

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

4 Info 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 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 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 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 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 Ordered Binary Trees Node Deletion different situations : a) not present : 8 b) 0 or 1 successor : 1, 2, 4, 5, 7 c) 2 successors : 3, 6

10 Info Ordered Binary Trees Deletion of node

11 Info Ordered Binary Trees Deletion of node Root := Rightmost of left subtree

12 Info Ordered Binary Trees Deletion of node Root := Leftmost of right subtree

13 Info 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 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 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 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 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 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 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 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 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 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 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 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 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 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 AVL Tree Balancing A B 2 different situations : Case 1 : imbalance between outer subtrees Case 2 : imbalance between inner and outer subtrees

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

29 Info 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 AVL Tree Balancing Case 2 : inner imbalance (2) A B C AB C

31 Info 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 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 AVL Trees Dictionary Building (2) (* Insert Node *) NEW(p); WITH p^ DO Key := x; Left := NIL; Right := NIL; Bal := 0 END;

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

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

36 Info 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 AVL Trees Rearange, case 1 (1) A B P P1

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

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

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

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

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

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

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

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

46 Info 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 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 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 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 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 Tree Traversal InOrder ac b eg f ik j mo n d l h (left subtree) root (right subtree) abcdefghijklmno

52 Info 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 Tree Traversal PreOrder ac b eg f ik j mo n d l h root (left subtree) (right subtree) hdbacfegljiknmo

54 Info 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 Tree Traversal PostOrder ac b eg f ik j mo n d l h (left subtree) (right subtree) root acbegfdikjmonlh

56 Info 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 Expression Trees a bc /d ef * + - * (a+b/c)*(d-e*f) For evaluation on a stack machine : Postorder = abc/+def*-*

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

59 Info 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 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 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 (a+b/c)*(d-e/f) ^ Expression PROCEDURE Expression (VAR p: Link);... NEW(p);Term(p^Left);...

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

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

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

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

67 Info (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 (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 (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 (a+b/c)*(d-e/f)...^ Expression Factor Term Expression PROCEDURE Expression (VAR p: Link);... NEW(p);Term(p^Left);... + a

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

72 Info (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (a+b/c)*(d-e/f) ^ Term Expression + a * / b c PROCEDURE Term (VAR p: Link);... NEW(p);Factor(p^Left);...

84 Info (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