throbber
DATA STRUCTURES
`
`ehaL
`
`JOHN E. HOPCRO
`Saltmeme)-\,|
`
`
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 1
`
`

`

`Data Structures
`
`and Algorithms
`
`ALFRED V. AHO
`
`Bell Laboratories
`Murray Hill, New Jersey
`
`JOHN E. HOPCROFT
`
`Cornell University
`Ithaca, New York
`
`JEFFREY D. ULLMAN
`
`Stanford University
`Stanford, California
`
`.,•., ADDISON-WESLEY PUBLISHING COMPANY
`Reading, Massachusetts • Menlo Park, California
`London • Amsterdam • Don Mills, Ontario • Sydney
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 2
`
`

`

`This book is in the
`ADDISON-WESLEY SERIES IN
`COMPUTER SCIENCE AND INFORMATION PROCESSING
`
`MichaeJ A. Harrison
`Consulting Editor
`
`Ubnry of Congress C.liloging In PubUcatlon Data
`
`Aho. Alfred V.
`Data structures and algorithms.
`1. Data structures (Computer science) 2. Algorithms.
`I. Hopcroft, John E., 1939-
`. II. Ullman,
`Jeffrey D., 1942-
`III. Title.
`QA76.9.D35A38 1982
`001.64
`ISBN (}.201-00023-7
`
`82-11596
`
`Reproduced by Addison-Wesley from camera-ready copy supplied by the authors.
`
`Reprinted with corrections April, 1987
`Copyright e 1983 by Bell Telephone Laboratories, Incorporated.
`
`All rights reserved. No part of this publication may be reproduced, stored in a re(cid:173)
`trieval system, or transmitted, in any form or by any means, electronic, mechanical,
`photocopying, recording, or otherwise, without the prior written permission of the pub(cid:173)
`lisher. Printed in the United States of America. Published simultaneously in Canada.
`
`ISBN: 0-201-00023-7
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 3
`
`

`

`Contents
`
`Chapter 1
`
`I. I
`1.2
`l.3
`l.4
`l.5
`1.6
`l.7
`
`Chapter 2
`
`2. l
`2.2
`2.3
`2.4
`2.5
`2.6
`
`Chapter 3
`
`3. l
`3.2
`3.3
`3.4
`
`Chapter 4
`
`4. l
`4.2
`4.3
`4.4
`4.5
`4.6
`4.7
`4.S
`4.9
`4.10
`4.ll
`4.12
`
`Design and Analysis of Algorithms
`From Problems to Programs ................................................ I
`Abstract Data Types ........................................................ 10
`Data Types, Data Structures, and Abstract Data Types ............ 13
`The Running Time of a Program ........................................ 16
`Calculating the Running Time of a Program .......................... 21
`Good Programming Practice .............................................. 27
`Super Pascal ................................................................... 29
`
`Basic Data Types
`The Data Type "List" ...................................................... 37
`Implementation of Lists ................................................... .40
`Stacks ........................................................................... 53
`Queues ......................................................................... 56
`Mappings ...................................................................... 61
`Stacks and Recursive Procedures ........................................ 64
`
`Trees
`Basic Terminology ........................................................... 75
`The ADT TREE ............................................................. 82
`Implementations of Trees .................................................. 84
`Binary Trees .................................................................. 93
`
`Basic Operations on Sets
`Introduction to Sets ........................................................ I 07
`An ADT with Union, Intersection, and Difference ................ 109
`A Bit-Vector Implementation of Sets .................................. 112
`A Linked-List Implementation of Sets ................................ 115
`The Dictionary .............................................................. 117
`Simple Dictionary Implementations .................................... 119
`The Hash Table Data Structure ......................................... 122
`Estimating the Efficiency of Hash Functions ........................ 129
`Implementation of the Mapping ADT ................................. 135
`Priority Queues ............................................................. 135
`Implementations of Priority Queues ................................... 138
`Some Complex Set Structures ........................................... 145
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 4
`
`

