Basic Algorithms on Trees
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.
Data Structure Exercises 11.1
Binary Trees
The Binary Tree Abstract Data Type
A Binary Tree Interface in Java
Levels of a Binary Tree
Level: number of nodes at a level:
Level Number of nodes Pattern … … k … … … k 2 …
Traversals of a Binary Tree
Data Structure Exercises 11.2
Inorder tree traversal Inorder traversal based on recursion:
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
inorder(T, x) if … “visit” x if … 3 inorder(T, v) if … inorder(T, x) “visit” v If … inorder (T, y) 4
if … “visit” y if … 5 inorder(T, a) if … inorder(T, b) “visit” a If … inorder (T, c) 8 7 9
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); }
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) print(x) 3 print(w) 1 print(c) 89