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.

Slides:



Advertisements
Similar presentations
Binary Searching.
Advertisements

Section 7.4: Closures of Relations Let R be a relation on a set A. We have talked about 6 properties that a relation on a set may or may not possess: reflexive,
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
APPLICATIONS OF DIFFERENTIATION 4. In Sections 2.2 and 2.4, we investigated infinite limits and vertical asymptotes.  There, we let x approach a number.
Graphs, relations and matrices
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Induction and recursion
Binary Trees Chapter 6.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Induction and recursion
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
LIMITS AND DERIVATIVES 2. In Sections 2.2 and 2.4, we investigated infinite limits and vertical asymptotes.  There, we let x approach a number.  The.
APPLICATIONS OF DIFFERENTIATION 4. A polynomial behaves near infinity as its term of highest degree. The polynomial behaves like the polynomial Near infinity.
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Week 11 - Wednesday.  What did we talk about last time?  Graphs  Euler paths and tours.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Copyright © Cengage Learning. All rights reserved. CHAPTER 10 GRAPHS AND TREES.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Tries.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Module #19: Graph Theory: part II Rosen 5 th ed., chs. 8-9.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
4.3 Recursive Definitions and Structural Induction Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object.
File Organization and Processing Week Tree Tree.
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
Copyright © Cengage Learning. All rights reserved. 6 Inverse Functions.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Binary Tree 10/22/081. Tree A nonlinear data structure Contain a distinguished node R, called the root of tree and a set of subtrees. Two nodes n1 and.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Copyright © Cengage Learning. All rights reserved. Fundamental Concepts of Algebra 1.1 Real Numbers.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
Copyright © Cengage Learning. All rights reserved. CHAPTER 8 RELATIONS.
Bushy Binary Search Tree from Ordered List. Behavior of the Algorithm Binary Search Tree Recall that tree_search is based closely on binary search. If.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Algorithms for hard problems Parameterized complexity Bounded tree width approaches Juris Viksna, 2015.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
1 CMSC 341 Introduction to Trees Textbook sections:
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
Copyright © Cengage Learning. All rights reserved.
CMSC 341 Introduction to Trees.
Multi-Way Search Trees
Data Structures and Algorithm Analysis Trees
Presentation transcript:

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 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.

Various kinds of trees

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.

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

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!!!

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_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.

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.

Rotated form of linked implementation Linked 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 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.

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 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.

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, 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 )

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

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.

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.”

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