Trees PDF
Trees PDF
Binary Trees
Nature View of a Tree
leaves
branches
root
Computer Scientist’s View
root
leaves
branches
nodes
What is a Tree
• A tree is a finite nonempty
set of elements.
• It is an abstract model of a
hierarchical structure. Computers”R”Us
• consists of nodes with a
parent-child relation.
• Applications: Sales Manufacturing R&D
– Organization charts
– File systems
– Programming environments
US International Laptops Desktops
List Representation
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first, followed by a list of links to sub-trees
Data
• A node is represented by an
object storing ∅
– Element
– Parent node B
– Sequence of children nodes
∅ ∅
A D F
B
A D F
∅ ∅
C E
C E
Left Child, Right Sibling Representation
Data
Left Right
Child Sibling A
B C D
E F G H I
J K L
Tree Traversal
• Two main methods:
– Preorder
– Postorder
• Recursive definition
• Preorder:
– visit the root
– traverse in preorder the children (subtrees)
• Postorder
– traverse in postorder the children (subtrees)
– visit the root
Preorder Traversal
• A traversal visits the nodes of a tree in a Algorithm preOrder(v)
systematic manner
visit(v)
• In a preorder traversal, a node is visited
for each child w of v
before its descendants
preorder (w)
• Application: print a structured
document
1
Become Rich
2 5 9
1. Motivations 2. Methods 3. Success Stories
3 4 6 7 8
1.1 Enjoy 1.2 Help 2.1 Get a 2.2 Start a 2.3 Acquired
Life Poor Friends CS PhD Web Site by Google
Postorder Traversal
• In a postorder traversal, a node is Algorithm postOrder(v)
visited after its descendants
for each child w of v
• Application: compute space used by
postOrder (w)
files in a directory and its
subdirectories visit(v)
9
cs16/
8
3 7
todo.txt
homeworks/ programs/
1K
1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
Binary Tree
• A binary tree is a tree with the following Applications:
properties: arithmetic expressions
– Each internal node has at most two
children (degree of two) decision processes
– The children of a node are an ordered pair searching
H I
BinaryTree ADT
• The BinaryTree ADT extends • Update methods may be
the Tree ADT, i.e., it inherits defined by data structures
all the methods of the Tree implementing the BinaryTree
ADT ADT
• Additional methods:
– position leftChild(p)
– position rightChild(p)
– position sibling(p)
Examples of the Binary Tree
Skewed Binary Tree Complete Binary Tree
A 1 A
A
B B 2 B C
C
3 D E F G
D
E 4 H I
5
Differences Between A Tree and A Binary Tree
A A
B B
∅ ∅
B A D
A D ∅ ∅ ∅ ∅
C E
C E
Arithmetic Expression Tree
× ×
2 − 3 b
a 1
Decision Tree
• Binary tree associated with a decision process
– internal nodes: questions with yes/no answer
– external nodes: decisions
• Example: dining decision
Prove by induction.
k
k +1
∑ 2 i
= 2 −1
i =0
Relations between Number of
Leaf Nodes and Nodes of Degree 2
PROOF:
Let n and B denote the total number of nodes and branches in T.
Let n0, n1, n2 represent the nodes with no children, single child, and
two children respectively.
B+1=n
B=n1+2n2
n=n0+n1+n2
⇒ n1+2n2+1= n
⇒ n0=n2+1
Full Binary Tree
• A full binary tree of a given height k has 2k+1–1 nodes.
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Node Number Properties
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
• Right child of node i is node 2i+1, unless 2i+1 > n, where n is
the number of nodes.
• If 2i+1 > n, node i has no right child.
Complete Binary Trees
A labeled binary tree containing the labels 1 to n with root 1, branches
leading to nodes labeled 2 and 3, branches from these leading to 4, 5 and
6, 7, respectively, and so on.
A binary tree with n nodes and level k is complete iff its nodes
correspond to the nodes numbered from 1 to n in the full binary tree of
level k.
1 1
2 2 3
3
4 5 6 7 4 5 6 7
8 9 10 11 12 13 14 15
8 9
Complete binary tree Full binary tree of depth 3
Binary Tree Traversals
Let l, R, and r stand for moving left, visiting
the node, and moving right.
2 8
1 4 7 9
3 5
Print Arithmetic Expressions
• Specialization of an inorder traversal Algorithm inOrder (v)
– print operand or operator when if isInternal (v){
visiting node
– print “(“ before traversing left print(“(’’)
subtree inOrder (leftChild (v))}
– print “)“ after traversing right subtree
print(v.element ())
if isInternal (v){
inOrder (rightChild (v))
+ print (“)’’)}
× ×
a 1
Evaluate Arithmetic Expressions
× ×
2 − 3 2
5 1
Creativity:
pathLength(tree) = Σ depth(v) ∀v ∈ tree
Algorithm pathLength(v, n)
Input: a tree node v and an initial value n
Output: the pathLength of the tree with root v
Usage: pl = pathLength(root, 0);
if isExternal (v)
return n
return
(pathLength(leftChild (v), n + 1) +
pathLength(rightChild (v), n + 1) + n)
Euler Tour Traversal
• Generic traversal of a binary tree
• Includes a special cases the preorder, postorder and inorder traversals
• Walk around the tree and visit each node three times:
– on the left (preorder)
– from below (inorder)
– on the right (postorder)
L × R ×
B
2 − 3 2
5 1
Euler Tour Traversal
eulerTour(node v) {
perform action for visiting node on the left;
if v is internal then
eulerTour(v->left);
perform action for visiting node from below;
if v is internal then
eulerTour(v->right);
perform action for visiting node on the right;
}