VIEWS: 2 PAGES: 7 POSTED ON: 3/24/2010
Outline Trees CS240: Data Structures and Data Management Simple Tree ADT Deﬁnitions J´r´my Barbay ee Traversals Binary Trees Binary Representation of General Trees January 19 2006 Tree ADT Deﬁnitions Operations Recursive: A ﬁnite collection of nodes (at least one) that is root(), size() 1. A single distinguished node called the root or isInternal( node ), children( node ), 2. Partitioned into k + 1 subcollections: a designated root node parent( node ) connected together with k trees, T1 . . . Tk , by an edge attachSubtree( node, tree ), Graph which is Rooted, oriented, connected and acyclic detachSubtree( node ) List of nodes and edges s.t. all nodes but one have a parent Use trees to implement other ADTs node. Terminology Applications a Representing Hierarhies: Genealogical tree. parent, child, sibling, subtree DNS in Networking. ancestor, descendent b c (note: a node is its own Modelisation of algorithms (Merge Sort, Comparison based ancestor and descendent) 1 d e 2 searching). external node (leaf) Parsing (Arithmetic expression, LTEX, XML). A 3 4 5 f 6 internal node Codes (Huﬀman). 7 Depth/Height Tree variants Unordered – like a graph a = a b c d d c b Node Depth – The number of edges between the node, and the root of the entire tree Ordered – linear ordering on the children (ﬁrst, second, . . . ) Node Height – The maximum number of edges between the a = a node and any of its descendants b c d d c b Note: depth(root) = 0 Cardinal – children identiﬁed by their absolute position. height(leaf ) = 0 a = a Exercise: How do we compute each of these for a given node? b d b d Ordinal – children identiﬁed by their rank. a = a b c b c Tree variants (cont’) Binary Trees Binary Tree Data Structures Binary – each node has at most 2 children (cardinal tree) Proper Binary – each node has 0 or 2 children Linked Structure Full Binary – proper binary tree, all leaves at the same level Tree Node with 4 ﬁelds parent Example data a left right This tree is: Parent is optional b e binary True Array Proper Binary True An array of size 2h+1 , from 1 to 2h+1 . c d f i Full Binary False children of cell i at positions 2i and 2i + 1. g h j k Special value indicates no node. More on this with heaps. We will study more binary trees with There are more sophisticated ones... Priority Queue ADT (Heaps) Ordered Dictionaries ADT (AVL Trees) Properties of Binary Trees More Properties of proper binary trees h + 1 ≤ |E | ≤ 2h Theorem a Let |E | and |I | represent the number of external and internal nodes respectively in a proper binary tree. Then a |E | = |I | + 1 a a a a Proof: By Induction on |I | Base Case(s): |I | = 0 and |I | = 1 a a a a a Inductive Cases: a a a a a a a 1. Root node has one internal child 2. Root node has two internal children As n = |E | + |I |, this gives bounds on |I |, n and h. Recursive General Traversal More speciﬁc Traversals a General Traverse(node) a b c Visit node if node has left child then b c d e f Traverse( node.left ) end if d e f g h Visit node g h if node has right child then Depth-First Traversal Traverse( node.right ) abdgggddbbaceeecfgggffca General Traversal: abdgggddbbaceeecfhhhﬀca end if Pre-Order: abdgcefh Visit node In-Order: gdbaechf This algorithm is easily modiﬁed for other depth ﬁrst traversals, Post-Order: gdbehfca or for trees of higher degree. Breadth-First Traversal: abcdgefh Level-Order: abcdefgh Representation of a binary tree Summary How can we prove if a trace identiﬁes a tree? When tree is identiﬁed: Method to build the tree. Otherwise: Two distinct trees with same trace. The Tree ADT deﬁne operators for navigation and construction; Which traversal permit to identify a binary tree by the trace? terms: Height, Depth, ... properties 1. general: True with many variants: Cardinal/Ordinal, ... 2. pre-order: False The Binary Tree is a particular cardinal variant, 3. in-order: False which will be studied more in details later. 4. post-order: False 5. breadth-ﬁrst order: False 6. level-order: False Outline Tree Encodings Documents structured as a tree Some trees represent static documents, which must be stored. section Simple Tree ADT Deﬁnitions section ﬁg para section Traversals Binary Trees para para para Binary Representation of General Trees How do we encode a tree? Dynamically, with one data array per node Dynamically, but with one number per node Statically, but how? Separating structure from content Structural Encoding of Ordinal Trees Encode separately Theorem An ordinal tree of n nodes can be encoded in 2n bits. the structure section * Transpose(x) * * * * * print “(” section ﬁg para section * * * * for each child c of x do para para para * * * Transpose(c) * * * end for from the content ssppfpsp print “)” ( ( ( ) () ) () () ( () ) ) How much space do we need to encode each part? Exercises n lg σ bits or n words for the content, where n is the number of nodes and σ is number of distinct labels. 1. How do we build the tree from the string? 2. Can we do the same for binary (cardinal) trees? Outline Binary Tree Representation of Ordinal Trees Theorem An ordinal tree T can be represented by a (cardinal) binary tree T . For each internal node v ∈ T , an internal node v ∈ T If v has an immediate sibling w , then w is the right child of v Simple Tree ADT If v has ﬁrst child w , then w is the left child of v Deﬁnitions Traversals Fill all other spots with empty external nodes (i.e. leaves). Binary Trees a Binary Representation of General Trees b a c b c d d Ordinal Tree Representation of Binary Trees Exercise Ordinal to Binary Binary-Tree Representation T : A Theorem A (cardinal) binary tree T can be represented by forest of ordinal trees. B Original Tree T : Two-by-two correspondance between internal nodes. The right child of v is the sibling of v . A E C The left child of v is the ﬁrst child of v . D Ignore empty subtrees, B C D a a c E F G H F b c G b H Exercise Operations on this representation Binary to Ordinal Given an ordinal tree representated in a binary tree: 1 1. How to compute the height? By counting the maximum number of left edges on a rooted path. Complexity O(n). 1 9 15 2. How to compute the maximum degree? By counting the 2 9 maximum number of right edges on a rooted path. 2 8 10 14 Complexity O(n). 3 8 10 15 3 5 7 11 13 4 5 11 14 Exercise: 4 6 12 Given a binary tree representated as a forest of ordinal trees, how 6 7 12 13 to compute the height?
Pages to are hidden for
"Trees Outline Tree ADT Definitions"Please download to view full document