Trees Flashcards
What are the two ways you could view a tree as?
Generalisation of a linked list
Specialisation of a graph
Define a tree
Abstract data type that simulates a hierarchical tree structure, with a root value and subtrees with children
What makes a binary tree special?
Each node has at most two child nodes
What three things are stored in a class for a BinaryTreeNode?
element
left node
right node
Define traversal
Systematic way of accessing or visiting all the nodes
Complexity for insert in a tree
Linear
Complexity for contains in a tree
Linear
Complexity for remove in a tree
Linear
Complexity for size in a tree
Linear
What are the four strategies for traversal?
Breadth first Depth first (in order, post order, pre order)
Describe in order traversal
Visit the first child, visit the node, visit the next
node/child
l n r
Describe post order traversal
Visit the children, visit the node
l r n
Describe pre order traversal
Visit the node, visit the children
n l r
Write a recursive function that does pre order traversal of a tree
public void traverse(BinaryTreeNode node){
visit(node.element)
if(node.left !=null) traverse(node.left)
if(node.right !=null) traverse(node.right)
}
Write a recursive function that does post order traversal of a tree
public void traverse(BinaryTreeNode node){
if(node.left !=null) traverse(node.left)
if(node.right !=null) traverse(node.right)
visit(node.element)
}