Stack permutations and an order relation for binary trees

Similar documents
RESTRICTED PERMUTATIONS AND POLYGONS. Ghassan Firro and Toufik Mansour Department of Mathematics, University of Haifa, Haifa, Israel

Balanced Trees. Balanced Trees Tree. 2-3 Tree. 2 Node. Binary search trees are not guaranteed to be balanced given random inserts and deletes

Enumeration of Two Particular Sets of Minimal Permutations

Permutations P-seuences n = 4 n = 5 n = 4 n =

MA/CSSE 473 Day 13. Student Questions. Permutation Generation. HW 6 due Monday, HW 7 next Thursday, Tuesday s exam. Permutation generation

Pattern Avoidance in Unimodal and V-unimodal Permutations

Restricted Permutations Related to Fibonacci Numbers and k-generalized Fibonacci Numbers

CSS 343 Data Structures, Algorithms, and Discrete Math II. Balanced Search Trees. Yusuf Pisan

THE ENUMERATION OF PERMUTATIONS SORTABLE BY POP STACKS IN PARALLEL

A Note on Downup Permutations and Increasing Trees DAVID CALLAN. Department of Statistics. Medical Science Center University Ave

Pin-Permutations and Structure in Permutation Classes

The Theory Behind the z/architecture Sort Assist Instructions

MAS336 Computational Problem Solving. Problem 3: Eight Queens

#A13 INTEGERS 15 (2015) THE LOCATION OF THE FIRST ASCENT IN A 123-AVOIDING PERMUTATION

Harmonic numbers, Catalan s triangle and mesh patterns

Department of Electrical Engineering, University of Leuven, Kasteelpark Arenberg 10, 3001 Leuven-Heverlee, Belgium

CSE101: Algorithm Design and Analysis. Ragesh Jaiswal, CSE, UCSD

ON SOME PROPERTIES OF PERMUTATION TABLEAUX

CSL 356: Analysis and Design of Algorithms. Ragesh Jaiswal CSE, IIT Delhi

PUZZLES ON GRAPHS: THE TOWERS OF HANOI, THE SPIN-OUT PUZZLE, AND THE COMBINATION PUZZLE

5.4 Imperfect, Real-Time Decisions

Non-overlapping permutation patterns

Discrete Mathematics and Probability Theory Spring 2014 Anant Sahai Note 11

MA/CSSE 473 Day 14. Permutations wrap-up. Subset generation. (Horner s method) Permutations wrap up Generating subsets of a set

ON THE PERMUTATIONAL POWER OF TOKEN PASSING NETWORKS.

Graphs of Tilings. Patrick Callahan, University of California Office of the President, Oakland, CA

Quarter Turn Baxter Permutations

Chapter 7: Sorting 7.1. Original

Some algorithmic and combinatorial problems on permutation classes

Equivalence classes of length-changing replacements of size-3 patterns

COMP 2804 solutions Assignment 4

An Optimal Algorithm for a Strategy Game

DVA325 Formal Languages, Automata and Models of Computation (FABER)

An O(1) Time Algorithm for Generating Multiset Permutations

Module 3 Greedy Strategy

What Does the Future Hold for Restricted Patterns? 1

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Dyck paths, standard Young tableaux, and pattern avoiding permutations

IEEE TRANSACTIONS ON INFORMATION THEORY, VOL. 55, NO. 6, JUNE

17. Symmetries. Thus, the example above corresponds to the matrix: We shall now look at how permutations relate to trees.

Enumerative Combinatoric Algorithms. Gray code

5.4 Imperfect, Real-Time Decisions

Algorithms. Abstract. We describe a simple construction of a family of permutations with a certain pseudo-random

Permutations with short monotone subsequences

A Memory Efficient Anti-Collision Protocol to Identify Memoryless RFID Tags

Asymptotic behaviour of permutations avoiding generalized patterns

Module 3 Greedy Strategy

A low cost soft mapper for turbo equalization with high order modulation

EXPLAINING THE SHAPE OF RSK

Game Theory and Algorithms Lecture 19: Nim & Impartial Combinatorial Games

