ch7 - Ch7. Trees: 7.1 Introduction: Def: rooted tree is a...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
2001-2009 M. D. Evans All Rights Reserved 1 Ch7. Trees: 7.1 Introduction: Def: rooted tree – is a finite set T of 1 or more nodes such that: i) one designated node is called the root ii) the remaining nodes are partitioned into m disjoint sets T 1 , . . , T m , each of which is a subtree of the root. Ex.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2001-2009 M. D. Evans All Rights Reserved 2 Def: Forest Leaf Branch node Parent, child, sibling nodes Degree of a node Level of a node Height of a tree Path length between 2 nodes Binary tree – is a finite set of nodes that is either empty, or consists of a root and 2 disjoint binary trees called the LST and the RST. Note: - The definition is recursive. - LST / RST may have 2, 1, or 0 children. - Also called Knuth B.T.
Background image of page 2
2001-2009 M. D. Evans All Rights Reserved 3 Strict Binary Tree – a binary tree where each node has exactly 0 or 2 children. Full Binary Tree – a B.T. with all of its leaves on 1 level, and each node has 0 or 2 children. Note: 1) also called a complete B.T. 2) a FBT of height k has 2 k – 1 nodes and 2 k-1 leaves. Ex:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2001-2009 M. D. Evans All Rights Reserved 4 7.2 The Tree ADT: Operations: isEmpty() bool search (item, foundPosn) insert (item) delete (item) Attributes: root collection of items in tree height ?? size ??
Background image of page 4
2001-2009 M. D. Evans All Rights Reserved 5 7.3 Binary Tree Operations: Traversal: Definition of B.T. n T L T R b recursive traversal. ? when to ‘take action’ on the root? if n nodes ==> n! traversal possibilities, but not all have a pattern/regularity which is useful. 3 choices: 1) visit root before T L and T R 2) T L , visit root, T R 3) T L , T R , visit root Ex: Consider the expression tree:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2001-2009 M. D. Evans All Rights Reserved 6 The Traversal Algorithms: 1) Preorder - visit root - search LST in pre-order - search RST in pre-order b = A + * b c / - 3.6 * d e * f 4 2) Inorder - search LST in pre-order - visit root - search RST in pre-order b A = b * c + 3.6 - d * e / f * 4 3) Postorder - search LST in pre-order - search RST in pre-order - visit root b A b c * 3.6 d e * - f 4 * / + =
Background image of page 6
2001-2009 M. D. Evans All Rights Reserved 7 Note: an easy way to recall the traversals = The “Walled City” Traversals: Preorder – pass tower 1 st time (on west) Inorder – pass tower 2 nd time (on south) Postorder – pass tower 3 rd time (on east)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2001-2009 M. D. Evans All Rights Reserved 8 7.4 Binary Search Trees: Def: a BST is a BT in which the values in the LST of a node are all < the value of the node, and in which the values in the RST are all > the node’s value, and in which all LST’s and RST’s are BST’s,
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/13/2010 for the course CMPT 225 taught by Professor Annelavergne during the Spring '07 term at Simon Fraser.

Page1 / 28

ch7 - Ch7. Trees: 7.1 Introduction: Def: rooted tree is a...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online