`

`x
`
`CONTENTS
`
`Chapter 5
`
`Advanced Set Representation Methods
`
`5.1
`5.2
`5.3
`5.4
`5.5
`5.6
`
`Binary Search Trees ....................................................... 155
`Time Analysis of Binary Search Tree Operations .................. 160
`Tries ........................................................................... 163
`Balanced Tree Implementations of Sets ............................... 169
`Sets with the MERGE and FIND Operations ....................... 180
`An ADT with MERGE and SPLIT .................................... 189
`
`Chapter 6
`
`Directed Graphs
`
`6.1
`6.2
`6.3
`6.4
`6.5
`6.6
`6.7
`
`Basic Definitions .................................................... , ...... 198
`Representations for Directed Graphs ..... , ... , ........................ 199
`The Single-Source Shortest Paths Problem ........................... 203
`The All-Pairs Shortest Path Problem .................................. 208
`Traversals of Directed Graphs .......................................... 215
`Directed Acyclic Graphs .................................................. 219
`Strong Components ........................................................ 222
`
`Chapter 7
`
`Undirected Graphs
`
`7.1
`7.2
`7.3
`7.4
`7.5
`Chapter 8
`
`8.1
`8.2
`8.3
`8.4
`8.5
`8.6
`8.7
`Chapter 9
`
`Definitions . . . . . . . . . . . . . ... , ................................................. 230
`Minimum-Cost Spanning Trees ......................................... 233
`Traversals .......................................................... , ......... 239
`Articulation Points and Biconnected Components .................. 244
`Graph Matching ......................................... , ................. , 246
`
`Sorting
`
`The Internal Sorting Model. ............. , ............................... 253
`Some Simple Sorting Schemes ................................ , .......... 254
`Quicksort ..................................................................... 260
`Heapsort ...................................................................... 271
`Bin Sorting ................................................................... 274
`A Lower Bound for Sorting by Comparisons ........................ 282
`Order Statistics ........ ,, .................................................... 286
`
`Algorithm Analysis Techniques
`
`9.1
`9.2
`9.3
`9.4
`
`Efficiency of Algorithms ................................................. 293
`Analysis of Recursive Programs ..... , .................................. 294
`Solving Recurrence Equations .......................................... 296
`A General Solution for a Large Class of Recurrences ...... , ..... 298
`
`Chapter 10
`IO.I
`10.2
`10.3
`10.4
`10.5
`
`Algorithm Design Techniques
`
`Divide-and-Conquer Algorithms ........................................ 306
`Dynamic Programming ................................................... 311
`Greedy Algorithms ........................................................ 321
`Backtracking ................................................................. 324
`Local Search Algorithms ................................................. 336
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 5
`
`

`

`xi
`
`CONTENTS
`
`Chapter 11
`
`11.1
`11.2
`11.3
`11.4
`
`Chapter 12
`12. l
`12.2
`12.3
`12.4
`12.5
`12.6
`
`Data Structures and Algorithms for External Storage
`
`A Model of External Computation ..................................... 347
`External Sorting ............................................................ 349
`Storing Information in Files ............................................. 361
`External Search Trees ..................................................... 368
`
`Memory Management
`The Issues in Memory Management ................................... 378
`Managing Equal-Sized Blocks ........................................... 382
`Garbage Collection Algorithms for Equal-Sized Blocks .......... 384
`Storage Allocation for Objects with Mixed Sizes ................... 392
`Buddy Systems ............................................................. .400
`Storage Compaction ...................................................... .404
`
`Bibliography ............................................................... .411
`
`Index ......................................................................... .419
`
`~~~----------------------·
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 6
`
`

`

