Introduction to trees

• Trees

• Tree

• Tree = a set of nodes that is organized into a hierarchy

• There is exactly one node on the top level of the hierarchy

 The top level node is called: the root node

• Example: tree

Note:

 The root node is a --- there is always only one node at the top level

• Weird fact

• Computer Scientists draw a tree upside down:

• That's why the top node is called the root node:

• Terminology

• Node, root and edge:

Term Meaning
node   The vertices that form the hierarchical structure
root (or "root node")   The topmost node in a tree
edge (or link)   The connection from a level k node to a level k+1 node

Graphically:

• Parent and child

Term Meaning
Parent node   The node that is immediately above in the tree
Child node   The node that is immediately below in the tree

Graphically:

• Internal and external (leaf) nodes

Term Meaning
Internal node   a node that has at least 1 child node
External (leaf) node   a node that has no (zero) child node

Graphically:

• Path, path length and height of a tree

Term Meaning
Path from a node x to a node y   The edges that connects node x to node y
length of a path   The number of edges in the path
height of a tree   The longest path from the root to a leaf node in the tree

Graphically:

• Height and depth of a node

Term Meaning
Height of a node x   The length of the longest path from the node x to a leaf node in the tree
Depth of a node x   The length of the path from the root node to the node x

Graphically:

Note:

 Height(c) = 2 because the longest path from c to a leaf node has 2 edges !

• Subtree

Term Meaning
Subtree rooted at a node x   The portion of the tree consisting of the node x and all the children nodes of x and their children nodes and so on.

Graphically: