Lecture 4
Lecture 4
2
•
• ” ”
•
•
•
▪ ” ”
▪
▪
•
•
•
•
• 𝑢 𝑣 𝑢=𝑣 𝑢 𝑣
•
•
•
•
•
• 𝑣 𝑢 𝑢 𝑣
•
•
• 𝑇 𝑣
𝑣 𝑇 𝑣
• getElement(): Returns the element stored at this position.
• root(): Returns the position of the root of the tree (or null if empty).
• parent(p): Returns the position of the parent of position p (or null if p is the
root).
• children(p): Returns an iterable collection containing the children of position
p (if any).
• numChildren(p): Returns the number of children of position p.
• isInternal(p): Returns true if position p has at least one child.
• isExternal(p): Returns true if position p does not have any children.
• isRoot(p): Returns true if position p is the root of the tree.
• size(): Returns the number of positions (and hence elements) that are contained in
the tree.
• isEmpty(): Returns true if the tree does not contain any positions (and thus no
elements).
• iterator(): Returns an iterator for all elements in the tree (so that the tree itself is
Iterable).
• positions(): Returns an iterable collection of all positions of the tree.
•
•
•
•
Algorithm preOrder(v)
visit(v)
for each child w of v
preorder (w)
•
•
Algorithm postOrder(v)
for each child w of v
postOrder (w)
visit(v)
Mammal
17
•
▪
•
▪
▪
•
▪
▪
▪
•
▪
▪
▪
•
▪
▪
•
•
▪
▪
•
•
• left(p): Returns the position of the left child of p (or null if p has no
left child).
• right(p): Returns the position of the right child of p (or null if p has no
right child).
• sibling(p): Returns the position of the sibling of p (or null if p has no
sibling).
•
•
•
▪
▪
Algorithm inOrder(v)
if left (v) ≠ null
inOrder (left (v))
visit(v)
if right(v) ≠ null
inOrder (right (v))
•
•
•
▪
▪ +
▪
L R
B
−
•
▪
▪ B
▪
•
A D F
C E
•
▪
▪
▪ B
▪
•
A D
C E
•
A B D … G H …
•
•
•
•
29