`CHAPTER 3
`
`Trees
`
`A tree imposes a hierarchical structure on a collection of items. Familiar
`examples of trees are genealogies and organization charts. Trees are used to
`help analyze electrical circuits and to represent the structure of mathematical
`formulas. Trees also arise naturally in many different areas of computer sci(cid:173)
`ence. For example, trees are used to organize information in database sys(cid:173)
`tems and to represent the syntactic structure of source programs in compilers.
`Chapter 5 describes applications of trees in
`the representation of data.
`Throughout this book, we shall use many different variants of trees. In this
`chapter we introduce the basic definitions and present some of the more com(cid:173)
`mon tree operations. We then describe some of the more frequently used data
`structures for trees that can be used to support these operations efficiently.
`
`3.1 Basic Terminology
`
`A tree is a collection of elements called nodes, one of which is distinguished as
`a root, along with a relation ("parenthood") that places a hierarchical struc(cid:173)
`ture on the nodes. A node, like an element of a list, can be of whatever type
`we wish. We often depict a node as a letter, a string, or a number with a cir(cid:173)
`cle around it. Formally, a tree can be defined recursively in the following
`manner.
`I. A single node by itself is a tree. This node is also the root of the tree.
`Z. Suppose n
`trees with
`roots
`is a node and T1, T,, ... , T, are
`nti n2, . . • ,nk, respectively. We can construct a new tree by making n
`In this tree n is the root and
`be the parent of nodes n 1, n2, • . . , n,.
`T1, T2 , •.. , Tk are the subtrees of the root. Nodes n 1, n2, . . . ,nk are
`called the children of node n.
`Sometimes, it is convenient to include among trees the null tree, a "tree'' with
`no nodes, which we shall represent by A.
`
`Example 3.1. Consider the table of contents of a book, as suggested by Fig.
`3. l(a). This table of contents is a tree. We can redraw it in the manner
`shown in Fig. 3.l(b). The parent-child relationship is depicted by a line.
`Trees are normally drawn top-down as in Fig. 3.l(b), with the parent above
`the child.
`the node called "Book," has
`The root,
`roots
`three subtrees with
`to
`the chapters Cl, CZ, and C3. This relationship
`is
`corresponding
`represented by the lines downward from Book to Cl, CZ, and C3. Book is
`the parent of Cl, CZ, and C3, and these three nodes are the children of Book.
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 7
`
`

`

`76
`
`TREES
`
`Book
`CI
`sl.l
`s 1.2
`C2
`s2. l
`s2.l .I
`s2.J .2
`s2.2
`s2.3
`C3
`
`(a)
`
`Book
`
`C2
`
`C3
`
`Cl
`
`~·1~
`I \ I I\
`/ \
`
`sl.I
`
`sl.2
`
`s2.I Q.1
`
`s2.3
`
`Q.1.1
`
`s2.l.2
`
`(b)
`
`Fig. 3.1. A table of contents and its tree representation.
`
`The third subtree, with root C3, is a tree of a single node, while the other
`two subtrees have a nontrivial structure. For example, the subtree with root
`C2 has three subtrees, corresponding to the sections s2. J, s2.2, and s2.3; the
`last two are one-node trees, while the first has two subtrees corresponding to
`the subsections s2. l. l and s2. I. 2. o
`
`Example 3.1 is typical of one kind of data that is best represented as a
`tree. In this example, the parenthood relationship stands for containment; a
`parent node is comprised of its children, as Book is comprised of Cl, C2, and
`C3. Throughout this book we shall encounter a variety of other relationships
`that can be represented by parenthood in trees.
`If n1o n2, ••• , nk is a sequence of nodes in a tree such that n; is the
`parent of n1+ 1 for 1 ,;; i < k, then this sequence is called a path from node n 1
`to node nk. The length of a path is one less than the number of nodes in the
`path. Thus there is a path of length zero from every node to itself. For
`example, in Fig. 3.1 there is a path of length two, namely (C2, s2.J, s2.1.2)
`from C2 to s2. l.2.
`If there is a path from node a to node b, then a is an ancestor of b, and b
`is a descendant of a. For example, in Fig. 3.1, the ancestors of s2.l, are
`itself, C2, and Book, while its descendants are itself, s2.l.J, and s2.l.2.
`Notice that any node is both an ancestor and a descendant of itself.
`An ancestor or descendant of a node, other than the node itself, is called
`a proper ancestor or proper descendant, respectively. Jn a tree, the root is the
`only node with no proper ancestors. A node with no proper descendants is
`called a leaf. A subtree of a tree is a node, together with all its descendants.
`The height of a node in a tree is the length of a longest path from the
`node to a leaf. In Fig. 3.1 node Cl has height I, node C2 height 2, and node
`C3 height 0. The height of a tree is the height of the root. The depth of a
`node is the length of the unique path from the root to that node.
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 8
`
`

`

