Trees (chapter 8) Flashcards
Define a tree in terms of models and structure
A tree is an abstract model that stores element hierarchically
What does a tree consist of?
consists of nodes that have a parent-child relationship with the exception of the root which is a special node that has no parent. Along with possible subtrees
Define internal and external nodes
an internal node is a node that has a child/children. An external node is a node with no children, also known as a leaf
Define an edge
an edge is pair of nodes, (u,v) such that u,v have a parent-child relationship.
Define a path
a path is a sequence of nodes situated in such a manner that any 2 consecutive nodes in the sequence form an edge
In what 3 areas can trees be applied?
- Organization charts (CEO and employees under him/her)
- File systems. There is a root folder, I go to documents, then CSC, then lectures but there are many other paths that I can take
- Programming environments
Define a sub-tree
A tree consists of a node and its descendants. A tree can be a subtree of itself
Describe the depth of a tree.
- depth is described as the number of edges present from the root of a tree to the particular NODE in question (excluding the node)
- also defined as the number of ancestors a node has (excluding the node)
Give code on how to find the depth of a tree recursively
int depth(node v){ if v is root return 0 else return 1 + depth(parent(v)) }
What is the height of a tree (hint, an external node has a height of 0)
The height of a tree is the largest depth of a node.
Give code to find the height of a node
int height(Node v){ if v isExternal return 0 else return 1 + max(height(child(v))) }
What ADT do we use to define a tree why is it useful
We use position ADT, why? Beats me
How is a single node in a tree represented?
They are represented by an object that stores an element, a parent of the node, and a sequence of children of the node
3 types of tree traversals?
Post order, pre-order and inorder traversal
What is a pre-order traversal and where can it be applied?
in a pre-order, a node is visited before its descendants.
the application would be printing a structured document, the headings and subheadings must be printed before the text can be printed.