0% found this document useful (0 votes)
7 views29 pages

Lecture 4

The document outlines various tree data structure operations, including methods for retrieving elements, parent-child relationships, and tree traversal algorithms such as pre-order, post-order, and in-order. It also describes characteristics of tree nodes, such as whether they are internal, external, or the root. Additionally, it provides details on how to access left and right children and siblings of a node.

Uploaded by

Zooz 24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views29 pages

Lecture 4

The document outlines various tree data structure operations, including methods for retrieving elements, parent-child relationships, and tree traversal algorithms such as pre-order, post-order, and in-order. It also describes characteristics of tree nodes, such as whether they are internal, external, or the root. Additionally, it provides details on how to access left and right children and siblings of a node.

Uploaded by

Zooz 24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

1

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

Dog Caw Cat

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

You might also like