Introduction To Trees: Maria Sabir
Introduction To Trees: Maria Sabir
Maria Sabir
Terminology
Hierarchical data structure
A tree consists of a finite set of elements,
called nodes, and a finite set of directed lines,
called branches, that connect the nodes.
The number of branches associated with a
node is the degree of the node.
The number of branches directed toward the
node is the indegree of the node.
The number of branches directed away from
the node is the outdegree of the node.
Terminology
The sum of the indegree and outdegree is the
degree of the node.
If the tree is not empty, then the first node is
called the root node.
The indegree of the root node is, by definition,
zero.
With the exception of the root node, all of the
nodes in the tree must have an indegree of exactly
one.
All nodes in the tree can have zero, one, or more
branches leaving them (i.e., an outdegree of 0, 1,
or more).
Terminology
Example of a tree
Terminology
In addition to root, many other terms
are used to describe the attributes of a
tree.
A leaf is any node with an outdegree of
zero.
An internal node is a node with an
outdegree of at least 1 (i.e., not a leaf
node).
Terminology
A node is a parent if it has successor
nodes – that is, if it has an outdegree
greater than zero.
Conversely, a node with a predecessor
is called a child.
2 or more children with the same
parent are siblings.
Terminology
Hence:
The root node is the only node in the tree
that does not have a parent.
A leaf node has no children.
An internal node has at least one child.
Terminology
Parents: A, B, F Leaves: C, D, E, G, H, I
Children: B, E, F, C, D, G, H, I Internal Nodes: A, B, F
Siblings: {B, E, F}, {C, D}, {G, H, I}
Terminology
Two nodes are adjacent if a branch connects them.
A path is a sequence of nodes in which each node
is adjacent to the next one.
Every node in the tree can be reached by following
a unique path starting from the root.
The length of this path is the number of edges on
the path.
There is a path of length 0 from every node to itself.
Terminology
The path from the root, A, to the leaf, I, is denoted as AFI and
has a length of 2.
ABD is the path from the root, A, to the leaf, D, and also has a
length of 2.
Terminology
An ancestor of node N is any node in
the path from the root to node N.
A descendent of node N is any node in
the path below N; that is, all nodes in
the paths from node N to a leaf are
descendents of N.
Terminology
The depth (or level) of a node is the
length of the path from the root to the
node.
Hence, the depth of the root is 0, the
root’s children have a depth of 1, etc.
The depth of a tree is the length of the
path from the root to the leaf in the
longest path.
Terminology
For example, C and D are siblings, and G,H, and I are siblings.
However, C and G are not siblings because they have different
parents.
Terminology
The height of a node is the length of
the longest path from the node to a
leaf.
Hence, all leaves are at height 0, a
leaf’s parent is at height 1, etc.
The height of the tree is simply the
height of the root.
The height of an empty tree is –1.
Terminology
A tree may be divided into subtrees.
A subtree is a tree that has the child of a node
as its root.
Hence, a subtree is composed of a node and all
of that node’s descendants.
The first node in a subtree is known as the root
of the subtree and is used to name the subtree.
Subtrees themselves can be further divided into
other subtrees.
Terminology
B = H L - HR
Balance Factor Example
B = H L - HR
(a) B = (-1) – (-1) = 0
(b) B = (-1) – (-1) = 0
(c) B = 0 – (-1) = 1
(d) B = (-1) – 0 = -1
(e) B = 0–0=0
(f) B = 1–0=1
(g) B = (-1) – 1 = -2
(h) B = 1 – (-1) = 2
Balance
A tree is completely balanced if its
balance factor is 0 and its subtrees are
also completely balanced.
Completely balanced:
B = 1 – 1 = 0 and
nodes B and C are
completely balanced as well.
42 31
37 9
7 19
Complete Binary Tree
A complete binary tree is a binary
tree that is completely filled, with the
possible exception of the bottom level,
which is filled from left to right.
Examples:
Perfect Binary Tree
A perfect binary tree corresponds to
a tree in which each node has 2
children (except for the last level in
which each node has no children).
Intuitively, a perfect binary tree has no
missing nodes.
A perfect binary tree has the maximum
number of entries for its height.
Perfect Binary Tree
Note:
A perfect binary tree is a full binary tree in
which all leaf nodes are at the same level.
A perfect binary tree is a special case of a
complete tree.
Examples:
Binary Tree Structure
The representation of a binary tree structure
is relatively straightforward.
We need a variable to store the data at the
node and 2 pointers to the left and right
subtrees.
Node {
int data
Node *left
Node *right
}
Binary Tree Structure
Binary Tree Traversals
A binary tree traversal requires that
each node of the tree be processed
once and only once in a predetermined
sequence.
The 2 general approaches to the
traversal sequence are:
Depth-first
Breadth-first
Depth-First Traversal
In the depth-first traversal, the
processing proceeds along a path from the
root through one child to the most distant
descendant of that first child before
processing a second child.
In other words, in the depth-first traversal,
you process all of the descendents of a
child before going on to the next child.
Depth-First Traversals
The 3 standard depth-first traversals
are shown below.
The numbers represent the order in
which the nodes are processed.
Preorder Traversal
In the preorder traversal, the root node
is processed first, followed by the left
subtree and then the right subtree.
m.ali shahid
Traversals and Function
Pointers
Traversal algorithms are a great use of
function pointers.
For example, you can write 1 traversal
algorithm and have it perform many different
tasks.
Example: myTree.inorder(myFunc)
This performs an inorder traversal of the tree
and, at each node, can display the contents,
modify the contents, etc. depending on the
function that is passed to it.
General Trees
A general tree is a tree in which each node
has an unrestricted outdegree.
Many “real world” situations can be modeled
with general trees.
Therefore, we need to be able to process
general trees.
To process a general tree, however, we first
convert the general tree into a binary tree.
We now describe this process.
General Trees
Here, each node has 2 pointers:
The left pointer points to the node’s oldest
child (i.e., left-most child).
The right pointer points to the node’s next
sibling.
General Trees
m.ali shahid
n-ary Trees
An n-ary tree is a generalization of a
binary tree whose nodes can have no
more than n children.
Implementations:
You can use the implementation just
discussed to implement an n-ary tree OR
Since you know the maximum number of
children for each node, you can let each
node point directly to its children.
n-ary Trees
Example of a ternary tree
m.ali shahid
Alternate Implementation
We have assumed we are using
pointers.
However, we could have just as easily
implemented our tree using an array.
Alternate Implementation