Fast Sorting and Pattern-Avoiding Permutations

mywbut.com Two agent games : alpha beta pruning

10/5/2015. Constraint Satisfaction Problems. Example: Cryptarithmetic. Example: Map-coloring. Example: Map-coloring. Constraint Satisfaction Problems

Lecture 7: The Principle of Deferred Decisions

NON-OVERLAPPING PERMUTATION PATTERNS. To Doron Zeilberger, for his Sixtieth Birthday

Week 1. 1 What Is Combinatorics?

Past questions from the last 6 years of exams for programming 101 with answers.

Some constructions of mutually orthogonal latin squares and superimposed codes

Introduction to. Algorithms. Lecture 10. Prof. Constantinos Daskalakis CLRS

Huffman Coding - A Greedy Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

Backtracking. Chapter Introduction

arxiv: v1 [math.co] 7 Aug 2012

Information Theory and Communication Optimal Codes

Permutations. = f 1 f = I A

Solutions to Part I of Game Theory

Olympiad Combinatorics. Pranav A. Sriram

Equivalence Classes of Permutations Modulo Replacements Between 123 and Two-Integer Patterns


Twin Binary Sequences: A Nonredundant Representation for General Nonslicing Floorplan

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 6 Lecture - 37 Divide and Conquer: Counting Inversions

ON SOME PROPERTIES OF PERMUTATION TABLEAUX

In how many ways can we paint 6 rooms, choosing from 15 available colors? What if we want all rooms painted with different colors?

Problem A. Vera and Outfits

