Download presentation
Presentation is loading. Please wait.
Published byPeter Sutton Modified over 9 years ago
1
Multiway Trees
2
Trees with possibly more than two branches at each node are know as Multiway trees. 1. Orchards, Trees, and Binary Trees 2. Lexicographic Search Trees: Tries 3. External Searching: B-Trees 4. Red-Black Trees
3
Orchards, Trees, and Binary Trees Binary trees, as we have seen, are a powerful and elegant form of data structure. Even so, the restriction to no more than two children at each node is severe, and there are many possible applications for trees as data structures where the number of children of a node can be arbitrary. This section elucidates a pleasant and helpful surprise: Binary trees provide a convenient way to represent what first appears to be a far broader class of trees.
4
Mathematical definition of tree In mathematics, the term tree has a quite broad meaning: A (free) tree is any set of points (called vertices) and any set of pairs of distinct vertices (called edges or branches) such that (1) there is a sequence of edges (a path) from any vertex to any other, and (2) there are no circuits, that is, no paths starting from a vertex and returning to the same vertex.
5
free tree In computer applications we usually do not need to study trees in such generality, and when we do, for emphasis we call them free trees. Our trees are almost always tied down by having one particular vertex singled out as the root, and for emphasis we call such a tree a rooted tree.
6
Rooted tree, ordered tree, forest, orchard A rooted tree is a tree in which one vertex, called the roo t, is distinguished. An ordered tree is a rooted tree in which the children of each vertex are assigned an order. A forest is a set of trees. We usually assume that all trees in a forest are rooted. An orchard (also called an ordered fores t) is an ordered set of ordered trees.
7
Various kinds of trees
8
Implementations of Ordered Trees multiple links If we wish to use an ordered tree as a data structure, the obvious way to implement it in computer memory would be to extend the standard way to implement a binary tree, keeping as many link members in each node as there may be subtrees, in place of the two links needed for binary trees. Thus in a tree where some nodes have as many as ten subtrees, we would keep ten link members in each node.
9
Implementations of Ordered Trees But this will result in a great many of the link members being NUL L. In fact, we can easily determine exactly how many. If the tree has n nodes and each node has k link members, then there are n x k links altogether. There is exactly one link that points to each of the n - 1 nodes other than the root, so the proportion of NULL links must be
10
wasted space - n nodes, up to k link members = n X k links total - the root is not pointed to by such a link, but the other n-1 are - so of nk links, only n-1 are not NULL... - so number of wasted links are nk – (n-1) - ratio of wasted links to total links nk – (n-1)=nk – n + 1 nk nk =nk – n + 1 nk nk =1 - 1/k + 1/nk which is clearly bigger than 1 – 1/k - for space for one link say, we have: > 1-1/1 or > 0 i.e. more than 0% wasted space!!! - for space for ten links say, we have: > 1-1/10 or > 0.9 i.e. more than 90% wasted space!!!
11
Implementations of Ordered Trees Hence if a vertex might have ten subtrees, then more than ninety percent of the links will be NULL. Clearly this method of representing ordered trees is very wasteful of space. The reason is that, for each node, we are maintaining a contiguous list of links to all its children, and these contiguous lists reserve much unused space. We now investigate a way that replaces these contiguous lists with linked lists and leads to an elegant connection with binary trees.
12
Linked Implementation To keep the children of each node in a linked list, we shall need two kinds of links. First comes the header for a family of children; this will be a link from a parent node to its leftmost child, which we may call first_child. Second, each node except the root will appear in one of these lists, and hence requires a link to the next node on the list, that is, to the next child of the parent. We may call this second link next_sibling.
13
Linked implementation of an ordered tree
14
each node of the ordered tree For each node of the ordered tree we have defined two links (that will be NULL if not otherwise defined), first_child and next_siblin g. By using these two links we now have the structure of a binary tree; that is, the linked implementation of an ordered tree is a linked binary tree.
15
rotating it a few degrees clockwise If we wish, we can even form a better picture of a binary tree by taking the linked representation of the ordered tree and rotating it a few degrees clockwise, so that –downward (first_chil d) links point leftward and the –horizontal (next_siblin g) links point downward and to the right.
16
Rotated form of linked implementation Linked Ordered Tree-Binary Tree
17
Not every binary tree is obtained from a rooted tree Not every binary tree is obtained from a rooted tree by the foregoing process: Since the next_sibling link of the root is always NUL L, the root of the corresponding binary tree will always have an empty right subtree. So, we must consider another class of data structure.
18
Orchards In our work so far with binary trees we have profited from using recursion, and for other classes of trees we shall continue to do so. Employing recursion means reducing a problem to a smaller one. Hence we should see what happens if we take a rooted tree or an ordered tree and strip off the root. What is then left is (if not empty) a set of rooted trees or an ordered set of ordered trees, respectively.
19
forest The standard term for an arbitrary set of trees is forest, but when we use this term, we generally assume that the trees are rooted. orchard The phrase ordered forest is sometimes used for an ordered set of ordered trees, but we shall adopt the equally descriptive (and more colorful) term orchard for this class.
20
build a rooted or an ordered tree Note that not only can we obtain a forest or an orchard by removing the root from a rooted tree or an ordered tree, respectively, but we can build a rooted or an ordered tree by starting with a forest or an orchard, attaching a new vertex at the top, and adding branches from the new vertex (which will be the root) to the roots of all trees in the forest or the orchard. These actions are illustrated in Figure 11.4.
21
build a rooted or an ordered tree
22
recursive definitions We shall use this process to give a new, recursive definition of ordered trees and orchards, one that yields a formal proof of the connection with binary trees. First, let us consider how to start. Recall that it is possible that a binary tree be empty; that is, it may have no vertices. It is also possible that a forest or an orchard be empty; that is, that it contain no trees. It is, however, not possible that a rooted or an ordered tree be empty, since it is guaranteed to contain a root, at the minimum. If we wish to start building trees and forests, we can note that the tree with only one vertex is obtained by attaching a new root to an empty forest.
23
recursive definitions Once we have this tree, we can make a forest consisting of as many one-vertex trees as we wish. Then we can attach a new root to build all rooted trees of height 1. In this way we can continue to construct all the rooted trees in turn in accordance with the following mutually recursive definitions.
24
recursive definitions
25
ordering of trees Notice how the ordering of trees is implicit in the definition of orchard. A nonempty orchard contains a first tree, and the remaining trees form another orchard, which again has a first tree that is the second tree of the original orchard. Continuing to examine the remaining orchard yields the third tree, and so on, until the remaining orchard is the empty one.
26
Recursive construction of ordered trees and orchards
27
The Formal Correspondence Theorem: Let S be any finite set of vertices. There is a one-to- one correspondence f from the set of orchards whose set of vertices is S to the set of binary trees whose set of vertices is S.
28
Proof Let us use the notation introduced in the definitions to prove the theorem. First, we need a similar notation for binary trees: A binary tree B is either the empty set ; or consists of a root vertex v with two binary trees B1 and B2. We may thus denote a nonempty binary tree with the ordered triple B = [v, B1, B2]
29
Proof We shall prove the theorem by mathematical induction on the number of vertices in S. The first case to consider is the empty orchard , which will correspond to the empty binary tree: f( ) =
30
Orchards, and Binary Trees If the orchard O is not empty, then it is denoted by the ordered pair O = (T, O 2 ) where T is an ordered tree and O 2 another orchard. The ordered tree T is denoted as the pair T = { v, O 1 } where v is a vertex and O 1 is another orchard. We substitute this expression for T in the first expression, obtaining O = ({v, O 1 }, O 2 )
31
Orchards, and Binary Trees By the induction hypothesis, f provides a one-to-one correspondence from orchards with fewer vertices than in S to binary trees, and O 1 and O 2 are smaller than O, so the binary trees f(O 1 ) and f(O 2 ) are determined by the induction hypothesis. We define the correspondence f from the orchard to a binary tree by binary tree corresponds to the orchard is f (O), f (O) = f ({v, O 1 }, O 2 ) = [v, f (O 1 ), f (O 2 )] orchard binary tree
32
Orchards, and Binary Trees It is now obvious that the function f is a one-to-one correspondence between orchards and binary trees with the same vertices. For any way to fill in the symbols v, O 1, and O 2 on the left side, there is exactly one way to fill in the same symbols on the right, and vice versa.
33
Rotations We can also use this notational form of the correspondence to help us form the picture of the transformation from orchard to binary tree. In the binary tree [v, f (O 1 ), f (O 2 )] the left link from v goes to the root of the binary tree f(O 1 ), which in fact was the first child of v in the ordered tree {v, O 1 } The right link from v goes to the vertex that was formerly the root of the next ordered tree to the right. That is, “left link” in the binary tree corresponds to “first child” in an ordered tree, and “right link” corresponds to “next sibling.”
34
Orchards, Trees, and Binary Trees In geometrical terms, the transformation reduces to the following rules: 1. Draw the orchard so that the first child of each vertex is immediately below the vertex, rather than centering the children below the vertex. 2. Draw a vertical link from each vertex to its first child, and draw a horizontal link from each vertex to its next sibling. 3. Remove the remaining original links. 4. Rotate the diagram 45 degrees clockwise, so that the vertical links appear as left links and the horizontal links as right links.
35
Conversion from orchard to binary tree
36
Summary We have seen three ways to describe the correspondence between orchards and binary trees: first_child and next_sibling links, rotations of diagrams, formal notational equivalence. Most people find the second way, rotation of diagrams, the easiest to remember and to picture. It is the first way, setting up links to give the correspondence, that is usually needed in actually writing computer programs. The third way, the formal correspondence, finally, is the one that proves most useful in constructing proofs of various properties of binary trees and orchards.
37
Orchards, Trees, and Binary Trees
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.