`3. l BASIC TERMINOLOGY
`
`The Order of Nodes
`
`77
`
`The children of a node are usually ordered from left-to-right. Thus the two
`trees of Fig, 3.2 are different because the two chilpren of node a appear in a
`different order in the two trees. If we wish explicitly to ignore the order of
`children, we shall refer to a tree as an unordered tree.
`
`c
`
`Fig. 3.2. Two distinct (ordered) trees.
`
`The "left-to-right" ordering of siblings (children of the same node) can be
`extended to compare any two nodes that are not related by the ancestor(cid:173)
`descendant relationship. The relevant rule is that if a and b are siblings, and
`a is to the left of b, then all the descendants of a are to the left of all the des(cid:173)
`cendants of b.
`Example 3.2. Consider the tree in Fig. 3.3. Node 8 is to the right of node 2,
`to the left of nodes 9, 6, 10, 4, and 7, and neither left nor right of its ances(cid:173)
`tors I, 3, and 5.
`
`1
`
`~\~
`/~I
`I \
`I
`
`4
`
`7
`
`6
`
`10
`
`2
`
`3
`
`5
`
`8
`
`9
`
`Fig. 3.3. A tree.
`
`A simple rule, given a node n, for finding those nodes to its left and those
`to its right, is to draw the path from the root to n. All nodes branching off to
`the left of this path, and all descendants of such nodes, are to the left of n.
`All nodes and descendants of nodes branching off to the right are to the right
`of n. o
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 9
`
`

`

`78
`
`TREES
`
`Preorder, Postorder, and lnorder
`
`There are several useful ways in which we can systematically order all nodes
`of a tree. The three most important orderings are called preorder, inorder
`and postorder; these orderings are defined recursively as follows.
`•
`If a tree T is null, then the empty list is the preorder, inorder and post(cid:173)
`order listing of T.
`If T consists a single node, then that node by itself is the preorder,
`in order, and postorder listing of T.
`Otherwise, let T be a tree with root n and subtrees T" T2, ... , Tb as sug(cid:173)
`gested in Fig. 3.4.
`
`•
`
`...
`
`Fig. 3.4. Tree T,
`
`L The preorder listing (or preorder traversal) of the nodes of T is the root n
`of T followed by the nodes of T1 in preorder, then the nodes of T2 in
`preorder, and so on, up to the nodes of Tk in preorder.
`2. The inorder listing of the nodes of T is the nodes of T 1 in inorder, fol(cid:173)
`lowed by node n, followed by the nodes of T 2, ••• , Tb each group of
`nodes in inorder.
`•
`3. The postorder listing of the nodes of T is the nodes of T 1 in postorder,
`then the nodes of T2 in postorder, and so on, up to Tb all followed by
`node n.
`Figure 3.5(a) shows a sketch of a procedure to list the nodes of a tree in
`preorder. To make it a postorder procedure, we simply reverse the order of
`steps (I) and (2). Figure 3.5(b) is a sketch of an inorder procedure. In each
`case, we produce the desired ordering of the tree by calling the appropriate
`procedure on the root of the tree.
`
`Example 3.3. Let us list the tree of Fig. 3.3 in preorder. We first list I and
`then call PREORDER on the first subtree of I, the subtree with root 2. This
`subtree is a single node, so we simply list it. Then we proceed to the second
`subtree of I, the tree rooted at 3. We list 3, and then call PREORDER on
`the first subtree of 3. That call results in listing 5, 8, and 9, in that order.
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 10
`
`

`

`3.1 BASIC TERMINOLOGY
`
`79
`
`(I)
`(2)
`
`procedure PREORDER ( n: node);
`begin
`list n;
`for each child c of n, if any, in order from the left do
`PREORDER(c)
`end; { PREORDER }
`
`(a) PREORDER procedure.
`
`procedure !NORDER ( n: node);
`begin
`if n is a leaf then
`list n
`else begin
`INORDER(leftmost child of n);
`list n;
`for each child c of n, except for the leftmost,
`in order from the left do
`INORDER(c)
`
`end
`end; { !NORDER }
`
`(b) !NORDER procedure.
`
`Fig. 3.5. Recursive ordering procedures.
`
`Continuing in this manner, we obtain the complete preorder traversal of Fig.
`3.3: I, 2, 3, 5, 8, 9, 6, IO, 4, 7.
`Similarly, by simulating Fig. 3.5(a) with the steps reversed, we can dis(cid:173)
`cover that the postorder of Fig. 3.3 is 2, 8, 9, 5, IO, 6, 3, 7, 4, I. By simulat(cid:173)
`ing Fig. 3.5(b), we find that the inorder listing of Fig. 3.3 is 2, I, 8, 5, 9, 3,
`JO, 6, 7, 4. D
`
`A useful trick for producing the three node orderings is the following.
`Imagine we walk around the outside of the tree, starting at the root, moving
`counterclockwise, and staying as close to the tree as possible; the path we have
`in mind for Fig. 3.3 is shown in Fig. 3.6.
`For preorder, we list a node the first time we pass it. For postorder, we
`list a node the last time we pass it, as we move up to its parent. For inorder,
`we list a leaf the first time we pass it, but list an interior node the second time
`we pass it. For example, node I in Fig. 3.6 is passed the first time at the
`beginning, and the second time while passing through the "bay" between
`nodes 2 and 3. Note that the order of the leaves in the three orderings is
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 11
`
`

`

