This action might not be possible to undo. Are you sure you want to continue?

Rein Raudjärv May 8th 2008

**Breadth-first numbering
**

• Given a tree T, create a new tree of the same shape, but with the values at the nodes replaced by the numbers 1...|T| in breadth-first order. • For example:

Solutions

1. Queue based breath-first numbering 2. Level-oriented breath-first numbering 3. Breath-first numbering with lazy evaluation

Queue based solution is more straightforward but functional programmers prefer level-oriented solution.

**The function to be produced
**

datatype 'a Tree = E | T of 'a * 'a Tree * 'a Tree bfnum : 'a Tree int Tree

**Breadth-first traversal of a tree
**

• For example:

[a, b, d, c]

bftrav : 'a Tree 'a list

**Breadth-first traversal of a forest
**

• For example:

[a, d, e, b, c, f]

bftrav' : 'a Tree Seq ‘a list

**Definition of breadth-first traversal
**

bftrav t = bttrav' <t>

bftrav' <> = [] bftrav' (E ts) = bftrav' ts bftrav' (T (x,a,b) ts) = x :: bftrav' (ts a b)

remove from the beginning add to the end <> empty sequence

**Example of breadth-first traversal
**

bftrav' <a>

a :: a :: b :: a :: b :: d :: a :: b :: d :: a :: b :: d :: c :: ... a :: b :: d :: c :: a :: b :: d :: c :: []

bftrav' <b,d> bftrav' <d, E, c> bftrav' <E, c, E, E> bftrav' <c, E, E> bftrav' <E, E, E, E> ... bftrav' <>

**Breadth-first numbering
**

• For example:

bfnum : 'a Tree int Tree bfnum' : int 'a Tree Seq int Tree Seq

**Definition of breadth-first numbering
**

bfnum t = t' where <t'> = bfnum' 1 <t> bfnum' i <> = <> bfnum' i (E ts) = E ts' where ts' = bfnum' i ts bfnum' i (T (x,a,b) ts) = T (i,a',b') where ts' a' b' = bfnum' (i+1) (ts remove from the beginning add to the beginning add to the end

ts' a

b)

<> empty sequence

remove from the end

Double-ended queue is used

**Definition of breadth-first numbering
**

bfnum t = t' where <t'> = bfnum' 1 <t> bfnum' i <> = <> bfnum' i (E ts) = ts' E where ts' = bfnum' i ts bfnum' i (T (x,a,b) ts) = ts' T (i,a',b') where b' a' ts' = bfnum' (i+1) (ts a remove from the beginning add to the end

b)

<> empty sequence

Ordinary queue is used, result sequence is reversed

**Example of breadth-first numbering
**

<a> bfnum' 1 (T (a,b,d) ts) = ts' T (1,b',d') where d' b' ts' = bfnum' 2 (ts b d)

**Example of breadth-first numbering
**

<a> bfnum' 1 (T (a,b,d) ts) = ts' T (1,b',d') where d' b' ts' = bfnum' 2 (ts b d) <b,d> bfnum' 2 (T (b,E,c) ts) = ts' T (2,E,c') where c' E ts' = bfnum' 3 (ts E c)

**Example of breadth-first numbering
**

<a> bfnum' 1 (T (a,b,d) ts) = ts' T (1,b',d') where d' b' ts' = bfnum' 2 (ts b d) <b,d> bfnum' 2 (T (b,E,c) ts) = ts' T (2,E,c') where c' E ts' = bfnum' 3 (ts E c) <d,E,c> bfnum' 3 (T (d,E,E) ts) = ts' T (3,E,E) where E E ts' = bfnum' 4 (ts E E)

**Example of breadth-first numbering
**

<E,c,E,E> bfnum' 4 (E ts) = ts' E where ts' = bfnum 4 ts

**Example of breadth-first numbering
**

<E,c,E,E> bfnum' 4 (E ts) = ts' E where ts' = bfnum 4 ts

<c,E,E> bfnum' 4 (T (c,E,E) ts) = ts' T (4,E,E) where E E ts' = bfnum' 5 (ts E E)

**Example of breadth-first numbering
**

<E,c,E,E> bfnum' 4 (E ts) = ts' E where ts' = bfnum 4 ts

<c,E,E> bfnum' 4 (T (c,E,E) ts) = ts' T (4,E,E) where E E ts' = bfnum' 5 (ts E E)

<E,E,E,E> bfnum' 5 (E

...

ts) = ts'

E where ts' = bfnum 5 ts

**Example of breadth-first numbering
**

<E,c,E,E> bfnum' 4 (E ts) = ts' E where ts' = bfnum 4 ts

<c,E,E> bfnum' 4 (T (c,E,E) ts) = ts' T (4,E,E) where E E ts' = bfnum' 5 (ts E E)

<E,E,E,E> bfnum' 5 (E

ts) = <E,E,E,E>

**Example of breadth-first numbering
**

<E,c,E,E> bfnum' 4 (E ts) = ts' E where ts' = bfnum 4 ts

<c,E,E> bfnum' 4 (T (c,E,E) ts) = ts' where E E ts' = <E,E,E,E>

T (4,E,E)

**Example of breadth-first numbering
**

<E,c,E,E> bfnum' 4 (E ts) = ts' E where ts' = bfnum 4 ts

<c,E,E> bfnum' 4 (T (c,E,E)

ts) = <E,E,T (4,E,E)>

**Example of breadth-first numbering
**

<E,c,E,E> bfnum' 4 (E ts) = <E,E,T (4,E,E),E>

**Example of breadth-first numbering
**

