0% found this document useful (0 votes)
40 views56 pages

Introduction To Trees: Maria Sabir

The document defines key terminology used for trees, including nodes, branches, degrees, paths, ancestors, descendants, subtrees, and different types of binary trees. It describes tree traversal methods, specifically preorder, inorder, and postorder traversals for depth-first searching of a binary tree. The traversal methods are defined by the order in which tree nodes are processed.

Uploaded by

Awais Sahab
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views56 pages

Introduction To Trees: Maria Sabir

The document defines key terminology used for trees, including nodes, branches, degrees, paths, ancestors, descendants, subtrees, and different types of binary trees. It describes tree traversal methods, specifically preorder, inorder, and postorder traversals for depth-first searching of a binary tree. The traversal methods are defined by the order in which tree nodes are processed.

Uploaded by

Awais Sahab
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 56

Introduction to Trees

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

 Note the relationship between levels and siblings.


 Siblings are always at the same level, but all nodes in a level are
not necessarily siblings.
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

 The nodes in the triangles are subtrees.


 Therefore, BCD, E, and FGHI are all subtrees.
Binary Trees
 A binary tree is a tree in which no
node can have more than 2
children/subtrees.
 In other words, a node can have 0, 1,
or 2 subtrees.
 These subtrees are designated as left
and right child or left and right subtree.
Binary Trees

 Above is an example of a binary tree.


Binary Trees
 Below are more examples of binary trees.
 (a) is a null tree.
 A null tree is a tree with no nodes.
 Note that a tree need not be symmetric.
Binary Trees
Balance
 The distance of a node from the root
determines how efficiently it can be
located.
 The shorter the height of the tree, the
less time it takes to access any given
node.
 This observations leads us to the
important concept of balance.
Balance Factor
 To determine whether the tree is
balanced, we calculate its balance
factor.
 The balance factor of a binary tree is
the difference in height between its left
and right subtrees.

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.

NOT Completely balanced:


B = 1 – 1 = 0 but
node C is not completely
balanced!
Balance
 Because completely balanced trees occur so
seldomly, we usually concern ourselves with
nearly balanced trees.
 A binary tree is considered nearly balanced if
the height of its subtrees differs by no more
than 1 (i.e., a balance factor of –1, 0, or 1) and
its subtrees are also nearly balanced.
 From now on, when we speak of a balanced
tree, we will be referring to a nearly balanced
tree.
Full Binary Tree
 A full binary tree is a binary tree in
which each node has exactly 0 or 2
children. 
 Example: 22

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.

Preorder = root node before


the left and right subtrees.
Preorder Traversal
Preorder Traversal
Pseudocode
preOrder( root ) {
if (root is not null)
process(root)
preOrder(root->leftSubtree)
preOrder(root->rightSubtree)
end if
return Note that we process the
root node first, then the left
}
subtree, followed by the
right subtree.
Inorder Traversal
 In the inorder traversal, the left subtree
is processed first, followed by the root
node, and then the right subtree.

Inorder = root node in between


the left and right subtrees.
Inorder Traversal
Inorder Traversal Pseudocode
inOrder( root ) {
if (root is not null)
inOrder(root->leftSubtree)
process(root)
inOrder(root->rightSubtree)
end if
return Note that we process the left
subtree first, then the root
} node, followed by the
right subtree.
Postorder Traversal
 In the postorder traversal, the left
subtree is processed first, followed by
the right subtree, and then the root
node.

Postorder = root node after


the left and right subtrees.
Postorder Traversal
Postorder Traversal
Pseudocode
postOrder( root ) {
if (root is not null)
postOrder(root->leftSubtree)
postOrder(root->rightSubtree)
process(root)
end if
Note that we process the
return left subtree first, then the right
} subtree, followed by the root
node.
Depth-First Traversals
 Note that in the depth-first traversals
we use a stack.
 Remember: the depth-first traversals
utilize recursion, which is implemented
using a stack.
Breadth-First Traversals
 In a breadth-first traversal, the
processing proceeds horizontally from
the root to all of its children, then to its
children’s children, and so forth until all
nodes have been processed.
 In other words, in the breadth-first
traversal, each level is completely
processed before the next level is
started.
Breadth-First Traversal
Breadth-First Traversal
Pseudocode
nodeToProcess = root
loop (nodeToProcess not null) Note that the breadth-first
process(nodeToProcess) traversal makes use of the
if( nodeToProcess->left not null)
enqueue(nodeToProcess->left)
queue data structure.
end if
if( nodeToProcess->right not null)
enqueue(nodeToProcess->right)
end if
if( not emptyQueue)
nodeToProcess = dequeue()
else
nodeToProcess = null
end if
end loop

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

Here, our tree is stored in


a 2D array. The numbers
stored with a node are the
indices of the left and right
children of the node in the
array.

You might also like