Download presentation
Presentation is loading. Please wait.
1
Basic Algorithms on Trees
3
If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of the parent of the node v.
11
Data Structure Exercises 11.1
12
Binary Trees
14
The Binary Tree Abstract Data Type
15
A Binary Tree Interface in Java
17
Levels of a Binary Tree
18
Level: number of nodes at a level:
19
Level Number of nodes Pattern 0 1 1 2 2 4 3 8 … … k … … 0 2 1 2 2 2 3 2 … k 2 …
20
Traversals of a Binary Tree
23
Data Structure Exercises 11.2
24
Inorder tree traversal Inorder traversal based on recursion:
25
inorder(T, r) if … inorder(T, u) “visit” r If … inorder (T, a) inorder(T, u) if … inorder(T, w) “visit” u If … inorder (T, v) inorder(T, w) if … “visit” w if … 1 26
26
inorder(T, x) if … “visit” x if … 3 inorder(T, v) if … inorder(T, x) “visit” v If … inorder (T, y) 4
27
if … “visit” y if … 5 inorder(T, a) if … inorder(T, b) “visit” a If … inorder (T, c) 8 7 9
28
Inorder traversal based on Stack data structure Algorithm Stack-control-inorder(T, v) establish stack S; S.push(v); while (S is not empty) do {u := S.pop(); if u is leaf or u is marked, visit u; else {let v1 and v2 be the left and right child node of v, respectively; S.push(v2); mark u; S.push(u*); S.push(v1); }
29
u r* a r w u* v r* a u* v r* a v r* a print(u) x v* y r* a v* y r* a print(v) y r* a print(y) r* a print(y) a b a* c a* c print(b) c print(a) 2 456 7 print(x) 3 print(w) 1 print(c) 89
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.