COMP 250 Lecture 20 tree trversl Oct. 25/26, 2017 1
2
Tree Trversl How to visit (enumerte, iterte through, trverse ) ll the nodes of tree? 3
depthfirst (root){ // preorder if (root is not empty){ visit root for ech child of root depthfirst( child ) 1 4
depthfirst (root){ // preorder if (root is not empty){ visit root for ech child of root depthfirst( child ) 2 1 3 5
depthfirst (root){ // preorder if (root is not empty){ visit root for ech child of root depthfirst( child ) 2 1 3 4 5 6
depthfirst (root){ // preorder if (root is not empty){ visit root for ech child of root depthfirst( child ) 1 2 7 8 3 4 9 10 11 5 6 7
Preorder Trversl e.g. Printing directory (visit = print) Eminem Lose Yourself My Documents Music Videos Wor She my sillies out Rffi Bby Belug COMP 250 Reserch Documents (directory) Music (directory) Eminem (directory) Lose Yourself (file) Rffi (directory) She My Sillies Out (file) Bby Belug (file) Videos (directory) : (file) Wor (directory) COMP250 (directory) : Reserch (directory) : 8
Visit implies tht you do something t tht node. Anlogy: you ren t visiting London UK if you just fly through Hethrow. 9
depthfirst (root){ // postorder if (root is not empty){ for ech child of root depthfirst( child ) visit root Q: Which node is visited first? 10
depthfirst (root){ // postorder if (root is not empty){ for ech child of root depthfirst( child ) visit root 1 11
depthfirst (root){ // postorder if (root is not empty){ for ech child of root depthfirst( child ) visit root 1 2 12
depthfirst (root){ // postorder if (root is not empty){ for ech child of root depthfirst( child ) visit root 5 1 4 2 3 13
depthfirst (root){ // postorder if (root is not empty){ for ech child of root depthfirst( child ) visit root 11 5 6 10 1 4 7 8 9 2 3 14
Exmple 1 postorder: recll lst lecture 4 2 1 3 1 0 0 2 1 0 0 0 1 0 0 0 height(v){ if (v is lef) return 0 else{ h = 0 for ech child w of v h = mx(h, height(w)) return 1 + h visit = return vlue of height 15
Exmple 2 Postorder: Wht is the totl number of bytes in ll files in directory? 16
numbytes(root){ if root is lef return number of bytes t root else { sum = 0 for ech child of root{ sum += numbytes(child) return sum By visit here, we men determining the number of bytes for node, e.g. If we were to store sum t the node. 17
NOTE: Sme cll sequence occurs for preorder vs postorder. Letter order corresponds to depthfirst() cll order b g h c d i j e f 18
Cll stc for depthfirst() b g h c d i j e f e f c d d d d d i j b b b b b b b b b g h h h h h h h 19
Cll stc for depthfirst() b g h c d i j e f e f c d d d d d i j b b b b b b b b b g h h h h h h h 20
Cll stc for depthfirst() b g h c d i j e f e f c d d d d d i j b b b b b b b b b g h h h h h h h 21
Cll stc for depthfirst() b g h c d i j e f e f c d d d d d i j b b b b b b b b b g h h h h h h h 22
Tree trversl Recursive depth first (pre- versus post-order) Non-Recursive using stc using queue 23
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur // moving visit cur to be for ech child of cur // fter for loop s.push(child) // chnges nothing 24
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur 25
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) 26
Wht is the order of nodes visited? 27
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 28
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 29
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 30
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 31
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 32
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 33
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 34
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 35
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 36
treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 37
Stc bsed method is depth first, but visits children from right to left b g h c d i j e f recursive non-recursive (stc) bcdefghij hjigbdfec 38
Pre- or post order? treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) visit cur Moving the visit does not me it post order. Why not? 39
Wht if we use queue insted? treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) 40
treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) Queue stte t strt of the while loop b c d e f g h i j 41
treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) Queue stte t strt of the while loop b c d b c d e f g h i j 42
treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) Queue stte t strt of the while loop b c d c d e f b c d e f g h i j 43
treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) Queue stte t strt of the while loop b c d c d e f d e f b c d e f g h i j 44
treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) Queue stte t strt of the while loop b c d c d e f d e f e f g h i b c d e f g h i j 45
treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) b c d e f g h j i b c d c d e f d e f e f g h i f g h i g h i j h i j i j j 46
bredth first trversl for ech level i visit ll nodes t level i b c d order visited: bcdefghij e f g h i j 47
Implementtion Detils Recll: first child, next sibling clss TreeNode<T>{ T element; TreeNode<T> firstchild; TreeNode<T> nextsibling; : : clss Tree<T>{ TreeNode<T> root; : : 48
for ech child{ mens: child = cur.firstchild while (child!= null){.. child = child.nextsibling 49