<a> bfnum' 1 (T (a,b,d) ts) = ts' T (1,b',d') where d' b' ts' = bfnum' 2 (ts b d) <b,d> bfnum' 2 (T (b,E,c) ts) = ts' T (2,E,c') where c' E ts' = bfnum' 3 (ts E c) <d,E,c> bfnum' 3 (T (d,E,E) ts) = ts' T (3,E,E) where E E ts' = <E,E,T (4,E,E),E>

**Example of breadth-first numbering
**

<a> bfnum' 1 (T (a,b,d) ts) = ts' T (1,b',d') where d' b' ts' = bfnum' 2 (ts b d) <b,d> bfnum' 2 (T (b,E,c) ts) = ts' T (2,E,c') where c' E ts' = bfnum' 3 (ts E c) <d,E,c> bfnum' 3 (T (d,E,E)

ts) = <T (4,E,E),E,T (3,E,E)>

**Example of breadth-first numbering
**

<a> bfnum' 1 (T (a,b,d) ts) = ts' T (1,b',d') where d' b' ts' = bfnum' 2 (ts b d) <b,d> bfnum' 2 (T (b,E,c) ts) = ts' T (2,E,c') where c' E ts' = <T (4,E,E),E,T (3,E,E)>

**Example of breadth-first numbering
**

<a> bfnum' 1 (T (a,b,d) ts) = ts' T (1,b',d') where d' b' ts' = bfnum' 2 (ts b d) <b,d> bfnum' 2 (T (b,E,c) ts) = <T (3,E,E),T (2,E,T (4,E,E))>

**Example of breadth-first numbering
**

<a> bfnum' 1 (T (a,b,d) ts) = ts' T (1,b',d') where d' b' ts' = <T (3,E,E),T (2,E,T (4,E,E))>

**Example of breadth-first numbering
**

<a> bfnum' 1 (T (a,b,d) ts) = <T (1,T (2,E,T (4,E,E)),T (3,E,E))>

**Example of breadth-first numbering
**

bfnum T (a,b,d) = T (1,T (2,E,T (4,E,E)),T (3,E,E))

**Extracting the next level
**

concat (map children lvl) fun children E = [] | children (T (x,a,b)) = [a,b]

**Rebuilding the tree at each level
**

fun rebuild i [] [] = [] | rebuild i (E :: ts) cs = E :: rebuild i ts cs | rebuild i (T (_,_,_) :: ts) (a :: b :: cs) = T (i,a,b) :: rebuild (i+1) ts cs

• 1st list is the current level, 2nd list is the next level. • We are walking down both lists simultainously.

**Numbering all levels recursively
**

bfnum t = hd (bfnum' 1 [t]) fun bfnum' i [] = [] | bfnum' i lvl = rebuild i lvl nextLvl' where nextLvl = concat (map children lvl) j = i + (length nextlvl div 2) nextLvl' = bfnum' j nextLvl

• The number of non-empty nodes in the current level is the length of the next level divided by 2.

Discussion

• No reason to prefer one over the other • Level-oriented is easier to design from scratch • Queue-based is modest extension to the well-known algorithm for bread-first traversal • Functional programmers prefer level-oriented solution

**Why Functional programmers prefer leveloriented solution?
**

• Unfamiliarity with the underlying traversal algorithm • Unfamiliarity with functional queues and doubleended queues • Premature commitment to a data structure (lists) • Premature commitment to a programming language (pattern matching)

by Jones and Gibbons

Algorithm

bfn :: ([Int], Tree a) -> ([Int], Tree Int)

bfn (ks, E) = (ks, E) bfn (k : ks, T x a b) = (k+1 : ks'', T k a' b') where (ks', a') = bfn (ks, a) (ks'', b') = bfn (ks', b)

• The list of integers represent the first available index on each level.

Threading a list of indices through a tree

**The lazy evaluation
**

bfnum t = t’ where (ks, t') = bfn (1 : ks, t)

• The output of the function is feeded back into the input.

Threading the output of one level into the next level

Thank you!

- Algebra I eksamiks kordamine
- Embedded Typesafe Domain Specific Languages for Java
- Java baitkood
- Inversion of Control, referaat
- Sissejuhatus matemaatilisse loogikasse konspekt
- Automaatne Windows XP paigaldus, praktikumimaterjal
- Ontoloogiad, referaat
- Teksti töötluse alused, referaat
- Automaadid, keeled ja translaatorid konspekt
- Elektrimõõtmised konspekt
- Sissejuhatus arvutuslingvistikasse loengu konspekt
- Sissejuhatus üldkeeleteadusesse loengu konspekt
- Füüsika keskkooli eksami kordamine
- Diskreetse matemaatika elemendid raamatu kokkuvõte
- Matemaatiline analüüs II loengu konspekt
- Matemaatiline analüüs I loengu konspekt
- Blocking Calls in Java
- Blocking Calls in Java

Sign up to vote on this title

UsefulNot usefulPresentation of an article
May 8 2008

Presentation of an article

May 8 2008

May 8 2008

- AP CS AB 2005 Free Response Questions
- Heap
- a1.pdf
- 10.1.1.10
- ADSs
- Queuing Theory
- Uninformed Search
- 04.search
- Interprocesscommunication
- Waiting Line
- jquery-1.8.2.js
- Lab 2 Tree Traversal in Prolog
- Search 041005
- elk-20-5-8-1012-945
- DElta Que BW/FI extraction
- Mutual Exclusion in Distributed System
- UT Dallas Syllabus for se3345.081.06u taught by Ovidiu Daescu (daescu)
- UT Dallas Syllabus for cs3345.081 06u taught by Ovidiu Daescu (daescu)
- ex no1
- 47977466-Qrfc-and-Trfc
- Aug02_JohnZ
- Chapter 3
- lect-13-14
- Implementing Publication Flow
- HTB
- 2. Abs Ack Dec List
- Tree
- B+ Trees
- Forest Optimization Algorithm
- Ada
- Breadth-First Numbering