TYVA-T URBANA DEPT OF COMPUTER SCIENCE. F/s It/I~ GENERATION OF K-ARY TREES. (U) 1980 C L LIU N C UNCL ASS7[IF IED F IMF ENTD

THE TAYLOR EXPANSIONS OF tan x AND sec x

Generating trees and pattern avoidance in alternating permutations

PD-SETS FOR CODES RELATED TO FLAG-TRANSITIVE SYMMETRIC DESIGNS. Communicated by Behruz Tayfeh Rezaie. 1. Introduction

ENUMERATION SCHEMES FOR PATTERN-AVOIDING WORDS AND PERMUTATIONS

Universal Cycles for Permutations Theory and Applications

Completion of the Wilf-Classification of 3-5 Pairs Using Generating Trees

Random permutations avoiding some patterns

EQUIPOPULARITY CLASSES IN THE SEPARABLE PERMUTATIONS

Lecture 18 - Counting

Algorithmique appliquée Projet UNO

On Range of Skill. Thomas Dueholm Hansen and Peter Bro Miltersen and Troels Bjerre Sørensen Department of Computer Science University of Aarhus

CS 540-2: Introduction to Artificial Intelligence Homework Assignment #2. Assigned: Monday, February 6 Due: Saturday, February 18

Lectures: Feb 27 + Mar 1 + Mar 3, 2017

Game Theory and Randomized Algorithms

Splay tree concept Splaying operation: double-rotations Splay insertion Splay search Splay deletion Running time of splay tree operations

Permutation Tableaux and the Dashed Permutation Pattern 32 1

SMT 2014 Advanced Topics Test Solutions February 15, 2014

Lossy Compression of Permutations

Coding for Efficiency

Permutation Groups. Definition and Notation

AN ALTERNATIVE METHOD FOR ASSOCIATION RULES

Struct: Finding Structure in Permutation Sets

Permutation Generation Method on Evaluating Determinant of Matrices

5 AVL trees: deletion

Gray code and loopless algorithm for the reflection group D n

What is a Sorting Function?

Transcription:

University of Wollongong Research Online Department of Computing Science Working Paper Series Faculty of Engineering and Information Sciences 1982 Stack permutations and an order relation for binary trees Reinhold Friedrich Hille University of Wollongong Recommended Citation Hille, Reinhold Friedrich, Stack permutations and an order relation for binary trees, Department of Computing Science, University of Wollongong, Working Paper 82-8, 1982, 11p. http://ro.uow.edu.au/compsciwp/25 Research Online is the open access institutional repository for the University of Wollongong. For further information contact the UOW Library: research-pubs@uow.edu.au

STACK PERMUTATIONS ANI> AN ORDER RELATION FOR BINARY TREES R. Fritz Hille Department of Computing Science University of Wollongong Preprint No. 82-8 Reprinted 9.4.84 P.O. Box 1144. WOLLONGONG N.S.W. 2500. AUSTRALIA tel (042)-270-859 telex AA29022

STACK PERMUTATIONS AND AN ORDER RELATION FOR BINARY TREES. by R.F. Hille Department 01 Computing Science The University 01 Wollongong p.o. Box 1144 WoJlongong 2500 Australia ABSTRACT An isomorphism between stack permutations of a set of n elements and ordered binary trees with n t/ertices Is presented. which allows the construction of simple linear time algorithms to compute a ranking 1unction and its inverse for binary trees. No pre-processing of tables is required. as was the case with previously published methods. Keywords and phrases: binary trees. encoding. lexicographic order. permutations. stack. ' CR categories: 5.31. 5.32. 5.39.

STACK PERMUTAT.ONS AND AN ORDER RELATlON FOR BINARY TREES. by R.F. Hille Department of Computing Science The University of Wollongong p.o. Box 1144- WoUongong 2500 Australia 1. INTRODUCTION Binary trees play an important role In computing. not only as data structures but also as analytical devices. We focus our attention on ordered binary trees, because the distinction between left and right subtree Is naturally Implied In the usual computer re prese ntat/ons. By a stack permutation of a set we mean a permutation that can be generated by pass- Ing the set through a stack once. They are the inverses of Knott's tree permutations C1]. He describes a numbering system for binary trees based on the following definition of an order relation: Given two binary trees T1 and T2. we say T1 (T2 Iff: 1. cit1) (cct2l or 2. c<tu =cit2> and 1(T1> <ICT2>. or 3. c<tu = cit2> and 1(Tl> = IIT2> and rct1> < r<t2>, where err) Is the tree's cardinality. i.e. the number of vertices. 1m and rm are the left and right subtrees. respectively. Knott uses his tree permutations to define a ranking function. which is a mapping from these permutations Into the set of Integers. His tree permutations are In fact the pre-order traversal sequences of binary trees, which are labeled such that their In-

-2- order traversal sequences give the integers in ascending order. He defines them in the following way: a tree permutation p =Pl p' p" has the property that p' and p" are again tree permutations. Pl Is Just the leftmost element of p. His ranking function is a 1-1 mapping from the set of these permutations. and hence from the set of ordered binary trees. onto the set (1.2.3... B n ) of Integers. where B n Is the number of ordered binary trees with cardinality n 2. sec. 2.3.4.4). Knott's algorithm for computing the ranking function. rankct). runs In linear time and gives the relative addresses of the - element CrankClm). rankcrm» in a two dimensional array of size where B cclct» stands for the number of trees with the cardinality of the left subtree and B ccrct» for the number of trees with the cardinality of the right subtree. Hence. his algorithm requires pre-processing of a large table whose size grows exponentially with the cardinality of the trees. His method for computing rank- 1 involves the generation of tree permutations from which the corresponding binary trees can easily be constructed. This algorithm also requires pre-processing of the table. Rotem and Varol [3] use ballot sequences instead of tree permutations to generate all trees of given cardinality. These ballot sequences are the inversion tables of the tree permutations. The authors concede that their method does not produce the trees in lexicographical order. but claim that their algorithms are more efficient than those reported by Knott I Solomon and Finkel [4J present an algorithm that transforms a tree into its successor by operating directly on the tree. and a modification of Knott's Inverse ranking functlon. Their algorithms still require the preparation of a table of Catalan Numbers

-3-8 :: n 1 l+n Their algorithm Nextm requires calls to two routines that run In O(n}. Their RJJ.nk- 1 IT} runs in O(nlogn>. whereas their Rankm requires OCn} operations. These algorithms stili require the pre-processing of tables. Proskurowski [5] constructs all ordered binary trees with n vertices by generating all extended binary trees with n+1 leaves. This is achieved by expanding certain leaves 01 the extensions of ordered binary trees with n-1 vertices. His order Is derived 1rom a way of deciding which leaf to expand next. It appears that his algorithm expand will always commence by expanding the smallest tree 1Irst and building the list from the beginning. His extended binary trees are characterised by sequences 01 binary digits. He generates the master list of trees in reverse order. Rotem and Varol (3} claim:.... there is no simple way of deriving stack sortable permutations in their order corresponding to the natural order of trees. We present a bijection mapping between stack permutations and ordered binary trees and derive from it a simple way to generate binary trees In order corresponding to the lexicographical order of the related stack permutations. Our algorithms ENCODE and DECODE transform a tree Into a binary sequence or a sequence Into a tree. respectively. The algorithm NEXT constructs the successor 01 a given sequence. These algorlthms require no pre-processing of tables and their time complexity Is linear in the number 01 tree nodes. 2. Stack Permutations and Binary Trees. A stack permutation of the set S = Cl.2.3...n} is a permutation that can be generated by passing this set through a stack once. The necessary string of additions and delelions represents a legal sequence of stack operations. meaning. that at any time the number of deletions does not excede the number of additions. We represent the stack

