Programming, Block C Today’s Topics http://www.win.tue.nl/˜wstomv/2ip05/ • Branching dynamic data structures with pointers: Lecture 15 – Trees Tom Verhoe ff Kees Hemerik – Binary Trees (BTs) – Binary Search Trees (BSTs) Technische Universiteit Eindhoven Faculteit Wiskunde en Informatica Software Engineering & Technology • More recursion Feedback to T.Verhoeff@TUE.NL � 2007, T. Verhoe ff @ TUE.NL c 1 Programming, Block C: Lecture 15 � 2007, T. Verhoe ff @ TUE.NL c 2 Programming, Block C: Lecture 15 Linear Structures: Linked Lists Branching Structures: (Binary (Search)) Trees In computing, trees are everywhere • Storage overhead : 1 or 2 pointers per cell • Syntax trees A – Single-linked versus double-linked (bi-directional) B – 7 • Directory trees C • Access/modification time : worst-case proportional to list length D 3 11 • Widget trees Linear in the actual size: O ( N ) E 2 5 F • Search trees How to improve the worst-case time for operations? • . . . � 2007, T. Verhoe ff @ TUE.NL c 3 Programming, Block C: Lecture 15 � 2007, T. Verhoe ff @ TUE.NL c 4 Programming, Block C: Lecture 15
Tree Concepts and Terminology Binary Trees • Graph: node, edge (connected, no cycles) The set BT ( S ) of Binary Trees over a set S is inductively defined by • Parent, child, subtree, order (Basis) ε ∈ BT ( S ) (the empty tree) 7 (Step) if L ∈ BT ( S ) , a ∈ S, R ∈ BT ( S ), then � L, a, R � ∈ BT ( S ) • Root, leaf, front, root path 3 11 • Arity (fixed, variable) Some examples in BT ( N ): 7 2 5 • ε • Height, balance 3 11 • � ε , 2 , ε � • �� ε , 2 , ε � , 3 , � ε , 5 , ε �� • Traversal: pre-order, in-order, post-order 2 5 • ��� ε , 2 , ε � , 3 , � ε , 5 , ε �� , 7 , � ε , 11 , ε �� � 2007, T. Verhoe ff @ TUE.NL c 5 Programming, Block C: Lecture 15 � 2007, T. Verhoe ff @ TUE.NL c 6 Programming, Block C: Lecture 15 Recursive Functions on Binary Trees Binary Tree Representation in Pascal 1 type The height h.t of a binary tree t is recursively defined by TData = ...; // type for content of the binary trees 2 PNode = ˆTNode; 3 TNode = // node of binary tree over TData 4 (Basis) h. ε = 0 record 5 FData: TData; // value stored in this node 6 FLeft: PNode; // left subtree 7 (Step) h. � L, a, R � = 1 + ( h.L ↑ h.R ) FRight: PNode; // right subtree 8 end ; 9 7 TBTData = PNode; // Binary Trees over TData h. � ε , 11 , ε � = 1 10 // data invariants 11 3 11 // I0: all root paths via FLeft/FRight terminate in nil 12 h. � ε , 2 , ε � , 3 , � ε , 5 , ε �� = 2 // I1: if two root paths both end in u then u = nil 13 2 5 // Abstraction function for t: TBTData 14 h. ��� ε , 2 , ε � , 3 , � ε , 5 , ε �� , 7 , � ε , 11 , ε �� = 3 // empty tree if t=nil else <tˆ.FLeft, tˆ.FData, tˆ.FRight> 15 � 2007, T. Verhoe ff @ TUE.NL c 7 Programming, Block C: Lecture 15 � 2007, T. Verhoe ff @ TUE.NL c 8 Programming, Block C: Lecture 15
Binary Tree Type in Pascal: Examples Binary Trees in Pascal: Observations • Storage overhead : 2 pointers per node t t0 t1 7 7 7 • # nil pointers = # nodes + 1 3 11 3 11 • Maximum # nodes in tree of height N = 2 N − 1 2 5 5 • Minimum # nodes in tree of height N = N • log 2 (1 + #nodes) ≤ height ≤ #nodes t is OK t0 violates I0 t1 violates I1 � 2007, T. Verhoe ff @ TUE.NL c 9 Programming, Block C: Lecture 15 � 2007, T. Verhoe ff @ TUE.NL c 10 Programming, Block C: Lecture 15 Creating a Binary Tree in Pascal Creating a Binary Tree in Pascal: Example The empty tree ε is represented by nil . A nonempty tree is best created through an auxiliary function: 1 var 1 function MakeBTData(a: TData; L, R: TBTData): TBTData; t: TBTData; // a binary tree over TData 2 // pre: L and R do not share any nodes 2 3 // ret: <L, a, R> 3 4 begin begin 4 t := MakeBTData(7, New(Result); 5 5 with Resultˆ do begin 6 MakeBTData(3, 6 FData := a; 7 MakeBTData(2, nil , nil ), 7 FLeft := L; 8 MakeBTData(5, nil , nil )), 8 FRight := R 9 MakeBTData(11, nil , nil )); 9 end ; { with } 10 // Result satisfies I0 and I1 11 end ; 12 � 2007, T. Verhoe ff @ TUE.NL c 11 Programming, Block C: Lecture 15 � 2007, T. Verhoe ff @ TUE.NL c 12 Programming, Block C: Lecture 15
Height of Binary Tree in Pascal Binary Tree Traversals 1 uses Math; // for Max 2 Standard orders for “visiting” the nodes of a binary tree: 3 4 function Height(t: TBTData): Integer; // pre: true 5 • Pre-order : first the node itself, then the subtrees // ret: h.t 6 7 begin • In-order : first left subtree, next the node, then right subtree if t = nil then begin 8 Result := 0; 9 end 10 • Post-order : first the subtrees, then the node else { t <> nil } begin 11 Result := 1 + Max ( Height(tˆ.FLeft), Height(tˆ.FRight) ); 12 // termination guaranteed because of I0 13 Coding techniques: recursively or with repetition (and self-made stack) end ; 14 end ; 15 � 2007, T. Verhoe ff @ TUE.NL c 13 Programming, Block C: Lecture 15 � 2007, T. Verhoe ff @ TUE.NL c 14 Programming, Block C: Lecture 15 Pre-order Traversal in Pascal (recursive) In-order Traversal in Pascal (recursive) 1 procedure PreOrder(t: TBTData); Slightly “optimized” code // pre: true 2 1 procedure InOrder(t: TBTData); // post: all nodes of t have been processed in pre-order 3 // pre: true 2 begin 4 // post: all nodes of t have been processed in in-order 3 if t = nil then begin 5 begin // skip 4 6 if t <> nil then begin end 7 5 else { t <> nil } begin 8 with tˆ do begin 6 with tˆ do begin 9 InOrder(FLeft); 7 ... process FData ...; 10 ... process FData ...; 8 PreOrder(FLeft); 11 InOrder(FRight); 9 PreOrder(FRight); 12 end ; { with } 10 end ; { with } 13 end ; { if } 11 end ; 14 end ; 12 end ; 15 � 2007, T. Verhoe ff @ TUE.NL c 15 Programming, Block C: Lecture 15 � 2007, T. Verhoe ff @ TUE.NL c 16 Programming, Block C: Lecture 15
Parameterized Post-order Traversal in Pascal (recursive) Apply Parameterized Post-order Traversal in Pascal 1 type TDataAction = procedure ( const AData: TData ); 2 3 procedure PostOrder(t: TBTData; ADataAction: TDataAction); 1 procedure WriteData( const AData: TData); // pre: true 4 begin 2 // post: all nodes of t have been processed in post-order 5 Writeln(... AData ...) 3 // by applying ADataAction 6 end ; 4 7 begin 5 if t <> nil then begin 8 6 var with tˆ do begin 9 t: TBTData; // a binary tree 7 PostOrder(FLeft, ADataAction); 10 8 PostOrder(FRight, ADataAction); 11 9 // WriteData has type TDataAction ADataAction(FData); 12 end ; { with } 10 PostOrder(t, WriteData); 13 end ; { if } 14 end ; 15 � 2007, T. Verhoe ff @ TUE.NL c 17 Programming, Block C: Lecture 15 � 2007, T. Verhoe ff @ TUE.NL c 18 Programming, Block C: Lecture 15 Apply Post-order Traversal in Pascal (2) Binary Search Trees Time to find data in a binary tree is still worst-case linear in the size 1 var count: Integer; // global counter for CountData 2 Can be improved by keeping data sorted 3 4 procedure CountData( const AData: TData {; globvar count} ); The list ℓ .t of a binary tree t is inductively defined by // effect: count := count + 1 5 6 begin count := count + 1 7 (Basis) ℓ . ε = ε (the empty list) end ; 8 9 (Step) ℓ . � L, a, R � = ℓ .L + + [ a ] + + ℓ .R (cf. in-order traversal) 10 var t: TBTData; // a binary tree over TData 11 12 A Binary Search Tree is a binary tree with additional invariant: 13 count := 0; 14 PostOrder(t, CountData); • its list is strictly increasing (w.r.t. a suitable order on S ) 15 // count = # nodes in t � 2007, T. Verhoe ff @ TUE.NL c 19 Programming, Block C: Lecture 15 � 2007, T. Verhoe ff @ TUE.NL c 20 Programming, Block C: Lecture 15
Recommend
More recommend