MultiwayTrees
Multiway Trees
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
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.
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.
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.
Rooted tree, ordered tree, forest, orchard • A rooted tree is a tree in which one vertex, called
the root, 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 forest) is an ordered set of ordered trees.
Various kinds of trees
Implementations of Ordered Treesmultiple 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.
Implementations of Ordered Trees• But this will result in a great many of the link
members being NULL.
• 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
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/nkwhich 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!!!
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.
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.
Linked implementation of an ordered tree
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_sibling.
• 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.
rotating it a few degrees clockwiseIf 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_child) links point leftward and the – horizontal (next_sibling) links point downward and to the
right.
Rotated form of linked implementationLinked Ordered Tree - Binary Tree
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 NULL, the root of the corresponding binary tree will always have an empty right subtree.
• So, we must consider another class of data structure.
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.
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.
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.
build a rooted or an ordered tree
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.
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.
recursive definitions
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.
Recursive construction of ordered trees and orchards
The Formal CorrespondenceTheorem:
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.
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]
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() =
Orchards, and Binary Trees• If the orchard O is not empty, then it is denoted by
the ordered pair
O = (T , O2)
where T is an ordered tree and O2 another orchard.
• The ordered tree T is denoted as the pair
T = { v, O1 }
where v is a vertex and O1 is another orchard.
We substitute this expression for T in the first expression, obtaining
O = ({v, O1 }, O2)
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 O1 and O2 are smaller than O, so the binary trees f(O1 ) and f(O2) 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, O1 }, O2) = [v, f (O1 ), f (O2)]
orchard binary tree
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, O1 , and O2 on the left side, there is exactly one way to fill in the same symbols on the right, and vice versa.
RotationsWe 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 (O1 ), f (O2)] the left link from v goes to the root of the binary tree f(O1), which in fact was the first child of v in the ordered tree {v, O1 }
• 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.”
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.
Conversion from orchard to binary tree
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.
Orchards, Trees, and Binary Trees