`80
`
`TREES
`
`Fig. 3.6. Traversal of a tree.
`
`always the same left-to-right ordering of the leaves. It is only the ordering of
`the interior nodes and their relationship to the leaves that vary among the
`three. D
`
`Labeled Trees and Expression Trees
`
`Often it is useful to associate a label, or value, with each node of a tree, in
`the same spirit with which we associated a value with a list element in the pre(cid:173)
`vious chapter. That is, the label of a node is n11t the name of the node, but a
`value that is "stored" at the node. In some applications we shall even change
`the label of a node, while the name of a node remains the same. A useful
`analogy is tree:list = label:element = node:position.
`Example 3.4. Figure 3. 7 shows a labeled tree representing the arithmetic
`expression (a+b) * (a+c), where n 1, ••• , n7 are the names of the nodes,
`and the labels, by convention, are shown next to the nodes. The rules
`whereby a labeled tree represents an expression are as follows:
`I. Every leaf is labeled by an operand and consists of that operand alone.
`For example, node n4 represents the expression a.
`2. Every interior node n is labeled by an operator. Suppose n is labeled by a
`binary operator 6, such as + or •, and that the left child represents
`expression E 1 and the right child E2• Then n represents expression
`(E 1) 6 (E2). We may remove the parentheses if they are not necessary.
`For example, node n2 has operator +, and its left and right children
`represent the expressions a and b, respectively. Therefore, n2 represents
`(a)+(b), or just a+b. Node n1 represents (a+b)•(a+c), since• is the label
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 12
`
`

`

`3.1 BASIC TERMINOLOGY
`
`81
`
`at n1o and a+b and a+c are the expressions represented by n2 and n3, respec(cid:173)
`tively. o
`
`Fig. 3.7. Expression tree with labels.
`
`Often, when we produce the preorder, inorder, or postorder listing of a
`tree, we prefer to list not the node names, but rather the labels. In the case
`of an expression tree, the preorder listing of the labels gives us what is known
`as the prefix form of an expression, where the operator precedes its left
`operand and its right operand. To be precise, the prefix expression for a sin(cid:173)
`gle operand a is a itself. The prefix expression for (E 1) 0 (E2), with 0 a
`binary operator, is 0P 1P,, where P 1 and P2 are the prefix expressions for E 1
`and E2• Note that no parentheses are necessary in the prefix expression, since
`we can scan the prefix expression 0P 1P2 and uniquely identify P 1 as the shor(cid:173)
`test (and only) prefix of P 1P 2 that is a legal prefix expression.
`For example, the preorder listing of the labels of Fig. 3.7 is •+ab+ac.
`The prefix expression for n2 , which is +ab, is the shortest legal prefix of
`+ab+ac.
`Similarly, a postorder listing of the labels of an expression tree gives us
`what is known as the postfix (or Polish) representation of an expression. The
`expression (E 1) 0 (£2) is represented by the postfix expression P 1P 26, where
`P 1 and P 2 are the postfix representations of E 1 and E 2, respectively. Again,
`no parentheses are necessary in the postfix representation, as we can deduce
`what P2 is by looking for the shortest suffix of P 1P2 that is a legal postfix
`expression. For example, the postfix expression for Fig. 3.7 is ab+ac+*. If
`we write this expression as P 1P2•, then P2 is ac+, the shortest suffix of
`ab+ac+ that is a legal postfix expression.
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 13
`
`

`