-.,foperations add and delete by the binary disglts 1 and O. respectively. Consequently. we have an isomorphism between legal sequences of stack operations and our stack permutations. We can now construct a simple isomorphism between stack permutations and binary trees In the following way: The digit 1 means to add the left child to the current node. whereas the subsequence 01 means to add the right child. A subsequence of k digits 0 preceding the next digit 1 means to ascend k-1 left links (return k-l levels of recursion) before adding the right child to the node just reached. For example, the sequence 1101000 means that we generate the permutation {2.3.1} from the set 1.2.31 and It represents the binary tree The sequences have 2n+1 digits because every vertex forces a 1 and every open link forces a 0, A binary tree with ~ vertices has n+ 1 open links. The trees are always labeled in such a way that their pre-order traversal sequence gives the Integers In ascending order. The stack permutation generated is then given by the In-order traversal sequence of the tree. The proof that this Is an Isomorphism can easily be constructed directly by showing that there exists exactly one binary tree for every stack permutation. and that there exists exactly one stack permutation for each binary tree. Given a legal sequence of stack operations. one can always construct at least one corresponding ordered binary tree according to the mapping. It Is easily seen that

-5- there cannot be another different binary tree corresponding to the sequence. Conversely. given an ordered binary tree. one can traverse It In pre-order and build the sequence by adding a 1 every time a ver:tex Is visited. When an empty pointer field is encountered. a 0 Is added to the sequence. Hence. It is possible to construct at least one sequence for each binary tree. The fact that this sequene is unique follows from the uniqueness of the traversal order. T,he order of the trees. and therefore also the order of the stack permutations. is the same as the lexicographic order of the binary sequences. Thus. the five binary trees with cardinality 3 are. in order: and the corresponding binary sequences together with the stack permutations In the same ascending order are: 1010100 1 23 1011000 1 32 1100100 2 1 3 1101000 23 1 1110000 321 The last two digits are always 0 and may be omlned. We show them here for completeness. It seems at first sight that the order of the trees produced here 15 the same as the order defined by Knott. However. since the stack permutations are the Inverses of the tree pemutations. the order of the trees will differ In general from that given by Knott. This does not matter very much because the order Is used here primarily for the purpose of avoiding isomorphism checking (see Read. R.C. I7J> when generating a list of trees.

-6- If one wants to generate a random binary tree one may either use the algorithm 01 Arnold and Sleep [81 for the uniform random generation of balanced paranthesis strings or construct a mapping. and therefore an algorithm. from the integers Into the set of bit strings defined above. This allows the use of a random number generator. The successor of a given binary sequence can be constructed by locating the rightmost digit 1 that has a left neighbour 0 and transposing the two. The tail of the sequence (to the right of the current position) must then be reversed to the firtst one 01 It s size, that is. alf digits 1" must be put back at their original places. This can already be done during the search. The current sequence is the last one If the "1" located 15 the leftmost digit of the sequence. 3. The AJgorithms Gj\l8n a binary tree with n vertices. and therefore a binary sequence with 2n+1 digits. we construct the successor In the following way: procedure NEXT begin begin at the right end of the sequence flnished:=false while there are digits to the left and not finished do begin move left to the nearest "1" If the left n'eighbour is "0 then begin exchange 1inlshed:=true end else move the ",. to Its original place end end NEXT In the worst case the while loop runs n times. namely when the given sequence is already the last one. A small amount of book keeping Is required, namely. how many

-7- digits 1 are already in their original places. This determines the original place of the current,"' If it must be moved. Our procedure ENCODE is the equivalent 01 Knott's ranldng function. It produces a binary sequence which is interpreted as an integer for the purpose of determining its successor or comparing It with the code sequence of another binary tree. The algorlthm ENCODE Is essentially a pre-order traversal of the tree, adding the digits 1 or 0 to the sequence a1 the right moment proced ure ENCODE (rootptr) begin If not rootptr=nll then begin add.,. to the sequence ENCODE (rootptr.left) ENCODE(rootptr.rlght> end else add 0 to the sequence end ENCODE The Inverse of this algorithm simply scans the sequence from left to right and builds the tree according to the mapping described in section 2. This corresponds to a pre-order traversal of the tree and hence also runs in linear time because every vertex is visited exactly once.

-8- procedure DECODE(rootptr) begin rootptr:=gecnode geldlgit If digit:1 then DECODE(rootptr.left) If dlglt=o'and nexcdlgit=l then DECODE(rootptr.rlght> geldigit end DECODE 4. A Classlfic~tlon of Tree Permutations In his paper 1J Knott mentiones four classes of tree permutations. which he characterises as hlr, hrl, LRh, and RLh. Obviously. thes~ correspond to the pre- and post-order traversal sequences of the trees associated with the permutations. He then states that the classes LhR and RhL are degenerate because they contain only one permutation. The reas.on for this degeneracy Is very simple. Knott's tree permutations are the pre-order traversal sequences of trees that are labeled In such a way that their inorder sequences are just the Integers in ascending order. The class LhR of permutalions Is made up of the In-orde'r tr~versal sequences of trees whose in-rder traversal sequence is C1.2.3...N}. Hence the degeneracy. Instead of the four classes of tree permutations mentioned by Knott we can define 24 classes. The trees can be labeled In 6 different ways. namely two for each of the three traversal sequences. In each case they can be traversed in four different ways. so that we obtain 24 classes of tree permutations. There exist various close relatlon- Ships between these classes.

-9-5. Conclusion We have presented an algorithm for computing the value of a ranking function of a binary tree. the inverse..and an algorithm to compute the successor of a binary tree. The time complexity of these algorithms is O(n). where n Is the number of vertices. No pre-processing of tables is required. Previously published algorithms for the same purpose required the pre'-processing of tables. and computing the inverse 01 the rank Ing function required time O(nlogn).

- 10- REFERENCES (lj Knott. G.D. -A numbering System lor binary Trees, COmm ACM 20 (1977)113 115. l21 Knuth, D.E. "The Art of Computer Programming-, vets. 1 and 3, second edition, Addison-Wesley. 1973. l31 Rotem, D. and Varol. Y.L "Generation of Binary Trees from Ballot Sequences, J. ACM 25 (1978)396-404. l41 Solomon, M. and Finkel, R.A. -A Note on Enumerating Binary Trees" J. ACM 27 (1980)3-5. l51 Proskurowski. A. "On the Generation of Binary Trees, J. ACM 27 <1980>1-2. (6) Rotem. D. "On a Correspondence between Binary Trees and a certain Type of Permutation", Into. Proc. Lett... (975)58-61, (7) Read. R.C. -Every one a Winner in Algorithmic Aspects of Combinstorics. Afpach, Hell, and Miller, Eds., North-Holland. Amsterdam, 1978, pp. 107-120. (8J Arnold. D,P. and Sleep. M.R. -Uniform Random Generation of Balanced P?ranthesls Strings- ACM Transactions on Programming Languages and Systems 2 <1980>122-8.