intro to trees
TRANSCRIPT
-
8/8/2019 Intro to Trees
1/56
Introduction to Trees
Maria Sabir
-
8/8/2019 Intro to Trees
2/56
Terminology Hierarchical data structure
A tree consists of a finite set of elements,
called nodes, and a finite set of directedlines, called branches, that connect thenodes.
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.
-
8/8/2019 Intro to Trees
3/56
Terminology The sum of the indegree and outdegree is the
degree of the node.
If the tree is not empty, then the first node iscalled the rootnode.
The indegree of the root node is, bydefinition, zero.
With the exception of the root node, all of thenodes in the tree must have an indegree ofexactly one.
All nodes in the tree can have zero, one, ormore branches leaving them (i.e., anoutdegree of 0, 1, or more).
-
8/8/2019 Intro to Trees
4/56
TerminologyExample of a tree
-
8/8/2019 Intro to Trees
5/56
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 internalnode is a node with anoutdegree of at least 1 (i.e., not a leafnode).
-
8/8/2019 Intro to Trees
6/56
TerminologyA node is a parent if it has successor
nodes that is, if it has an outdegreegreater than zero.
Conversely, a node with a predecessoris called a child.
2 or more children with the sameparent are siblings.
-
8/8/2019 Intro to Trees
7/56
Terminology Hence:
The root node is the only node in the treethat does not have a parent.
A leaf node has no children.
An internal node has at least one child.
-
8/8/2019 Intro to Trees
8/56
Terminology
Parents: A, B, F Leaves: C, D, E, G, H, IChildren: B, E, F, C, D, G, H, I Internal Nodes: A, B, FSiblings: {B, E, F}, {C, D}, {G, H, I}
-
8/8/2019 Intro to Trees
9/56
Terminology Two nodes are adjacent if a branch
connects them.
A path is a sequence of nodes in which eachnode is adjacent to the next one.
Every node in the tree can be reached byfollowing 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.
-
8/8/2019 Intro to Trees
10/56
Terminology
The path from the root, A, to the leaf, I, isdenoted as AFI and has a length of2.
ABD is the path from the root, A, to the leaf,
D, and also has a length of2.
-
8/8/2019 Intro to Trees
11/56
-
8/8/2019 Intro to Trees
12/56
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
roots children have a depth of 1, etc.
The depth of a tree is the length of thepath from the root to the leaf in thelongest path.
-
8/8/2019 Intro to Trees
13/56
Terminology
Note the relationship between levels andsiblings.
Siblings are always at the same level, but allnodes in a level are not necessarily siblings.
-
8/8/2019 Intro to Trees
14/56
-
8/8/2019 Intro to Trees
15/56
Terminology The heightof a node is the length of
the longest path from the node to a
leaf. Hence, all leaves are atheight 0, a
leafs parent is atheight 1, etc.
The height of the tree is simply theheight of the root.
The height of an empty tree is 1.
-
8/8/2019 Intro to Trees
16/56
Terminology A tree may be divided into subtrees.
A subtree is a tree thathas the child of a
node as its root. Hence, a subtree is composed of a node and
all of that nodes descendants.
The first node in a subtree is known as the
root of the subtree and is used to name thesubtree.
Subtrees themselves can be further dividedinto other subtrees.
-
8/8/2019 Intro to Trees
17/56
-
8/8/2019 Intro to Trees
18/56
Binary TreesA binarytree is a tree in which no
node can have more than 2children/subtrees.
In other words, a node can have 0, 1,or 2 subtrees.
These subtrees are designated as leftand right child or left and right subtree.
-
8/8/2019 Intro to Trees
19/56
Binary Trees
Above is an example of a binary tree.
-
8/8/2019 Intro to Trees
20/56
Binary Trees
Below are more examples of binary trees.
(a) is a nulltree. A null tree is a tree with no nodes.
Note that a tree need not be symmetric.
-
8/8/2019 Intro to Trees
21/56
Binary Trees
-
8/8/2019 Intro to Trees
22/56
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 givennode.
This observations leads us to theimportant concept ofbalance.
-
8/8/2019 Intro to Trees
23/56
Balance Factor To determine whether the tree is
balanced, we calculate its balancefactor.
The balance factor of a binary tree isthe difference in height between its left
and right subtrees.
B = HL - HR
-
8/8/2019 Intro to Trees
24/56
Balance Factor ExampleB = HL - 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
-
8/8/2019 Intro to Trees
25/56
BalanceA tree is completely balanced if its
balance factor is 0 and its subtrees are
also completely balanced.Completely balanced:B = 1 1 = 0 andnodes B and C are
completely balanced as well.
NOT Completely balanced:B = 1 1 = 0 butnode C is not completely
balanced!
-
8/8/2019 Intro to Trees
26/56
Balance Because completely balanced trees occur so
seldomly, we usually concern ourselves with
nearlybalancedtrees. A binary tree is considered nearly balanced if
the height of its subtrees differs by no morethan 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 balancedtree, we will be referring to a nearly balancedtree.
-
8/8/2019 Intro to Trees
27/56
Full Binary TreeA fullbinarytree is a binary tree in
which each node has exactly 0 or 2
children.
Example:22
4231
37
7 19
9
-
8/8/2019 Intro to Trees
28/56
Complete Binary TreeA completebinarytree is a binary
tree that is completely filled, with the
possible exception of the bottom level,which is filled from left to right.
Examples:
-
8/8/2019 Intro to Trees
29/56
Perfect Binary TreeA perfectbinarytree corresponds to
a tree in which each node has 2
children (except for the last level inwhich each node has no children).
Intuitively, a perfect binary tree has nomissing nodes.
A perfect binary tree has the maximumnumber of entries for its height.
-
8/8/2019 Intro to Trees
30/56
Perfect Binary Tree Note:
A perfect binary tree is a full binary tree inwhich all leaf nodes are at the same level.
A perfect binary tree is a special case of acomplete tree.
Examples:
-
8/8/2019 Intro to Trees
31/56
Binary Tree Structure The representation of a binary tree structure
is relatively straightforward.
We need a variable to store the data at thenode and 2 pointers to the left and rightsubtrees.
Node {int dataNode *leftNode *right
}
-
8/8/2019 Intro to Trees
32/56
Binary Tree Structure
-
8/8/2019 Intro to Trees
33/56
Binary Tree TraversalsA binarytreetraversalrequires that
each node of the tree be processed
once and only once in a predeterminedsequence.
The 2 general approaches to thetraversal sequence are: Depth-first
Breadth-first
-
8/8/2019 Intro to Trees
34/56
Depth-First Traversal In the depth-first traversal, the
processing proceeds along a path from
the root through one child to the mostdistant descendant of that first childbefore processing a second child.
In other words, in the depth-firsttraversal, you process all of thedescendents of a child before going on
to the next c
hild.
-
8/8/2019 Intro to Trees
35/56
Depth-First Traversals The 3 standard depth-first traversals
are shown below.
The numbers represent the order inwhich the nodes are processed.
-
8/8/2019 Intro to Trees
36/56
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 beforethe left and right subtrees.
-
8/8/2019 Intro to Trees
37/56
Preorder Traversal
-
8/8/2019 Intro to Trees
38/56
Preorder Traversal
PseudocodepreOrder( root ) {
if (root is not null)process(root)preOrder(root->leftSubtree)preOrder(root->rightSubtree)
end ifreturn
}
Note that we process theroot node first, then the leftsubtree, followed by theright subtree.
-
8/8/2019 Intro to Trees
39/56
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 inbetweenthe left and right subtrees.
-
8/8/2019 Intro to Trees
40/56
Inorder Traversal
-
8/8/2019 Intro to Trees
41/56
Inorder Traversal PseudocodeinOrder( root ) {
if (root is not null)inOrder(root->leftSubtree)process(root)inOrder(root->rightSubtree)
end ifreturn
}
Note that we process the leftsubtree first, then the rootnode, followed by theright subtree.
-
8/8/2019 Intro to Trees
42/56
Postorder Traversal In the postorder traversal, the left
subtree is processed first, followed by
the right subtree, and then the rootnode.
Postorder = root node afterthe left and right subtrees.
-
8/8/2019 Intro to Trees
43/56
Postorder Traversal
-
8/8/2019 Intro to Trees
44/56
Postorder Traversal
PseudocodepostOrder( root ) {
if (root is not null)postOrder(root->leftSubtree)postOrder(root->rightSubtree)process(root)
end ifreturn
}
Note that we process theleft subtree first, then the rightsubtree, followed by the rootnode.
-
8/8/2019 Intro to Trees
45/56
Depth-First Traversals Note that in the depth-first traversals
we use a stack.
Remember: the depth-first traversalsutilize recursion, which is implementedusing a stack.
-
8/8/2019 Intro to Trees
46/56
Breadth-First Traversals In a breadth-firsttraversal, the
processing proceeds horizontally from
the root to all of its children, then to itschildrens children, and so forth until allnodes have been processed.
In other words, in the breadth-firsttraversal, each level is completelyprocessed before the next level is
started.
-
8/8/2019 Intro to Trees
47/56
Breadth-First Traversal
-
8/8/2019 Intro to Trees
48/56
m.ali shahid
Breadth-First Traversal
PseudocodenodeToProcess = root
loop (nodeToProcess not null)
process(nodeToProcess)
if( nodeToProcess->left not null)enqueue(nodeToProcess->left)
end if
if( nodeToProcess->right not null)
enqueue(nodeToProcess->right)
end ifif( not emptyQueue)
nodeToProcess = dequeue()else
nodeToProcess = null
end if
end loop
Note that the breadth-firsttraversal makes use of the
queue data structure.
-
8/8/2019 Intro to Trees
49/56
Traversals and Function
Pointers Traversal algorithms are a great use of
function pointers.
For example, you can write 1 traversalalgorithm and have it perform many differenttasks.
Example: myTree.inorder(myFunc)
This performs an inorder traversal of the treeand, at each node, can display the contents,modify the contents, etc. depending on thefunction that is passed to it.
-
8/8/2019 Intro to Trees
50/56
General Trees A generaltree is a tree in which each nodehas an unrestricted outdegree.
Many real world situations can be modeledwith general trees.
Therefore, we need to be able to processgeneral trees.
To process a general tree, however, we firstconvert the general tree into a binary tree.
We now describe this process.
-
8/8/2019 Intro to Trees
51/56
General Trees Here, each node has 2 pointers:
The left pointer points to the nodes oldest
child(i.e., left-most child). The right pointer points to the nodes next
sibling.
-
8/8/2019 Intro to Trees
52/56
m.ali shahid
General Trees
-
8/8/2019 Intro to Trees
53/56
n-ary TreesAn n-arytree is a generalization of a
binary tree whose nodes can have no
more than n children. Implementations:
You can use the implementation justdiscussed to implement an n-ary tree OR
Since you know the maximum number ofchildren for each node, you can let eachnode point directly to its children.
-
8/8/2019 Intro to Trees
54/56
m.ali shahid
n-ary TreesExample of a ternary tree
-
8/8/2019 Intro to Trees
55/56
Alternate Implementation We have assumed we are using
pointers.
However, we could have just as easilyimplemented our tree using an array.
-
8/8/2019 Intro to Trees
56/56
Alternate Implementation
Here, our tree is stored in
a 2D array. The numbersstored with a node are theindices of the left and rightchildren of the node in thearray.