`82
`
`TREES
`
`The inorder traversal of an expression tree gives the infix expression
`itself, but with no parentheses. For example, the inorder listing of the labels
`of Fig. 3. 7 is a+ b • a+ c. The reader is invited to provide an algorithm for
`traversing an expression tree and producing an infix expression with all
`needed pairs of parentheses.
`
`Computing Ancestral Information
`
`The preorder and postorder traversals of a tree are useful in obtaining ances(cid:173)
`tral information. Suppose postorder(n) is the position of node n in a post(cid:173)
`order listing of the nodes of a tree. Suppose desc(n) is the number of proper
`descendants of node n. For example, in the tree of Fig. 3.7 the postorder
`numbers of nodes n2 , n4 , and n5 are 3, 1, and 2, respectively.
`The postorder numbers assigned to the nodes have the useful property
`that the nodes in the subtree with root n are numbered consecutively from
`postorder(n) - desc(n) to postorder(n). To test if a vertex x is a descendant
`of vertex y, all we need do is determine whether
`
`postorder(y)-desc(y) ,,;; postorder(x) ,,;; postorder(y).
`
`A similar property holds for preorder.
`
`3.2 The ADT TREE
`
`In Chapter 2, lists, stacks, queues, and mappings were treated as abstract data
`types (ADT's). In this chapter trees will be treated both as ADT's and as
`data structures. One of our most important uses of trees occurs in the design
`of implementations for the various ADT's we study. For example, in Section
`5.1, we shall see how a "binary search tree" can be used to implement
`abstract data types based on the mathematical model of a set, together with
`operations such as INSERT, DELETE, and MEMBER (to test whether an
`element is in a set). The next two chapters present a number of other tree
`implementations of various ADT's.
`In this section, we shall present several useful operations on trees and
`show how tree algorithms can be designed in terms of these operations. As
`with lists, there are a great variety of operations that can be performed on
`trees. Here, we shall consider the following operations:
`I. PARENT(n, T). This function returns the parent of node n in tree T. If
`n is the root, which has no parent, A is returned. In this context, A is a
`"null node," which is used as a signal that we have navigated off the tree.
`2. LEFTMOST _CHILD(n, T) returns the leftmost child of node n in tree T,
`and it returns A if n is a leaf, which therefore has no children.
`3. RIGHT_SIBLING(n, T) returns the right sibling of node n in tree T,
`defined to be that node m with the same parent p as n such that m lies
`immediately to the right of n in the ordering of the children of p. For
`in Fig. 3.7, LEFTMOST_CHILD(n 2) = n4;
`example,
`for
`the
`tree
`RIGHT_SIBLING(n4) = n5, and RIGHT_SIBLING (ns) = A.
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 14
`
`

`

`3.2 THE ADT TREE
`
`83
`
`4. LABEL(n, T) returns the label of node n in tree T. We do not, however,
`require labels to be defined for every tree.
`5. CREATEi(v, T 1• Tz, ... , T;) is one of an infinite family of functions,
`one for each value of i = 0, I, 2, . . .. CREATEi makes a new node r
`label v and gives it i children, which are the roots of trees
`with
`Ti. T2 , . . . , T;, in order from the left. The tree with root r is returned.
`Note that if i = 0, then r is both a leaf and the root.
`6. ROOT(T) returns the node that is the root of tree T, or A if T is the null
`tree.
`7. MAKENULL(T) makes T be the null tree.
`
`Example 3.5. Let us write both recursive and nonrecursive procedures to take
`a tree and list the labels of its nodes in preorder. We assume that there are
`data types node and TREE already defined for us, and that the data type
`TREE is for trees with labels of the type labeltype. Figure 3.8 shows a recur(cid:173)
`sive procedure that, given node n, lists the labels of the subtree rooted at n in
`preorder. We call PREORDER(ROOT(T)) to get a preorder listing of tree T.
`
`p·rocedure PREORDER ( n: node );
`{ list the labels of the descendants of n in preorder }
`var
`
`c: node;
`begin
`print(LABEL(n, T));
`c := LEFTMOST_CHILD(n, T);
`while c < > A do begin
`PREORDER(c);
`c := RIGHT_SIBLING(c, T)
`
`end
`end; { PREORDER }
`
`Fig. 3.8. A recursive preorder listing procedure.
`
`We shall also develop a nonrecursive procedure to print a tree in
`preorder. To find our way around the tree, we shall use a stack S, whose
`type STACK is really "stack of nodes." The basic idea underlying our algo(cid:173)
`rithm is that when we are at a node n, the stack will hold the path from the
`root to n, with the root at the bottom of the stack and node n at the top. t
`
`t Recall our discussion of recursion in Section 2.6 in which we illustrated how the implementation
`If we examine Fig. 3.8, we can
`of a recursive procedure involves a stack of activation records.
`observe that when PREORDER(n) is called, lhe active procedure calls, and therefore the stack of
`activation records, correspond to the calls of PREORDER for all the ancestors of n. Thus our
`nonrecursive preorder procedure, like the ex.ample in Section 2.6, models closely the way the re~
`cursive procedure is implemented.
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 15
`
`

`

`84
`
`TREES
`
`One way to perform a nonrecursive preorder traversal of a tree is given
`by the program NPREORDER shown in Fig. 3.9. This program has two
`modes of operation. In the first mode it descends down the leftmost unex(cid:173)
`plored path in the tree; printing and stacking the nodes along the path, until it
`reaches a leaf.
`The program then enters the second mode of operation in which it retreats
`back up the stacked path, popping the nodes of the path off the stack, until it
`encounters a node on the path with a right sibling. The program then reverts
`back to the first mode of operation, starting the descent from that unexplored
`right sibling.
`The program begins in mode one at the root and terminates when the
`stack becomes empty. The complete program is shown in Fig. 3.9.
`
`3.3 Implementations of Trees
`
`In this section we shall present several basic implementations for trees and dis(cid:173)
`cuss their capabilities for supporting the various tree operations introduced in
`Section 3.2.
`
`An Array Representation of Trees
`
`Let T be a tree in which the nodes are named J, 2, ... , n. Perhaps the sim(cid:173)
`plest representation of T that supports the PARENT operation is a linear
`array A in which entry A [i] is a pointer or a cursor to the parent of node i.
`The root of T can be distinguished by giving it a null pointer or a pointer to
`itself as parent. In Pascal, pointers to array elements are not feasible, so we
`shall have to use a cursor scheme where A[i] = j if node j is the parent of
`node i, and A[i] = 0 if node i is the root.
`This representation uses the property of trees that each node has a unique
`parent. With this representation the parent of a node can be found in con(cid:173)
`stant time. A path going up the tree, that is, from node to parent to parent,
`and so on, can be traversed in time proportional to the number of nodes on
`the path. We can also support the LABEL operator by adding another array
`L, such that L(i] is the label of node i, or by making the elements of array A
`be records consisting of an integer (cursor) and a label.
`
`Example 3.6. The tree of Fig. 3. lO(a) has the parent representation given by
`the array A shown in Fig. 3. IO(b). o
`
`The parent pointer representation does not facilitate operations that
`require child-of information. Given a node n, it is expensive to determine the
`children of n, or the height of n.
`In addition, the parent pointer representa(cid:173)
`tion does not specify the order of the children of a node. Thus, operations
`like LEFTMOST_CHILD and RIGHT_SIBLING are not well defined. We
`could impose an artificial order, for example, by numbering the children of
`each node after numbering the parent, and numbering the children in
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 16
`
`

`

`3.3 IMPLEMENT A T!ONS OF TREES
`
`85
`
`procedure NPREORDER ( T: TREE );
`{ nonrecursive preorder traversal of tree T }
`
`var
`
`m: node; { a temporary }
`S: STACK; { stack of nodes holding path from the root
`to the parent TOP(S) of the "current" node m }
`
`begin
`{ initialize }
`MAKENULL(S);
`m := ROOT(T);
`
`while true do
`if m < > A then begin
`print(LABEL(m, T));
`PUSH(m, S);
`{ explore leftmost child of m }
`m := LEFTMOST_CHILD(m, T)
`end
`else begin
`{ exploration of path on stack
`is now complete }
`if EMPTY(S) then
`return;
`{ explore right sibling of node
`on top of stack }
`m := RIGHT_SIBLING(TOP(S), T);
`POP(S)
`end
`end; { NPREORDER }
`
`Fig. 3.9. A nonrecursive preorder procedure.
`
`increasing order from left to right. On that assumption, we have written the
`function RIGHT_SIBLING in Fig. 3.11, for types node and TREE that are
`defined as follows:
`
`type
`node = integer;
`TREE = array [l..maxnodes] of node;
`
`For this implementation we assume the null node A is represented by 0.
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 17
`
`

`

`86
`
`TREES
`
`(a} a tree
`
`10
`9
`8
`7
`6
`5
`4
`3
`2
`1
`AO I 2 2 5 5 5 3 3
`
`(b) parent representation.
`
`Fig. 3.10. A tree and its parent pointer representation.
`
`function RIGHT_SIBLING ( n: node; T: TREE) : node;
`{ return the right sibling of node n in tree T }
`•var
`
`i, parent: node;
`begin
`parent:= T[n];
`for i : = n + 1 to maxnodes do
`{ search for node after n with same parent }
`if T[i) = parent then
`return (i);
`return (0) { null node will be returned
`if no right sibling is ever found }
`end; { RIGHT_SIBLING}
`
`Fig. 3.11. Right sibling operation using array representation.
`
`IPR2019-01304
`BloomReach, Inc. EX1010 Page 18
`
`

`

`3.3 IMPLEMENTATIONS OF TREES
`
`87
`
`Representation of Trees by Lists of Children
`
`An important and useful way of representing trees is to form for each node a
`list of its children. The lists can be represented by any of the methods sug(cid:173)
`gested in Chapter 2, but because the number of children each node may have
`can be variable, the linked-list representations are often more appropriate.
`Figure 3.12 suggests how the tree of Fig. 3.lO(a) might be represented.
`There is an array of header cells, indexed by nodes, which we assume to be
`numbered l, 2, ... , 10. Each header points to a linked list of "elements,"
`which are nodes. The elements on the list headed by header[i] are the chil(cid:173)
`dren of node i; for example, 9 and 10 are the children of 3.
`
`4
`
`6
`
`7
`
`=r 1 3 ] . l
`-•
`_r 2 J
`-l -
`_r 1 . l
`J
`I
`::r
`-l 5
`-r JO ] . l
`9 J
`- t
`I
`::r
`1.
`t 1 8 1 • J
`J J 1 1
`
`2
`3
`4
`5
`6
`7
`8
`9
`JO
`
`• •
`•
`•
`•
`•
`
`header
`
`Fig. 3.12. A linked-list representation of a tree.
`
`Let u

This document is available on Docket Alarm but you must sign up to view it.


Or .

Accessing this document will incur an additional charge of $.

After purchase, you can access this document again without charge.

Accept $ Charge
throbber

Still Working On It

This document is taking longer than usual to download. This can happen if we need to contact the court directly to obtain the document and their servers are running slowly.

Give it another minute or two to complete, and then try the refresh button.

throbber

A few More Minutes ... Still Working

It can take up to 5 minutes for us to download a document if the court servers are running slowly.

Thank you for your continued patience.

This document could not be displayed.

We could not find this document within its docket. Please go back to the docket page and check the link. If that does not work, go back to the docket and refresh it to pull the newest information.

Your account does not support viewing this document.

You need a Paid Account to view this document. Click here to change your account type.

Your account does not support viewing this document.

Set your membership status to view this document.

With a Docket Alarm membership, you'll get a whole lot more, including:

  • Up-to-date information for this case.
  • Email alerts whenever there is an update.
  • Full text search for other cases.
  • Get email alerts whenever a new case matches your search.

Become a Member

One Moment Please

The filing “” is large (MB) and is being downloaded.

Please refresh this page in a few minutes to see if the filing has been downloaded. The filing will also be emailed to you when the download completes.

Your document is on its way!

If you do not receive the document in five minutes, contact support at support@docketalarm.com.

Sealed Document

We are unable to display this document, it may be under a court ordered seal.

If you have proper credentials to access the file, you may proceed directly to the court's system using your government issued username and password.


Access Government Site

We are redirecting you
to a mobile optimized page.





Document Unreadable or Corrupt

Refresh this Document
Go to the Docket

We are unable to display this document.

Refresh this Document
Go to the Docket