SlideShare a Scribd company logo
1 of 199
Download to read offline
Data Structures
Trees
Andres Mendez-Vazquez
May 6, 2015
1 / 100
Images/cinvestav-
Outline
1 Trees: Basic Concepts
Subtrees, Leaves and Levels
2 Binary Trees
Basic Concepts
The Height Of a Binary Tree
Arithmetic Expressions
Properties
Numbering Nodes in A Full Binary Tree
Array Representation
Linked Representation
Traversals
2 / 100
Images/cinvestav-
Trees Structures
Linear lists are useful for serially ordered data.
(e0,e1,...,en−1)
Days of week.
Months in a year.
Students in this class.
Trees are useful for hierarchically ordered data
Employees of a corporation.
President, vice presidents, managers, and so on.
Java's classes.
Object is at the top of the hierarchy.
Subclasses of Object are next, and so on.
3 / 100
Images/cinvestav-
Trees Structures
Linear lists are useful for serially ordered data.
(e0,e1,...,en−1)
Days of week.
Months in a year.
Students in this class.
Trees are useful for hierarchically ordered data
Employees of a corporation.
President, vice presidents, managers, and so on.
Java's classes.
Object is at the top of the hierarchy.
Subclasses of Object are next, and so on.
3 / 100
Images/cinvestav-
Denition
First
A tree T is a nite nonempty set of elements.
Root
One of these elements is called the root.
The rest
The remaining elements, if any, are partitioned into trees, which are called
the subtrees of T.
4 / 100
Images/cinvestav-
Denition
First
A tree T is a nite nonempty set of elements.
Root
One of these elements is called the root.
The rest
The remaining elements, if any, are partitioned into trees, which are called
the subtrees of T.
4 / 100
Images/cinvestav-
Denition
First
A tree T is a nite nonempty set of elements.
Root
One of these elements is called the root.
The rest
The remaining elements, if any, are partitioned into trees, which are called
the subtrees of T.
4 / 100
Images/cinvestav-
For example
Class Structure
5 / 100
Images/cinvestav-
For example
Class Structure
ROOT
6 / 100
Images/cinvestav-
For example
Class Structure
ROOT
Children of The ROOT
7 / 100
Images/cinvestav-
For example
Class Structure
ROOT
Children of The ROOT
Grand Children of The ROOT
8 / 100
Images/cinvestav-
Outline
1 Trees: Basic Concepts
Subtrees, Leaves and Levels
2 Binary Trees
Basic Concepts
The Height Of a Binary Tree
Arithmetic Expressions
Properties
Numbering Nodes in A Full Binary Tree
Array Representation
Linked Representation
Traversals
9 / 100
Images/cinvestav-
SubTrees
We have the following
10 / 100
Images/cinvestav-
Leaves
We have the following
11 / 100
Images/cinvestav-
Levels
We have the following
Level 4
Level 1
Level 2
Level 3
12 / 100
Images/cinvestav-
Caution
BE AWARE
Some texts start level numbers at 0 rather than at 1.
Thus
Root is at level 0. Its children are at level 1.
The grand children of the root are at level 2.
And so on.
We will adapt to the following notation
Root is at level 1!!!
13 / 100
Images/cinvestav-
Caution
BE AWARE
Some texts start level numbers at 0 rather than at 1.
Thus
Root is at level 0. Its children are at level 1.
The grand children of the root are at level 2.
And so on.
We will adapt to the following notation
Root is at level 1!!!
13 / 100
Images/cinvestav-
Caution
BE AWARE
Some texts start level numbers at 0 rather than at 1.
Thus
Root is at level 0. Its children are at level 1.
The grand children of the root are at level 2.
And so on.
We will adapt to the following notation
Root is at level 1!!!
13 / 100
Images/cinvestav-
Caution
BE AWARE
Some texts start level numbers at 0 rather than at 1.
Thus
Root is at level 0. Its children are at level 1.
The grand children of the root are at level 2.
And so on.
We will adapt to the following notation
Root is at level 1!!!
13 / 100
Images/cinvestav-
Caution
BE AWARE
Some texts start level numbers at 0 rather than at 1.
Thus
Root is at level 0. Its children are at level 1.
The grand children of the root are at level 2.
And so on.
We will adapt to the following notation
Root is at level 1!!!
13 / 100
Images/cinvestav-
General Structure
AHHHH!!!!
14 / 100
Images/cinvestav-
Thus, we have the concept of height
Which is Height == Depth
Level 4
Level 1
Level 2
Level 3
15 / 100
Images/cinvestav-
In addition...
Node Degree == Number Of Children
3
2 1 1
00 1 0
0
16 / 100
Images/cinvestav-
Tree Degree == Max Node Degree
In our case 3
3
2 1 1
00 1 0
0
17 / 100
Images/cinvestav-
Outline
1 Trees: Basic Concepts
Subtrees, Leaves and Levels
2 Binary Trees
Basic Concepts
The Height Of a Binary Tree
Arithmetic Expressions
Properties
Numbering Nodes in A Full Binary Tree
Array Representation
Linked Representation
Traversals
18 / 100
Images/cinvestav-
Denition
We have that a Binary Tree is
A nite (possibly empty) collection of elements.
Second
1 A nonempty binary tree has a root element.
2 The remaining elements (if any) are partitioned into two binary trees.
3 These are called the left and right subtrees of the binary tree.
19 / 100
Images/cinvestav-
Denition
We have that a Binary Tree is
A nite (possibly empty) collection of elements.
Second
1 A nonempty binary tree has a root element.
2 The remaining elements (if any) are partitioned into two binary trees.
3 These are called the left and right subtrees of the binary tree.
19 / 100
Images/cinvestav-
Denition
We have that a Binary Tree is
A nite (possibly empty) collection of elements.
Second
1 A nonempty binary tree has a root element.
2 The remaining elements (if any) are partitioned into two binary trees.
3 These are called the left and right subtrees of the binary tree.
19 / 100
Images/cinvestav-
Denition
We have that a Binary Tree is
A nite (possibly empty) collection of elements.
Second
1 A nonempty binary tree has a root element.
2 The remaining elements (if any) are partitioned into two binary trees.
3 These are called the left and right subtrees of the binary tree.
19 / 100
Images/cinvestav-
Example
A Way to See Them
ROOT
20 / 100
Images/cinvestav-
Examples of Binary Trees
We have some examples
21 / 100
Images/cinvestav-
Dierences Between A Tree  A Binary Tree
First
No node in a binary tree may have a degree more than 2, whereas there is
no limit on the degree of a node in a tree.
Second
A binary tree may be empty; a tree cannot be empty.
22 / 100
Images/cinvestav-
Dierences Between A Tree  A Binary Tree
First
No node in a binary tree may have a degree more than 2, whereas there is
no limit on the degree of a node in a tree.
Second
A binary tree may be empty; a tree cannot be empty.
22 / 100
Images/cinvestav-
Outline
1 Trees: Basic Concepts
Subtrees, Leaves and Levels
2 Binary Trees
Basic Concepts
The Height Of a Binary Tree
Arithmetic Expressions
Properties
Numbering Nodes in A Full Binary Tree
Array Representation
Linked Representation
Traversals
23 / 100
Images/cinvestav-
The height of full or complete trees.
Something Notable
FULL Tree Height
1
Number Of Nodes
24 / 100
Images/cinvestav-
The height of full or complete trees.
Something Notable
FULL Tree Height
2
Number Of Nodes
25 / 100
Images/cinvestav-
The height of full or complete trees.
Something Notable
FULL Tree Height
3
Number Of Nodes
26 / 100
Images/cinvestav-
Now, if n is the number of nodes in a full tree,
Number of nodes
n = 2h
−1 (1)
Height of the tree
h = log2 (n +1) (2)
27 / 100
Images/cinvestav-
Now, if n is the number of nodes in a full tree,
Number of nodes
n = 2h
−1 (1)
Height of the tree
h = log2 (n +1) (2)
27 / 100
Images/cinvestav-
Outline
1 Trees: Basic Concepts
Subtrees, Leaves and Levels
2 Binary Trees
Basic Concepts
The Height Of a Binary Tree
Arithmetic Expressions
Properties
Numbering Nodes in A Full Binary Tree
Array Representation
Linked Representation
Traversals
28 / 100
Images/cinvestav-
Arithmetic Expressions
Imagine the following expressions
(a+b)∗(c +d)+ef /g ∗h +3.25 (3)
Operators
+,−,/,∗ (4)
Operands
a,b,c,d,e,f ,g,h,3.25,(a+b),(c +d),etc. (5)
29 / 100
Images/cinvestav-
Arithmetic Expressions
Imagine the following expressions
(a+b)∗(c +d)+ef /g ∗h +3.25 (3)
Operators
+,−,/,∗ (4)
Operands
a,b,c,d,e,f ,g,h,3.25,(a+b),(c +d),etc. (5)
29 / 100
Images/cinvestav-
Arithmetic Expressions
Imagine the following expressions
(a+b)∗(c +d)+ef /g ∗h +3.25 (3)
Operators
+,−,/,∗ (4)
Operands
a,b,c,d,e,f ,g,h,3.25,(a+b),(c +d),etc. (5)
29 / 100
Images/cinvestav-
In addition
Delimiters
(...) (6)
30 / 100
Images/cinvestav-
Operators Degree
Denition
Number of operands that the operator requires.
Binary operator requires two operands.
a + b
c / d
e - f
Unary operator requires one operand.
+ g
- h
31 / 100
Images/cinvestav-
Operators Degree
Denition
Number of operands that the operator requires.
Binary operator requires two operands.
a + b
c / d
e - f
Unary operator requires one operand.
+ g
- h
31 / 100
Images/cinvestav-
Operators Degree
Denition
Number of operands that the operator requires.
Binary operator requires two operands.
a + b
c / d
e - f
Unary operator requires one operand.
+ g
- h
31 / 100
Images/cinvestav-
About the Inx form
Something Notable
Normal way to write an expression.
Thus
Binary operators come in between their left and right operands.
Example
a * b
a + b * c a * b / c (a + b) * (c + d) + e  f/g*h + 3.25
32 / 100
Images/cinvestav-
About the Inx form
Something Notable
Normal way to write an expression.
Thus
Binary operators come in between their left and right operands.
Example
a * b
a + b * c a * b / c (a + b) * (c + d) + e  f/g*h + 3.25
32 / 100
Images/cinvestav-
About the Inx form
Something Notable
Normal way to write an expression.
Thus
Binary operators come in between their left and right operands.
Example
a * b
a + b * c a * b / c (a + b) * (c + d) + e  f/g*h + 3.25
32 / 100
Images/cinvestav-
Operator Priorities
How do you gure out the operands of an operator?
a + b * c
a * b + c / d
This is done by assigning operator priorities
priority(*) = priority(/)  priority(+) = priority(-)
Thus
When an operand lies between two operators, the operand associates with
the operator that has higher priority.
33 / 100
Images/cinvestav-
Operator Priorities
How do you gure out the operands of an operator?
a + b * c
a * b + c / d
This is done by assigning operator priorities
priority(*) = priority(/)  priority(+) = priority(-)
Thus
When an operand lies between two operators, the operand associates with
the operator that has higher priority.
33 / 100
Images/cinvestav-
Operator Priorities
How do you gure out the operands of an operator?
a + b * c
a * b + c / d
This is done by assigning operator priorities
priority(*) = priority(/)  priority(+) = priority(-)
Thus
When an operand lies between two operators, the operand associates with
the operator that has higher priority.
33 / 100
Images/cinvestav-
Tie Breakers
However this can happen
When an operand lies between two operators that have the same priority,
the operand associates with the operator on the left.
34 / 100
Images/cinvestav-
Delimiters
What about ()
Subexpression within delimiters is treated as a single operand, independent
from the remainder of the expression.
Example
(a + b) * (c  d) / (e  f)
35 / 100
Images/cinvestav-
Delimiters
What about ()
Subexpression within delimiters is treated as a single operand, independent
from the remainder of the expression.
Example
(a + b) * (c  d) / (e  f)
35 / 100
Images/cinvestav-
HOWEVER
Inx Expression Is Hard To Parse
Need operator priorities, tie breaker, and delimiters.
This makes computer evaluation more dicult than is necessary.
Postx and prex expression forms do not rely on operator priorities, a
tie breaker, or delimiters.
So it is easier for a computer to evaluate expressions that are in these
forms.
36 / 100
Images/cinvestav-
HOWEVER
Inx Expression Is Hard To Parse
Need operator priorities, tie breaker, and delimiters.
This makes computer evaluation more dicult than is necessary.
Postx and prex expression forms do not rely on operator priorities, a
tie breaker, or delimiters.
So it is easier for a computer to evaluate expressions that are in these
forms.
36 / 100
Images/cinvestav-
HOWEVER
Inx Expression Is Hard To Parse
Need operator priorities, tie breaker, and delimiters.
This makes computer evaluation more dicult than is necessary.
Postx and prex expression forms do not rely on operator priorities, a
tie breaker, or delimiters.
So it is easier for a computer to evaluate expressions that are in these
forms.
36 / 100
Images/cinvestav-
HOWEVER
Inx Expression Is Hard To Parse
Need operator priorities, tie breaker, and delimiters.
This makes computer evaluation more dicult than is necessary.
Postx and prex expression forms do not rely on operator priorities, a
tie breaker, or delimiters.
So it is easier for a computer to evaluate expressions that are in these
forms.
36 / 100
Images/cinvestav-
For example
4*(3+17)
*
4 +
3 17
37 / 100
Images/cinvestav-
Postx Form
We have that
The postx form of a variable or constant is the same as its inx form.
a, b, 3.25
Relative order
The relative order of operands is the same in inx and postx forms.
Thus
Operators come immediately after the postx form of their operands.
Inx = a + b
Postx = ab+
38 / 100
Images/cinvestav-
Postx Form
We have that
The postx form of a variable or constant is the same as its inx form.
a, b, 3.25
Relative order
The relative order of operands is the same in inx and postx forms.
Thus
Operators come immediately after the postx form of their operands.
Inx = a + b
Postx = ab+
38 / 100
Images/cinvestav-
Postx Form
We have that
The postx form of a variable or constant is the same as its inx form.
a, b, 3.25
Relative order
The relative order of operands is the same in inx and postx forms.
Thus
Operators come immediately after the postx form of their operands.
Inx = a + b
Postx = ab+
38 / 100
Images/cinvestav-
Example
1st One
Inx = a+b*c
Postx= abc*+
2nd One
Inx = a*b+c
Postx = ab*c+
3rd One
Inx = (a + b) * (c  d) / (e + f)
Postx = (ab)+cd-*ef+/
39 / 100
Images/cinvestav-
Example
1st One
Inx = a+b*c
Postx= abc*+
2nd One
Inx = a*b+c
Postx = ab*c+
3rd One
Inx = (a + b) * (c  d) / (e + f)
Postx = (ab)+cd-*ef+/
39 / 100
Images/cinvestav-
Example
1st One
Inx = a+b*c
Postx= abc*+
2nd One
Inx = a*b+c
Postx = ab*c+
3rd One
Inx = (a + b) * (c  d) / (e + f)
Postx = (ab)+cd-*ef+/
39 / 100
Images/cinvestav-
Example
1st One
Inx = a+b*c
Postx= abc*+
2nd One
Inx = a*b+c
Postx = ab*c+
3rd One
Inx = (a + b) * (c  d) / (e + f)
Postx = (ab)+cd-*ef+/
39 / 100
Images/cinvestav-
Example
1st One
Inx = a+b*c
Postx= abc*+
2nd One
Inx = a*b+c
Postx = ab*c+
3rd One
Inx = (a + b) * (c  d) / (e + f)
Postx = (ab)+cd-*ef+/
39 / 100
Images/cinvestav-
Example
1st One
Inx = a+b*c
Postx= abc*+
2nd One
Inx = a*b+c
Postx = ab*c+
3rd One
Inx = (a + b) * (c  d) / (e + f)
Postx = (ab)+cd-*ef+/
39 / 100
Images/cinvestav-
What about Unary Operators?
After all they need to be evaluated
Everything need to be interpreted!!!
Replace with new symbols
+ a = a @
+ a + b = a @ b +
- a = a ?
- a-b = a ? b -
40 / 100
Images/cinvestav-
What about Unary Operators?
After all they need to be evaluated
Everything need to be interpreted!!!
Replace with new symbols
+ a = a @
+ a + b = a @ b +
- a = a ?
- a-b = a ? b -
40 / 100
Images/cinvestav-
What about Unary Operators?
After all they need to be evaluated
Everything need to be interpreted!!!
Replace with new symbols
+ a = a @
+ a + b = a @ b +
- a = a ?
- a-b = a ? b -
40 / 100
Images/cinvestav-
What about Unary Operators?
After all they need to be evaluated
Everything need to be interpreted!!!
Replace with new symbols
+ a = a @
+ a + b = a @ b +
- a = a ?
- a-b = a ? b -
40 / 100
Images/cinvestav-
What about Unary Operators?
After all they need to be evaluated
Everything need to be interpreted!!!
Replace with new symbols
+ a = a @
+ a + b = a @ b +
- a = a ?
- a-b = a ? b -
40 / 100
Images/cinvestav-
Postx Evaluation
How do we do the evaluation?
Scan postx expression from left to right pushing operands on to a stack.
Then
When an operator is encountered, pop as many operands as this
operator needs.
Then, evaluate the operator.
Then, push the result on to the stack.
IMPORTANT
This works because, in postx, operators come immediately after their
operands.
41 / 100
Images/cinvestav-
Postx Evaluation
How do we do the evaluation?
Scan postx expression from left to right pushing operands on to a stack.
Then
When an operator is encountered, pop as many operands as this
operator needs.
Then, evaluate the operator.
Then, push the result on to the stack.
IMPORTANT
This works because, in postx, operators come immediately after their
operands.
41 / 100
Images/cinvestav-
Postx Evaluation
How do we do the evaluation?
Scan postx expression from left to right pushing operands on to a stack.
Then
When an operator is encountered, pop as many operands as this
operator needs.
Then, evaluate the operator.
Then, push the result on to the stack.
IMPORTANT
This works because, in postx, operators come immediately after their
operands.
41 / 100
Images/cinvestav-
Postx Evaluation
How do we do the evaluation?
Scan postx expression from left to right pushing operands on to a stack.
Then
When an operator is encountered, pop as many operands as this
operator needs.
Then, evaluate the operator.
Then, push the result on to the stack.
IMPORTANT
This works because, in postx, operators come immediately after their
operands.
41 / 100
Images/cinvestav-
Postx Evaluation
How do we do the evaluation?
Scan postx expression from left to right pushing operands on to a stack.
Then
When an operator is encountered, pop as many operands as this
operator needs.
Then, evaluate the operator.
Then, push the result on to the stack.
IMPORTANT
This works because, in postx, operators come immediately after their
operands.
41 / 100
Images/cinvestav-
Example: (a + b) * (c  d) / (e + f)
a b + c d - * e f + /
42 / 100
Images/cinvestav-
Example: (a + b) * (c  d) / (e + f)
a b + c d - * e f + /
43 / 100
Images/cinvestav-
Example: (a + b) * (c  d) / (e + f)
a b + c d - * e f + /
44 / 100
Images/cinvestav-
Example: (a + b) * (c  d) / (e + f)
a b + c d - * e f + /
45 / 100
Images/cinvestav-
Example: (a + b) * (c  d) / (e + f)
a b + c d - * e f + /
46 / 100
Images/cinvestav-
Example: (a + b) * (c  d) / (e + f)
a b + c d - * e f + /
47 / 100
Images/cinvestav-
Example: (a + b) * (c  d) / (e + f)
a b + c d - * e f + /
48 / 100
Images/cinvestav-
Prex Form
Here
The prex form of a variable or constant is the same as its inx form.
a, b, 3.25
Thus
The relative order of operands is the same in inx and prex forms.
Then
Operators come immediately before the prex form of their operands.
Inx = a + b
Postx = ab+
Prex = +ab
49 / 100
Images/cinvestav-
Prex Form
Here
The prex form of a variable or constant is the same as its inx form.
a, b, 3.25
Thus
The relative order of operands is the same in inx and prex forms.
Then
Operators come immediately before the prex form of their operands.
Inx = a + b
Postx = ab+
Prex = +ab
49 / 100
Images/cinvestav-
Prex Form
Here
The prex form of a variable or constant is the same as its inx form.
a, b, 3.25
Thus
The relative order of operands is the same in inx and prex forms.
Then
Operators come immediately before the prex form of their operands.
Inx = a + b
Postx = ab+
Prex = +ab
49 / 100
Images/cinvestav-
Prex Form
Here
The prex form of a variable or constant is the same as its inx form.
a, b, 3.25
Thus
The relative order of operands is the same in inx and prex forms.
Then
Operators come immediately before the prex form of their operands.
Inx = a + b
Postx = ab+
Prex = +ab
49 / 100
Images/cinvestav-
Prex Form
Here
The prex form of a variable or constant is the same as its inx form.
a, b, 3.25
Thus
The relative order of operands is the same in inx and prex forms.
Then
Operators come immediately before the prex form of their operands.
Inx = a + b
Postx = ab+
Prex = +ab
49 / 100
Images/cinvestav-
Prex Form
Here
The prex form of a variable or constant is the same as its inx form.
a, b, 3.25
Thus
The relative order of operands is the same in inx and prex forms.
Then
Operators come immediately before the prex form of their operands.
Inx = a + b
Postx = ab+
Prex = +ab
49 / 100
Images/cinvestav-
Prex Form
Here
The prex form of a variable or constant is the same as its inx form.
a, b, 3.25
Thus
The relative order of operands is the same in inx and prex forms.
Then
Operators come immediately before the prex form of their operands.
Inx = a + b
Postx = ab+
Prex = +ab
49 / 100
Images/cinvestav-
Then, we can use trees to represent operations
a+b
+
a b
-a
-
a
50 / 100
Images/cinvestav-
Then, we can use trees to represent operations
a+b
+
a b
-a
-
a
50 / 100
Images/cinvestav-
Example
(a + b) * (c  d) / (e + f)
51 / 100
Images/cinvestav-
Merits Of Binary Tree Form
We love them
The left and right operands are easy to visualize.
There are code optimization algorithms that work well with the binary
tree form of an expression.
We can use simple recursive evaluations on each expression to get the
results.
52 / 100
Images/cinvestav-
Merits Of Binary Tree Form
We love them
The left and right operands are easy to visualize.
There are code optimization algorithms that work well with the binary
tree form of an expression.
We can use simple recursive evaluations on each expression to get the
results.
52 / 100
Images/cinvestav-
Merits Of Binary Tree Form
We love them
The left and right operands are easy to visualize.
There are code optimization algorithms that work well with the binary
tree form of an expression.
We can use simple recursive evaluations on each expression to get the
results.
52 / 100
Images/cinvestav-
Outline
1 Trees: Basic Concepts
Subtrees, Leaves and Levels
2 Binary Trees
Basic Concepts
The Height Of a Binary Tree
Arithmetic Expressions
Properties
Numbering Nodes in A Full Binary Tree
Array Representation
Linked Representation
Traversals
53 / 100
Images/cinvestav-
Minimum Number Of Nodes
Question
Which is the minimum number of nodes in a binary tree whose height is h?
First
You have at least one node at each of the h levels.
Then
54 / 100
Images/cinvestav-
Minimum Number Of Nodes
Question
Which is the minimum number of nodes in a binary tree whose height is h?
First
You have at least one node at each of the h levels.
Then
54 / 100
Images/cinvestav-
Minimum Number Of Nodes
Question
Which is the minimum number of nodes in a binary tree whose height is h?
First
You have at least one node at each of the h levels.
Then
54 / 100
Images/cinvestav-
Maximum Number Of Nodes
Here, all possible nodes at rst h levels are present.
Here, we have that
1+2+4+8+...+2h−1
=
2h −1
2−1
= 2h
−1 (7)
55 / 100
Images/cinvestav-
Maximum Number Of Nodes
Here, all possible nodes at rst h levels are present.
Here, we have that
1+2+4+8+...+2h−1
=
2h −1
2−1
= 2h
−1 (7)
55 / 100
Images/cinvestav-
Number Of Nodes  Height
We have that
Let n be the number of nodes in a binary tree whose height is h.
Then
h ≤ n ≤ 2h
−1 (8)
Thus
log2 (n +1) ≤ h ⇒ log2 (n +1) ≤ h ≤ n (9)
56 / 100
Images/cinvestav-
Number Of Nodes  Height
We have that
Let n be the number of nodes in a binary tree whose height is h.
Then
h ≤ n ≤ 2h
−1 (8)
Thus
log2 (n +1) ≤ h ⇒ log2 (n +1) ≤ h ≤ n (9)
56 / 100
Images/cinvestav-
Number Of Nodes  Height
We have that
Let n be the number of nodes in a binary tree whose height is h.
Then
h ≤ n ≤ 2h
−1 (8)
Thus
log2 (n +1) ≤ h ⇒ log2 (n +1) ≤ h ≤ n (9)
56 / 100
Images/cinvestav-
Full Binary Tree
A full binary tree of a given height h has 2
h1 nodes.
57 / 100
Images/cinvestav-
Outline
1 Trees: Basic Concepts
Subtrees, Leaves and Levels
2 Binary Trees
Basic Concepts
The Height Of a Binary Tree
Arithmetic Expressions
Properties
Numbering Nodes in A Full Binary Tree
Array Representation
Linked Representation
Traversals
58 / 100
Images/cinvestav-
Numbering Nodes In A Full Binary Tree
It is possible to do the following
1 Number the nodes 1 through 2h  1.
2 Number by levels from top to bottom.
3 Within a level number from left to right.
We get the following
59 / 100
Images/cinvestav-
Numbering Nodes In A Full Binary Tree
It is possible to do the following
1 Number the nodes 1 through 2h  1.
2 Number by levels from top to bottom.
3 Within a level number from left to right.
We get the following
59 / 100
Images/cinvestav-
Numbering Nodes In A Full Binary Tree
It is possible to do the following
1 Number the nodes 1 through 2h  1.
2 Number by levels from top to bottom.
3 Within a level number from left to right.
We get the following
59 / 100
Images/cinvestav-
Numbering Nodes In A Full Binary Tree
It is possible to do the following
1 Number the nodes 1 through 2h  1.
2 Number by levels from top to bottom.
3 Within a level number from left to right.
We get the following
59 / 100
Images/cinvestav-
Node Number Properties
We have the following
The parent of node i is node i/2 , unless i = 1.
Node 1 is the root and has no parent.
What about the children
Left child of node i is node 2i, unless 2i  n, where n is the number of
nodes.
If 2i  n, node i has no left child.
60 / 100
Images/cinvestav-
Node Number Properties
We have the following
The parent of node i is node i/2 , unless i = 1.
Node 1 is the root and has no parent.
What about the children
Left child of node i is node 2i, unless 2i  n, where n is the number of
nodes.
If 2i  n, node i has no left child.
60 / 100
Images/cinvestav-
Node Number Properties
We have the following
The parent of node i is node i/2 , unless i = 1.
Node 1 is the root and has no parent.
What about the children
Left child of node i is node 2i, unless 2i  n, where n is the number of
nodes.
If 2i  n, node i has no left child.
60 / 100
Images/cinvestav-
Node Number Properties
We have the following
The parent of node i is node i/2 , unless i = 1.
Node 1 is the root and has no parent.
What about the children
Left child of node i is node 2i, unless 2i  n, where n is the number of
nodes.
If 2i  n, node i has no left child.
60 / 100
Images/cinvestav-
After all
We get the following
61 / 100
Images/cinvestav-
What about the right Children?
We have the following
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.
62 / 100
Images/cinvestav-
What about the right Children?
We have the following
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.
62 / 100
Images/cinvestav-
Complete Binary Tree With n Nodes
Dention
1 It starts with a full binary tree that has at least n nodes.
2 Number the nodes as described earlier.
3 The binary tree dened by the nodes numbered 1 through n is the
unique n node complete binary tree.
63 / 100
Images/cinvestav-
Complete Binary Tree With n Nodes
Dention
1 It starts with a full binary tree that has at least n nodes.
2 Number the nodes as described earlier.
3 The binary tree dened by the nodes numbered 1 through n is the
unique n node complete binary tree.
63 / 100
Images/cinvestav-
Complete Binary Tree With n Nodes
Dention
1 It starts with a full binary tree that has at least n nodes.
2 Number the nodes as described earlier.
3 The binary tree dened by the nodes numbered 1 through n is the
unique n node complete binary tree.
63 / 100
Images/cinvestav-
Example
Complete binary tree with 10 nodes.
64 / 100
Images/cinvestav-
How we represent them?
We have two ways
Array Representation
Linked Representation
65 / 100
Images/cinvestav-
Outline
1 Trees: Basic Concepts
Subtrees, Leaves and Levels
2 Binary Trees
Basic Concepts
The Height Of a Binary Tree
Arithmetic Expressions
Properties
Numbering Nodes in A Full Binary Tree
Array Representation
Linked Representation
Traversals
66 / 100
Images/cinvestav-
Array Representation
We do the following
Number the nodes using the numbering scheme for a full binary tree. The
node that is numbered i is stored in tree[i].
Then
67 / 100
Images/cinvestav-
Array Representation
We do the following
Number the nodes using the numbering scheme for a full binary tree. The
node that is numbered i is stored in tree[i].
Then
67 / 100
Images/cinvestav-
What if we have something like this
Right-Skewed Binary Tree
What a waste of memory!!!
68 / 100
Images/cinvestav-
What if we have something like this
Right-Skewed Binary Tree
What a waste of memory!!!
68 / 100
Images/cinvestav-
Outline
1 Trees: Basic Concepts
Subtrees, Leaves and Levels
2 Binary Trees
Basic Concepts
The Height Of a Binary Tree
Arithmetic Expressions
Properties
Numbering Nodes in A Full Binary Tree
Array Representation
Linked Representation
Traversals
69 / 100
Images/cinvestav-
What can we do?
Linked Representation
Each binary tree node is represented as an object whose data type is
BinaryTreeNode.
The space required by an n node binary tree is n * (space required by
one node).
70 / 100
Images/cinvestav-
What can we do?
Linked Representation
Each binary tree node is represented as an object whose data type is
BinaryTreeNode.
The space required by an n node binary tree is n * (space required by
one node).
70 / 100
Images/cinvestav-
Code for a Node
We have then with Generic Key and Item
p a c k a g e T r e e ;
p u b l i c c l a s s B i n a r y T r e e N o d e
{
Key k e y ;
I t e m e l e m e n t ;
B i n a r y T r e e N o d e l e f t C h i l d ; // l e f t subtree
B i n a r y T r e e N o d e r i g h t C h i l d ; // r i g h t subtree
// c o n s t r u c t o r s and any other methods
// come here
}
71 / 100
Images/cinvestav-
Example
Look at this
72 / 100
Images/cinvestav-
Which Operations
We might want to have
Determine the height.
Determine the number of nodes.
Make a clone.
Determine if two binary trees are clones.
Maybe, we want
Display the binary tree.
Evaluate the arithmetic expression represented by a binary tree.
Finally, we want
Obtain the inx form of an expression.
Obtain the prex form of an expression.
Obtain the postx form of an expression.
73 / 100
Images/cinvestav-
Which Operations
We might want to have
Determine the height.
Determine the number of nodes.
Make a clone.
Determine if two binary trees are clones.
Maybe, we want
Display the binary tree.
Evaluate the arithmetic expression represented by a binary tree.
Finally, we want
Obtain the inx form of an expression.
Obtain the prex form of an expression.
Obtain the postx form of an expression.
73 / 100
Images/cinvestav-
Which Operations
We might want to have
Determine the height.
Determine the number of nodes.
Make a clone.
Determine if two binary trees are clones.
Maybe, we want
Display the binary tree.
Evaluate the arithmetic expression represented by a binary tree.
Finally, we want
Obtain the inx form of an expression.
Obtain the prex form of an expression.
Obtain the postx form of an expression.
73 / 100
Images/cinvestav-
Which Operations
We might want to have
Determine the height.
Determine the number of nodes.
Make a clone.
Determine if two binary trees are clones.
Maybe, we want
Display the binary tree.
Evaluate the arithmetic expression represented by a binary tree.
Finally, we want
Obtain the inx form of an expression.
Obtain the prex form of an expression.
Obtain the postx form of an expression.
73 / 100
Images/cinvestav-
Which Operations
We might want to have
Determine the height.
Determine the number of nodes.
Make a clone.
Determine if two binary trees are clones.
Maybe, we want
Display the binary tree.
Evaluate the arithmetic expression represented by a binary tree.
Finally, we want
Obtain the inx form of an expression.
Obtain the prex form of an expression.
Obtain the postx form of an expression.
73 / 100
Images/cinvestav-
Which Operations
We might want to have
Determine the height.
Determine the number of nodes.
Make a clone.
Determine if two binary trees are clones.
Maybe, we want
Display the binary tree.
Evaluate the arithmetic expression represented by a binary tree.
Finally, we want
Obtain the inx form of an expression.
Obtain the prex form of an expression.
Obtain the postx form of an expression.
73 / 100
Images/cinvestav-
Which Operations
We might want to have
Determine the height.
Determine the number of nodes.
Make a clone.
Determine if two binary trees are clones.
Maybe, we want
Display the binary tree.
Evaluate the arithmetic expression represented by a binary tree.
Finally, we want
Obtain the inx form of an expression.
Obtain the prex form of an expression.
Obtain the postx form of an expression.
73 / 100
Images/cinvestav-
Which Operations
We might want to have
Determine the height.
Determine the number of nodes.
Make a clone.
Determine if two binary trees are clones.
Maybe, we want
Display the binary tree.
Evaluate the arithmetic expression represented by a binary tree.
Finally, we want
Obtain the inx form of an expression.
Obtain the prex form of an expression.
Obtain the postx form of an expression.
73 / 100
Images/cinvestav-
Which Operations
We might want to have
Determine the height.
Determine the number of nodes.
Make a clone.
Determine if two binary trees are clones.
Maybe, we want
Display the binary tree.
Evaluate the arithmetic expression represented by a binary tree.
Finally, we want
Obtain the inx form of an expression.
Obtain the prex form of an expression.
Obtain the postx form of an expression.
73 / 100
Images/cinvestav-
Outline
1 Trees: Basic Concepts
Subtrees, Leaves and Levels
2 Binary Trees
Basic Concepts
The Height Of a Binary Tree
Arithmetic Expressions
Properties
Numbering Nodes in A Full Binary Tree
Array Representation
Linked Representation
Traversals
74 / 100
Images/cinvestav-
Traversals
Something Notable
Many binary tree operations are done by performing a traversal of the
binary tree.
Actually
In a traversal, each element of the binary tree is visited exactly once.
Actions
During the visit of an element, all action (make a clone, display, evaluate
the operator, etc.) with respect to this element is taken.
75 / 100
Images/cinvestav-
Traversals
Something Notable
Many binary tree operations are done by performing a traversal of the
binary tree.
Actually
In a traversal, each element of the binary tree is visited exactly once.
Actions
During the visit of an element, all action (make a clone, display, evaluate
the operator, etc.) with respect to this element is taken.
75 / 100
Images/cinvestav-
Traversals
Something Notable
Many binary tree operations are done by performing a traversal of the
binary tree.
Actually
In a traversal, each element of the binary tree is visited exactly once.
Actions
During the visit of an element, all action (make a clone, display, evaluate
the operator, etc.) with respect to this element is taken.
75 / 100
Images/cinvestav-
Binary Tree Traversal Methods
Thus, we have
Preorder
Inorder
Postorder
Level order
76 / 100
Images/cinvestav-
Binary Tree Traversal Methods
Thus, we have
Preorder
Inorder
Postorder
Level order
76 / 100
Images/cinvestav-
Binary Tree Traversal Methods
Thus, we have
Preorder
Inorder
Postorder
Level order
76 / 100
Images/cinvestav-
Binary Tree Traversal Methods
Thus, we have
Preorder
Inorder
Postorder
Level order
76 / 100
Images/cinvestav-
Remarks
First
In a traversal of a binary tree, each element of the binary tree is visited
exactly once.
Second
During the visit of an element, all action (make a clone, display, evaluate
the operator, etc.) with respect to this element is taken.
77 / 100
Images/cinvestav-
Remarks
First
In a traversal of a binary tree, each element of the binary tree is visited
exactly once.
Second
During the visit of an element, all action (make a clone, display, evaluate
the operator, etc.) with respect to this element is taken.
77 / 100
Images/cinvestav-
Preorder Traversals
Code
p u b l i c s t a t i c v o i d p r e O r d e r ( B i n a r y T r e e N o d e t )
{
i f ( t != n u l l )
{
v i s i t ( t ) ;
p r e O r d e r ( t . l e f t C h i l d ) ;
p r e O r d e r ( t . r i g h t C h i l d ) ;
}
}
78 / 100
Images/cinvestav-
Example I : (visit = print)
Something Notable
79 / 100
Images/cinvestav-
Example II : (visit = print)
Something Notable
80 / 100
Images/cinvestav-
Actually the preorder can be used to express the prex
form!!!
Example
81 / 100
Images/cinvestav-
Inorder Traversals
Code
p u b l i c s t a t i c v o i d i n O r d e r ( B i n a r y T r e e N o d e t )
{
i f ( t != n u l l )
{
i n O r d e r ( t . l e f t C h i l d ) ;
v i s i t ( t ) ;
i n O r d e r ( t . r i g h t C h i l d ) ;
}
}
82 / 100
Images/cinvestav-
Example I : (visit = print)
Something Notable
83 / 100
Images/cinvestav-
Example II : (visit = print)
Something Notable
84 / 100
Images/cinvestav-
Example III : (visit = print)
Inorder By Projection (Squishing)
85 / 100
Images/cinvestav-
Inorder Of Expression Tree
Example
Not only you get the inx form
But, we have a implicit representation of the parenthesis
86 / 100
Images/cinvestav-
Inorder Of Expression Tree
Example
Not only you get the inx form
But, we have a implicit representation of the parenthesis
86 / 100
Images/cinvestav-
Postorder Traversal
Code
p u b l i c s t a t i c v o i d p o s t O r d e r ( B i n a r y T r e e N o d e t )
{
i f ( t != n u l l )
{
p o s t O r d e r ( t . l e f t C h i l d ) ;
p o s t O r d e r ( t . r i g h t C h i l d ) ;
v i s i t ( t ) ;
}
}
87 / 100
Images/cinvestav-
Example I : (visit = print)
Something Notable
88 / 100
Images/cinvestav-
Example II : (visit = print)
Something Notable
89 / 100
Images/cinvestav-
Actually the preorder can be used to express the postx
form!!!
Example
90 / 100
Images/cinvestav-
Traversal Applications
First One
Make a Clone
Second
Determine the height.
Third
Determine number of nodes.
91 / 100
Images/cinvestav-
Traversal Applications
First One
Make a Clone
Second
Determine the height.
Third
Determine number of nodes.
91 / 100
Images/cinvestav-
Traversal Applications
First One
Make a Clone
Second
Determine the height.
Third
Determine number of nodes.
91 / 100
Images/cinvestav-
Now
What if we want to traverse each node in every level rst - level order
92 / 100
Images/cinvestav-
Level Order
Code
Which one?
The code
93 / 100
Images/cinvestav-
Level Order
Code
Which one?
The code
L e t t be t h e t r e e r o o t .
w h i l e ( t != n u l l )
{
v i s i t t and p u t i t s c h i l d r e n on a FIFO queue ;
remove a node from t h e FIFO queue and c a l l i t t ;
// remove r e t u r n s n u l l when queue i s empty
}
93 / 100
Images/cinvestav-
Binary Tree Construction
Question
Can you construct the binary tree, given two traversal sequences?
However
It depends on which two sequences are given.
Properties
94 / 100
Images/cinvestav-
Binary Tree Construction
Question
Can you construct the binary tree, given two traversal sequences?
However
It depends on which two sequences are given.
Properties
94 / 100
Images/cinvestav-
Binary Tree Construction
Question
Can you construct the binary tree, given two traversal sequences?
However
It depends on which two sequences are given.
Properties
94 / 100
Images/cinvestav-
Preorder And Postorder
If we have the following
Something Notable
Preorder = ab
Postorder = ba
We have a problem
Preorder and postorder do not uniquely dene a binary tree.
Nor do preorder and level order (same example).
Nor do postorder and level order (same example).
95 / 100
Images/cinvestav-
Preorder And Postorder
If we have the following
Something Notable
Preorder = ab
Postorder = ba
We have a problem
Preorder and postorder do not uniquely dene a binary tree.
Nor do preorder and level order (same example).
Nor do postorder and level order (same example).
95 / 100
Images/cinvestav-
Preorder And Postorder
If we have the following
Something Notable
Preorder = ab
Postorder = ba
We have a problem
Preorder and postorder do not uniquely dene a binary tree.
Nor do preorder and level order (same example).
Nor do postorder and level order (same example).
95 / 100
Images/cinvestav-
Preorder And Postorder
If we have the following
Something Notable
Preorder = ab
Postorder = ba
We have a problem
Preorder and postorder do not uniquely dene a binary tree.
Nor do preorder and level order (same example).
Nor do postorder and level order (same example).
95 / 100
Images/cinvestav-
Preorder And Postorder
If we have the following
Something Notable
Preorder = ab
Postorder = ba
We have a problem
Preorder and postorder do not uniquely dene a binary tree.
Nor do preorder and level order (same example).
Nor do postorder and level order (same example).
95 / 100
Images/cinvestav-
Preorder And Postorder
If we have the following
Something Notable
Preorder = ab
Postorder = ba
We have a problem
Preorder and postorder do not uniquely dene a binary tree.
Nor do preorder and level order (same example).
Nor do postorder and level order (same example).
95 / 100
Images/cinvestav-
What if we have Inorder And Preorder
Example
inorder = g d h b e i a f j c
preorder = a b d g h e i c f j
Process
Scan the preorder left to right using the inorder to separate left and right
subtrees.
Thus
a is the root of the tree; gdhbei are in the left subtree; fjc are in the right
subtree.
96 / 100
Images/cinvestav-
What if we have Inorder And Preorder
Example
inorder = g d h b e i a f j c
preorder = a b d g h e i c f j
Process
Scan the preorder left to right using the inorder to separate left and right
subtrees.
Thus
a is the root of the tree; gdhbei are in the left subtree; fjc are in the right
subtree.
96 / 100
Images/cinvestav-
What if we have Inorder And Preorder
Example
inorder = g d h b e i a f j c
preorder = a b d g h e i c f j
Process
Scan the preorder left to right using the inorder to separate left and right
subtrees.
Thus
a is the root of the tree; gdhbei are in the left subtree; fjc are in the right
subtree.
96 / 100
Images/cinvestav-
Then
First
preorder = a b d g h e i c f j
97 / 100
Images/cinvestav-
Then
First
preorder = a b d g h e i c f j
97 / 100
Images/cinvestav-
Cont...
d is the next root; g is in the left subtree; h is in the right subtree.
98 / 100
Images/cinvestav-
Inorder And Postorder
Thus
Scan postorder from right to left using inorder to separate left and right
subtrees.
Example
inorder = g d h b e i a f j c
postorder = g h d i e b j f c a
Thus
Tree root is a; gdhbei are in left subtree; fjc are in right subtree.
99 / 100
Images/cinvestav-
Inorder And Postorder
Thus
Scan postorder from right to left using inorder to separate left and right
subtrees.
Example
inorder = g d h b e i a f j c
postorder = g h d i e b j f c a
Thus
Tree root is a; gdhbei are in left subtree; fjc are in right subtree.
99 / 100
Images/cinvestav-
Inorder And Postorder
Thus
Scan postorder from right to left using inorder to separate left and right
subtrees.
Example
inorder = g d h b e i a f j c
postorder = g h d i e b j f c a
Thus
Tree root is a; gdhbei are in left subtree; fjc are in right subtree.
99 / 100
Images/cinvestav-
Inorder And Postorder
Thus
Scan postorder from right to left using inorder to separate left and right
subtrees.
Example
inorder = g d h b e i a f j c
postorder = g h d i e b j f c a
Thus
Tree root is a; gdhbei are in left subtree; fjc are in right subtree.
99 / 100
Images/cinvestav-
Inorder And Level Order
Thus
Scan level order from left to right using inorder to separate left and right
subtrees.
Example
inorder = g d h b e i a f j c
level order = a b c d e f g h i j
Thus
Tree root is a; gdhbei are in left subtree; fjc are in right subtree.
100 / 100
Images/cinvestav-
Inorder And Level Order
Thus
Scan level order from left to right using inorder to separate left and right
subtrees.
Example
inorder = g d h b e i a f j c
level order = a b c d e f g h i j
Thus
Tree root is a; gdhbei are in left subtree; fjc are in right subtree.
100 / 100
Images/cinvestav-
Inorder And Level Order
Thus
Scan level order from left to right using inorder to separate left and right
subtrees.
Example
inorder = g d h b e i a f j c
level order = a b c d e f g h i j
Thus
Tree root is a; gdhbei are in left subtree; fjc are in right subtree.
100 / 100
Images/cinvestav-
Inorder And Level Order
Thus
Scan level order from left to right using inorder to separate left and right
subtrees.
Example
inorder = g d h b e i a f j c
level order = a b c d e f g h i j
Thus
Tree root is a; gdhbei are in left subtree; fjc are in right subtree.
100 / 100

More Related Content

What's hot

Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Kumar Boro
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
Mohd Arif
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
Rafal Edward
 

What's hot (20)

17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Review session2
Review session2Review session2
Review session2
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their Representations
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Tree
TreeTree
Tree
 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
 
Array
ArrayArray
Array
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
R Basics
R BasicsR Basics
R Basics
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
Array Data Structures
Array Data StructuresArray Data Structures
Array Data Structures
 

Similar to Preparation Data Structures 10 trees

Abstract idea of a tree
Abstract idea of a treeAbstract idea of a tree
Abstract idea of a tree
kavitaSR
 

Similar to Preparation Data Structures 10 trees (20)

Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Trees
TreesTrees
Trees
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
 
DSA IV Unit.pptx
DSA IV Unit.pptxDSA IV Unit.pptx
DSA IV Unit.pptx
 
Abstract idea of a tree
Abstract idea of a treeAbstract idea of a tree
Abstract idea of a tree
 
AD3251-Data Structures Design-Notes-Tree.pdf
AD3251-Data Structures  Design-Notes-Tree.pdfAD3251-Data Structures  Design-Notes-Tree.pdf
AD3251-Data Structures Design-Notes-Tree.pdf
 
Binary tree
Binary treeBinary tree
Binary tree
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Tree
TreeTree
Tree
 
Unit III - NON LINEAR DATA STRUCTURES
Unit III -  NON LINEAR DATA STRUCTURESUnit III -  NON LINEAR DATA STRUCTURES
Unit III - NON LINEAR DATA STRUCTURES
 
06 segment trees
06 segment trees06 segment trees
06 segment trees
 
B.TECH Math project
B.TECH Math projectB.TECH Math project
B.TECH Math project
 
Binary trees
Binary treesBinary trees
Binary trees
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
data structures module III & IV.pptx
data structures module III & IV.pptxdata structures module III & IV.pptx
data structures module III & IV.pptx
 

More from Andres Mendez-Vazquez

Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabus
Andres Mendez-Vazquez
 

More from Andres Mendez-Vazquez (20)

2.03 bayesian estimation
2.03 bayesian estimation2.03 bayesian estimation
2.03 bayesian estimation
 
05 linear transformations
05 linear transformations05 linear transformations
05 linear transformations
 
01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues
 
01.02 linear equations
01.02 linear equations01.02 linear equations
01.02 linear equations
 
01.01 vector spaces
01.01 vector spaces01.01 vector spaces
01.01 vector spaces
 
06 recurrent neural_networks
06 recurrent neural_networks06 recurrent neural_networks
06 recurrent neural_networks
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation
 
Zetta global
Zetta globalZetta global
Zetta global
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learning
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning Syllabus
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabus
 
Ideas 09 22_2018
Ideas 09 22_2018Ideas 09 22_2018
Ideas 09 22_2018
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data Sciences
 
Analysis of Algorithms Syllabus
Analysis of Algorithms  SyllabusAnalysis of Algorithms  Syllabus
Analysis of Algorithms Syllabus
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations
 
18.1 combining models
18.1 combining models18.1 combining models
18.1 combining models
 
17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension
 
A basic introduction to learning
A basic introduction to learningA basic introduction to learning
A basic introduction to learning
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

Preparation Data Structures 10 trees

  • 2. Images/cinvestav- Outline 1 Trees: Basic Concepts Subtrees, Leaves and Levels 2 Binary Trees Basic Concepts The Height Of a Binary Tree Arithmetic Expressions Properties Numbering Nodes in A Full Binary Tree Array Representation Linked Representation Traversals 2 / 100
  • 3. Images/cinvestav- Trees Structures Linear lists are useful for serially ordered data. (e0,e1,...,en−1) Days of week. Months in a year. Students in this class. Trees are useful for hierarchically ordered data Employees of a corporation. President, vice presidents, managers, and so on. Java's classes. Object is at the top of the hierarchy. Subclasses of Object are next, and so on. 3 / 100
  • 4. Images/cinvestav- Trees Structures Linear lists are useful for serially ordered data. (e0,e1,...,en−1) Days of week. Months in a year. Students in this class. Trees are useful for hierarchically ordered data Employees of a corporation. President, vice presidents, managers, and so on. Java's classes. Object is at the top of the hierarchy. Subclasses of Object are next, and so on. 3 / 100
  • 5. Images/cinvestav- Denition First A tree T is a nite nonempty set of elements. Root One of these elements is called the root. The rest The remaining elements, if any, are partitioned into trees, which are called the subtrees of T. 4 / 100
  • 6. Images/cinvestav- Denition First A tree T is a nite nonempty set of elements. Root One of these elements is called the root. The rest The remaining elements, if any, are partitioned into trees, which are called the subtrees of T. 4 / 100
  • 7. Images/cinvestav- Denition First A tree T is a nite nonempty set of elements. Root One of these elements is called the root. The rest The remaining elements, if any, are partitioned into trees, which are called the subtrees of T. 4 / 100
  • 11. Images/cinvestav- For example Class Structure ROOT Children of The ROOT Grand Children of The ROOT 8 / 100
  • 12. Images/cinvestav- Outline 1 Trees: Basic Concepts Subtrees, Leaves and Levels 2 Binary Trees Basic Concepts The Height Of a Binary Tree Arithmetic Expressions Properties Numbering Nodes in A Full Binary Tree Array Representation Linked Representation Traversals 9 / 100
  • 15. Images/cinvestav- Levels We have the following Level 4 Level 1 Level 2 Level 3 12 / 100
  • 16. Images/cinvestav- Caution BE AWARE Some texts start level numbers at 0 rather than at 1. Thus Root is at level 0. Its children are at level 1. The grand children of the root are at level 2. And so on. We will adapt to the following notation Root is at level 1!!! 13 / 100
  • 17. Images/cinvestav- Caution BE AWARE Some texts start level numbers at 0 rather than at 1. Thus Root is at level 0. Its children are at level 1. The grand children of the root are at level 2. And so on. We will adapt to the following notation Root is at level 1!!! 13 / 100
  • 18. Images/cinvestav- Caution BE AWARE Some texts start level numbers at 0 rather than at 1. Thus Root is at level 0. Its children are at level 1. The grand children of the root are at level 2. And so on. We will adapt to the following notation Root is at level 1!!! 13 / 100
  • 19. Images/cinvestav- Caution BE AWARE Some texts start level numbers at 0 rather than at 1. Thus Root is at level 0. Its children are at level 1. The grand children of the root are at level 2. And so on. We will adapt to the following notation Root is at level 1!!! 13 / 100
  • 20. Images/cinvestav- Caution BE AWARE Some texts start level numbers at 0 rather than at 1. Thus Root is at level 0. Its children are at level 1. The grand children of the root are at level 2. And so on. We will adapt to the following notation Root is at level 1!!! 13 / 100
  • 22. Images/cinvestav- Thus, we have the concept of height Which is Height == Depth Level 4 Level 1 Level 2 Level 3 15 / 100
  • 23. Images/cinvestav- In addition... Node Degree == Number Of Children 3 2 1 1 00 1 0 0 16 / 100
  • 24. Images/cinvestav- Tree Degree == Max Node Degree In our case 3 3 2 1 1 00 1 0 0 17 / 100
  • 25. Images/cinvestav- Outline 1 Trees: Basic Concepts Subtrees, Leaves and Levels 2 Binary Trees Basic Concepts The Height Of a Binary Tree Arithmetic Expressions Properties Numbering Nodes in A Full Binary Tree Array Representation Linked Representation Traversals 18 / 100
  • 26. Images/cinvestav- Denition We have that a Binary Tree is A nite (possibly empty) collection of elements. Second 1 A nonempty binary tree has a root element. 2 The remaining elements (if any) are partitioned into two binary trees. 3 These are called the left and right subtrees of the binary tree. 19 / 100
  • 27. Images/cinvestav- Denition We have that a Binary Tree is A nite (possibly empty) collection of elements. Second 1 A nonempty binary tree has a root element. 2 The remaining elements (if any) are partitioned into two binary trees. 3 These are called the left and right subtrees of the binary tree. 19 / 100
  • 28. Images/cinvestav- Denition We have that a Binary Tree is A nite (possibly empty) collection of elements. Second 1 A nonempty binary tree has a root element. 2 The remaining elements (if any) are partitioned into two binary trees. 3 These are called the left and right subtrees of the binary tree. 19 / 100
  • 29. Images/cinvestav- Denition We have that a Binary Tree is A nite (possibly empty) collection of elements. Second 1 A nonempty binary tree has a root element. 2 The remaining elements (if any) are partitioned into two binary trees. 3 These are called the left and right subtrees of the binary tree. 19 / 100
  • 30. Images/cinvestav- Example A Way to See Them ROOT 20 / 100
  • 31. Images/cinvestav- Examples of Binary Trees We have some examples 21 / 100
  • 32. Images/cinvestav- Dierences Between A Tree A Binary Tree First No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. Second A binary tree may be empty; a tree cannot be empty. 22 / 100
  • 33. Images/cinvestav- Dierences Between A Tree A Binary Tree First No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. Second A binary tree may be empty; a tree cannot be empty. 22 / 100
  • 34. Images/cinvestav- Outline 1 Trees: Basic Concepts Subtrees, Leaves and Levels 2 Binary Trees Basic Concepts The Height Of a Binary Tree Arithmetic Expressions Properties Numbering Nodes in A Full Binary Tree Array Representation Linked Representation Traversals 23 / 100
  • 35. Images/cinvestav- The height of full or complete trees. Something Notable FULL Tree Height 1 Number Of Nodes 24 / 100
  • 36. Images/cinvestav- The height of full or complete trees. Something Notable FULL Tree Height 2 Number Of Nodes 25 / 100
  • 37. Images/cinvestav- The height of full or complete trees. Something Notable FULL Tree Height 3 Number Of Nodes 26 / 100
  • 38. Images/cinvestav- Now, if n is the number of nodes in a full tree, Number of nodes n = 2h −1 (1) Height of the tree h = log2 (n +1) (2) 27 / 100
  • 39. Images/cinvestav- Now, if n is the number of nodes in a full tree, Number of nodes n = 2h −1 (1) Height of the tree h = log2 (n +1) (2) 27 / 100
  • 40. Images/cinvestav- Outline 1 Trees: Basic Concepts Subtrees, Leaves and Levels 2 Binary Trees Basic Concepts The Height Of a Binary Tree Arithmetic Expressions Properties Numbering Nodes in A Full Binary Tree Array Representation Linked Representation Traversals 28 / 100
  • 41. Images/cinvestav- Arithmetic Expressions Imagine the following expressions (a+b)∗(c +d)+ef /g ∗h +3.25 (3) Operators +,−,/,∗ (4) Operands a,b,c,d,e,f ,g,h,3.25,(a+b),(c +d),etc. (5) 29 / 100
  • 42. Images/cinvestav- Arithmetic Expressions Imagine the following expressions (a+b)∗(c +d)+ef /g ∗h +3.25 (3) Operators +,−,/,∗ (4) Operands a,b,c,d,e,f ,g,h,3.25,(a+b),(c +d),etc. (5) 29 / 100
  • 43. Images/cinvestav- Arithmetic Expressions Imagine the following expressions (a+b)∗(c +d)+ef /g ∗h +3.25 (3) Operators +,−,/,∗ (4) Operands a,b,c,d,e,f ,g,h,3.25,(a+b),(c +d),etc. (5) 29 / 100
  • 45. Images/cinvestav- Operators Degree Denition Number of operands that the operator requires. Binary operator requires two operands. a + b c / d e - f Unary operator requires one operand. + g - h 31 / 100
  • 46. Images/cinvestav- Operators Degree Denition Number of operands that the operator requires. Binary operator requires two operands. a + b c / d e - f Unary operator requires one operand. + g - h 31 / 100
  • 47. Images/cinvestav- Operators Degree Denition Number of operands that the operator requires. Binary operator requires two operands. a + b c / d e - f Unary operator requires one operand. + g - h 31 / 100
  • 48. Images/cinvestav- About the Inx form Something Notable Normal way to write an expression. Thus Binary operators come in between their left and right operands. Example a * b a + b * c a * b / c (a + b) * (c + d) + e f/g*h + 3.25 32 / 100
  • 49. Images/cinvestav- About the Inx form Something Notable Normal way to write an expression. Thus Binary operators come in between their left and right operands. Example a * b a + b * c a * b / c (a + b) * (c + d) + e f/g*h + 3.25 32 / 100
  • 50. Images/cinvestav- About the Inx form Something Notable Normal way to write an expression. Thus Binary operators come in between their left and right operands. Example a * b a + b * c a * b / c (a + b) * (c + d) + e f/g*h + 3.25 32 / 100
  • 51. Images/cinvestav- Operator Priorities How do you gure out the operands of an operator? a + b * c a * b + c / d This is done by assigning operator priorities priority(*) = priority(/) priority(+) = priority(-) Thus When an operand lies between two operators, the operand associates with the operator that has higher priority. 33 / 100
  • 52. Images/cinvestav- Operator Priorities How do you gure out the operands of an operator? a + b * c a * b + c / d This is done by assigning operator priorities priority(*) = priority(/) priority(+) = priority(-) Thus When an operand lies between two operators, the operand associates with the operator that has higher priority. 33 / 100
  • 53. Images/cinvestav- Operator Priorities How do you gure out the operands of an operator? a + b * c a * b + c / d This is done by assigning operator priorities priority(*) = priority(/) priority(+) = priority(-) Thus When an operand lies between two operators, the operand associates with the operator that has higher priority. 33 / 100
  • 54. Images/cinvestav- Tie Breakers However this can happen When an operand lies between two operators that have the same priority, the operand associates with the operator on the left. 34 / 100
  • 55. Images/cinvestav- Delimiters What about () Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression. Example (a + b) * (c d) / (e f) 35 / 100
  • 56. Images/cinvestav- Delimiters What about () Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression. Example (a + b) * (c d) / (e f) 35 / 100
  • 57. Images/cinvestav- HOWEVER Inx Expression Is Hard To Parse Need operator priorities, tie breaker, and delimiters. This makes computer evaluation more dicult than is necessary. Postx and prex expression forms do not rely on operator priorities, a tie breaker, or delimiters. So it is easier for a computer to evaluate expressions that are in these forms. 36 / 100
  • 58. Images/cinvestav- HOWEVER Inx Expression Is Hard To Parse Need operator priorities, tie breaker, and delimiters. This makes computer evaluation more dicult than is necessary. Postx and prex expression forms do not rely on operator priorities, a tie breaker, or delimiters. So it is easier for a computer to evaluate expressions that are in these forms. 36 / 100
  • 59. Images/cinvestav- HOWEVER Inx Expression Is Hard To Parse Need operator priorities, tie breaker, and delimiters. This makes computer evaluation more dicult than is necessary. Postx and prex expression forms do not rely on operator priorities, a tie breaker, or delimiters. So it is easier for a computer to evaluate expressions that are in these forms. 36 / 100
  • 60. Images/cinvestav- HOWEVER Inx Expression Is Hard To Parse Need operator priorities, tie breaker, and delimiters. This makes computer evaluation more dicult than is necessary. Postx and prex expression forms do not rely on operator priorities, a tie breaker, or delimiters. So it is easier for a computer to evaluate expressions that are in these forms. 36 / 100
  • 62. Images/cinvestav- Postx Form We have that The postx form of a variable or constant is the same as its inx form. a, b, 3.25 Relative order The relative order of operands is the same in inx and postx forms. Thus Operators come immediately after the postx form of their operands. Inx = a + b Postx = ab+ 38 / 100
  • 63. Images/cinvestav- Postx Form We have that The postx form of a variable or constant is the same as its inx form. a, b, 3.25 Relative order The relative order of operands is the same in inx and postx forms. Thus Operators come immediately after the postx form of their operands. Inx = a + b Postx = ab+ 38 / 100
  • 64. Images/cinvestav- Postx Form We have that The postx form of a variable or constant is the same as its inx form. a, b, 3.25 Relative order The relative order of operands is the same in inx and postx forms. Thus Operators come immediately after the postx form of their operands. Inx = a + b Postx = ab+ 38 / 100
  • 65. Images/cinvestav- Example 1st One Inx = a+b*c Postx= abc*+ 2nd One Inx = a*b+c Postx = ab*c+ 3rd One Inx = (a + b) * (c d) / (e + f) Postx = (ab)+cd-*ef+/ 39 / 100
  • 66. Images/cinvestav- Example 1st One Inx = a+b*c Postx= abc*+ 2nd One Inx = a*b+c Postx = ab*c+ 3rd One Inx = (a + b) * (c d) / (e + f) Postx = (ab)+cd-*ef+/ 39 / 100
  • 67. Images/cinvestav- Example 1st One Inx = a+b*c Postx= abc*+ 2nd One Inx = a*b+c Postx = ab*c+ 3rd One Inx = (a + b) * (c d) / (e + f) Postx = (ab)+cd-*ef+/ 39 / 100
  • 68. Images/cinvestav- Example 1st One Inx = a+b*c Postx= abc*+ 2nd One Inx = a*b+c Postx = ab*c+ 3rd One Inx = (a + b) * (c d) / (e + f) Postx = (ab)+cd-*ef+/ 39 / 100
  • 69. Images/cinvestav- Example 1st One Inx = a+b*c Postx= abc*+ 2nd One Inx = a*b+c Postx = ab*c+ 3rd One Inx = (a + b) * (c d) / (e + f) Postx = (ab)+cd-*ef+/ 39 / 100
  • 70. Images/cinvestav- Example 1st One Inx = a+b*c Postx= abc*+ 2nd One Inx = a*b+c Postx = ab*c+ 3rd One Inx = (a + b) * (c d) / (e + f) Postx = (ab)+cd-*ef+/ 39 / 100
  • 71. Images/cinvestav- What about Unary Operators? After all they need to be evaluated Everything need to be interpreted!!! Replace with new symbols + a = a @ + a + b = a @ b + - a = a ? - a-b = a ? b - 40 / 100
  • 72. Images/cinvestav- What about Unary Operators? After all they need to be evaluated Everything need to be interpreted!!! Replace with new symbols + a = a @ + a + b = a @ b + - a = a ? - a-b = a ? b - 40 / 100
  • 73. Images/cinvestav- What about Unary Operators? After all they need to be evaluated Everything need to be interpreted!!! Replace with new symbols + a = a @ + a + b = a @ b + - a = a ? - a-b = a ? b - 40 / 100
  • 74. Images/cinvestav- What about Unary Operators? After all they need to be evaluated Everything need to be interpreted!!! Replace with new symbols + a = a @ + a + b = a @ b + - a = a ? - a-b = a ? b - 40 / 100
  • 75. Images/cinvestav- What about Unary Operators? After all they need to be evaluated Everything need to be interpreted!!! Replace with new symbols + a = a @ + a + b = a @ b + - a = a ? - a-b = a ? b - 40 / 100
  • 76. Images/cinvestav- Postx Evaluation How do we do the evaluation? Scan postx expression from left to right pushing operands on to a stack. Then When an operator is encountered, pop as many operands as this operator needs. Then, evaluate the operator. Then, push the result on to the stack. IMPORTANT This works because, in postx, operators come immediately after their operands. 41 / 100
  • 77. Images/cinvestav- Postx Evaluation How do we do the evaluation? Scan postx expression from left to right pushing operands on to a stack. Then When an operator is encountered, pop as many operands as this operator needs. Then, evaluate the operator. Then, push the result on to the stack. IMPORTANT This works because, in postx, operators come immediately after their operands. 41 / 100
  • 78. Images/cinvestav- Postx Evaluation How do we do the evaluation? Scan postx expression from left to right pushing operands on to a stack. Then When an operator is encountered, pop as many operands as this operator needs. Then, evaluate the operator. Then, push the result on to the stack. IMPORTANT This works because, in postx, operators come immediately after their operands. 41 / 100
  • 79. Images/cinvestav- Postx Evaluation How do we do the evaluation? Scan postx expression from left to right pushing operands on to a stack. Then When an operator is encountered, pop as many operands as this operator needs. Then, evaluate the operator. Then, push the result on to the stack. IMPORTANT This works because, in postx, operators come immediately after their operands. 41 / 100
  • 80. Images/cinvestav- Postx Evaluation How do we do the evaluation? Scan postx expression from left to right pushing operands on to a stack. Then When an operator is encountered, pop as many operands as this operator needs. Then, evaluate the operator. Then, push the result on to the stack. IMPORTANT This works because, in postx, operators come immediately after their operands. 41 / 100
  • 81. Images/cinvestav- Example: (a + b) * (c d) / (e + f) a b + c d - * e f + / 42 / 100
  • 82. Images/cinvestav- Example: (a + b) * (c d) / (e + f) a b + c d - * e f + / 43 / 100
  • 83. Images/cinvestav- Example: (a + b) * (c d) / (e + f) a b + c d - * e f + / 44 / 100
  • 84. Images/cinvestav- Example: (a + b) * (c d) / (e + f) a b + c d - * e f + / 45 / 100
  • 85. Images/cinvestav- Example: (a + b) * (c d) / (e + f) a b + c d - * e f + / 46 / 100
  • 86. Images/cinvestav- Example: (a + b) * (c d) / (e + f) a b + c d - * e f + / 47 / 100
  • 87. Images/cinvestav- Example: (a + b) * (c d) / (e + f) a b + c d - * e f + / 48 / 100
  • 88. Images/cinvestav- Prex Form Here The prex form of a variable or constant is the same as its inx form. a, b, 3.25 Thus The relative order of operands is the same in inx and prex forms. Then Operators come immediately before the prex form of their operands. Inx = a + b Postx = ab+ Prex = +ab 49 / 100
  • 89. Images/cinvestav- Prex Form Here The prex form of a variable or constant is the same as its inx form. a, b, 3.25 Thus The relative order of operands is the same in inx and prex forms. Then Operators come immediately before the prex form of their operands. Inx = a + b Postx = ab+ Prex = +ab 49 / 100
  • 90. Images/cinvestav- Prex Form Here The prex form of a variable or constant is the same as its inx form. a, b, 3.25 Thus The relative order of operands is the same in inx and prex forms. Then Operators come immediately before the prex form of their operands. Inx = a + b Postx = ab+ Prex = +ab 49 / 100
  • 91. Images/cinvestav- Prex Form Here The prex form of a variable or constant is the same as its inx form. a, b, 3.25 Thus The relative order of operands is the same in inx and prex forms. Then Operators come immediately before the prex form of their operands. Inx = a + b Postx = ab+ Prex = +ab 49 / 100
  • 92. Images/cinvestav- Prex Form Here The prex form of a variable or constant is the same as its inx form. a, b, 3.25 Thus The relative order of operands is the same in inx and prex forms. Then Operators come immediately before the prex form of their operands. Inx = a + b Postx = ab+ Prex = +ab 49 / 100
  • 93. Images/cinvestav- Prex Form Here The prex form of a variable or constant is the same as its inx form. a, b, 3.25 Thus The relative order of operands is the same in inx and prex forms. Then Operators come immediately before the prex form of their operands. Inx = a + b Postx = ab+ Prex = +ab 49 / 100
  • 94. Images/cinvestav- Prex Form Here The prex form of a variable or constant is the same as its inx form. a, b, 3.25 Thus The relative order of operands is the same in inx and prex forms. Then Operators come immediately before the prex form of their operands. Inx = a + b Postx = ab+ Prex = +ab 49 / 100
  • 95. Images/cinvestav- Then, we can use trees to represent operations a+b + a b -a - a 50 / 100
  • 96. Images/cinvestav- Then, we can use trees to represent operations a+b + a b -a - a 50 / 100
  • 97. Images/cinvestav- Example (a + b) * (c d) / (e + f) 51 / 100
  • 98. Images/cinvestav- Merits Of Binary Tree Form We love them The left and right operands are easy to visualize. There are code optimization algorithms that work well with the binary tree form of an expression. We can use simple recursive evaluations on each expression to get the results. 52 / 100
  • 99. Images/cinvestav- Merits Of Binary Tree Form We love them The left and right operands are easy to visualize. There are code optimization algorithms that work well with the binary tree form of an expression. We can use simple recursive evaluations on each expression to get the results. 52 / 100
  • 100. Images/cinvestav- Merits Of Binary Tree Form We love them The left and right operands are easy to visualize. There are code optimization algorithms that work well with the binary tree form of an expression. We can use simple recursive evaluations on each expression to get the results. 52 / 100
  • 101. Images/cinvestav- Outline 1 Trees: Basic Concepts Subtrees, Leaves and Levels 2 Binary Trees Basic Concepts The Height Of a Binary Tree Arithmetic Expressions Properties Numbering Nodes in A Full Binary Tree Array Representation Linked Representation Traversals 53 / 100
  • 102. Images/cinvestav- Minimum Number Of Nodes Question Which is the minimum number of nodes in a binary tree whose height is h? First You have at least one node at each of the h levels. Then 54 / 100
  • 103. Images/cinvestav- Minimum Number Of Nodes Question Which is the minimum number of nodes in a binary tree whose height is h? First You have at least one node at each of the h levels. Then 54 / 100
  • 104. Images/cinvestav- Minimum Number Of Nodes Question Which is the minimum number of nodes in a binary tree whose height is h? First You have at least one node at each of the h levels. Then 54 / 100
  • 105. Images/cinvestav- Maximum Number Of Nodes Here, all possible nodes at rst h levels are present. Here, we have that 1+2+4+8+...+2h−1 = 2h −1 2−1 = 2h −1 (7) 55 / 100
  • 106. Images/cinvestav- Maximum Number Of Nodes Here, all possible nodes at rst h levels are present. Here, we have that 1+2+4+8+...+2h−1 = 2h −1 2−1 = 2h −1 (7) 55 / 100
  • 107. Images/cinvestav- Number Of Nodes Height We have that Let n be the number of nodes in a binary tree whose height is h. Then h ≤ n ≤ 2h −1 (8) Thus log2 (n +1) ≤ h ⇒ log2 (n +1) ≤ h ≤ n (9) 56 / 100
  • 108. Images/cinvestav- Number Of Nodes Height We have that Let n be the number of nodes in a binary tree whose height is h. Then h ≤ n ≤ 2h −1 (8) Thus log2 (n +1) ≤ h ⇒ log2 (n +1) ≤ h ≤ n (9) 56 / 100
  • 109. Images/cinvestav- Number Of Nodes Height We have that Let n be the number of nodes in a binary tree whose height is h. Then h ≤ n ≤ 2h −1 (8) Thus log2 (n +1) ≤ h ⇒ log2 (n +1) ≤ h ≤ n (9) 56 / 100
  • 110. Images/cinvestav- Full Binary Tree A full binary tree of a given height h has 2 h1 nodes. 57 / 100
  • 111. Images/cinvestav- Outline 1 Trees: Basic Concepts Subtrees, Leaves and Levels 2 Binary Trees Basic Concepts The Height Of a Binary Tree Arithmetic Expressions Properties Numbering Nodes in A Full Binary Tree Array Representation Linked Representation Traversals 58 / 100
  • 112. Images/cinvestav- Numbering Nodes In A Full Binary Tree It is possible to do the following 1 Number the nodes 1 through 2h 1. 2 Number by levels from top to bottom. 3 Within a level number from left to right. We get the following 59 / 100
  • 113. Images/cinvestav- Numbering Nodes In A Full Binary Tree It is possible to do the following 1 Number the nodes 1 through 2h 1. 2 Number by levels from top to bottom. 3 Within a level number from left to right. We get the following 59 / 100
  • 114. Images/cinvestav- Numbering Nodes In A Full Binary Tree It is possible to do the following 1 Number the nodes 1 through 2h 1. 2 Number by levels from top to bottom. 3 Within a level number from left to right. We get the following 59 / 100
  • 115. Images/cinvestav- Numbering Nodes In A Full Binary Tree It is possible to do the following 1 Number the nodes 1 through 2h 1. 2 Number by levels from top to bottom. 3 Within a level number from left to right. We get the following 59 / 100
  • 116. Images/cinvestav- Node Number Properties We have the following The parent of node i is node i/2 , unless i = 1. Node 1 is the root and has no parent. What about the children Left child of node i is node 2i, unless 2i n, where n is the number of nodes. If 2i n, node i has no left child. 60 / 100
  • 117. Images/cinvestav- Node Number Properties We have the following The parent of node i is node i/2 , unless i = 1. Node 1 is the root and has no parent. What about the children Left child of node i is node 2i, unless 2i n, where n is the number of nodes. If 2i n, node i has no left child. 60 / 100
  • 118. Images/cinvestav- Node Number Properties We have the following The parent of node i is node i/2 , unless i = 1. Node 1 is the root and has no parent. What about the children Left child of node i is node 2i, unless 2i n, where n is the number of nodes. If 2i n, node i has no left child. 60 / 100
  • 119. Images/cinvestav- Node Number Properties We have the following The parent of node i is node i/2 , unless i = 1. Node 1 is the root and has no parent. What about the children Left child of node i is node 2i, unless 2i n, where n is the number of nodes. If 2i n, node i has no left child. 60 / 100
  • 120. Images/cinvestav- After all We get the following 61 / 100
  • 121. Images/cinvestav- What about the right Children? We have the following 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. 62 / 100
  • 122. Images/cinvestav- What about the right Children? We have the following 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. 62 / 100
  • 123. Images/cinvestav- Complete Binary Tree With n Nodes Dention 1 It starts with a full binary tree that has at least n nodes. 2 Number the nodes as described earlier. 3 The binary tree dened by the nodes numbered 1 through n is the unique n node complete binary tree. 63 / 100
  • 124. Images/cinvestav- Complete Binary Tree With n Nodes Dention 1 It starts with a full binary tree that has at least n nodes. 2 Number the nodes as described earlier. 3 The binary tree dened by the nodes numbered 1 through n is the unique n node complete binary tree. 63 / 100
  • 125. Images/cinvestav- Complete Binary Tree With n Nodes Dention 1 It starts with a full binary tree that has at least n nodes. 2 Number the nodes as described earlier. 3 The binary tree dened by the nodes numbered 1 through n is the unique n node complete binary tree. 63 / 100
  • 127. Images/cinvestav- How we represent them? We have two ways Array Representation Linked Representation 65 / 100
  • 128. Images/cinvestav- Outline 1 Trees: Basic Concepts Subtrees, Leaves and Levels 2 Binary Trees Basic Concepts The Height Of a Binary Tree Arithmetic Expressions Properties Numbering Nodes in A Full Binary Tree Array Representation Linked Representation Traversals 66 / 100
  • 129. Images/cinvestav- Array Representation We do the following Number the nodes using the numbering scheme for a full binary tree. The node that is numbered i is stored in tree[i]. Then 67 / 100
  • 130. Images/cinvestav- Array Representation We do the following Number the nodes using the numbering scheme for a full binary tree. The node that is numbered i is stored in tree[i]. Then 67 / 100
  • 131. Images/cinvestav- What if we have something like this Right-Skewed Binary Tree What a waste of memory!!! 68 / 100
  • 132. Images/cinvestav- What if we have something like this Right-Skewed Binary Tree What a waste of memory!!! 68 / 100
  • 133. Images/cinvestav- Outline 1 Trees: Basic Concepts Subtrees, Leaves and Levels 2 Binary Trees Basic Concepts The Height Of a Binary Tree Arithmetic Expressions Properties Numbering Nodes in A Full Binary Tree Array Representation Linked Representation Traversals 69 / 100
  • 134. Images/cinvestav- What can we do? Linked Representation Each binary tree node is represented as an object whose data type is BinaryTreeNode. The space required by an n node binary tree is n * (space required by one node). 70 / 100
  • 135. Images/cinvestav- What can we do? Linked Representation Each binary tree node is represented as an object whose data type is BinaryTreeNode. The space required by an n node binary tree is n * (space required by one node). 70 / 100
  • 136. Images/cinvestav- Code for a Node We have then with Generic Key and Item p a c k a g e T r e e ; p u b l i c c l a s s B i n a r y T r e e N o d e { Key k e y ; I t e m e l e m e n t ; B i n a r y T r e e N o d e l e f t C h i l d ; // l e f t subtree B i n a r y T r e e N o d e r i g h t C h i l d ; // r i g h t subtree // c o n s t r u c t o r s and any other methods // come here } 71 / 100
  • 138. Images/cinvestav- Which Operations We might want to have Determine the height. Determine the number of nodes. Make a clone. Determine if two binary trees are clones. Maybe, we want Display the binary tree. Evaluate the arithmetic expression represented by a binary tree. Finally, we want Obtain the inx form of an expression. Obtain the prex form of an expression. Obtain the postx form of an expression. 73 / 100
  • 139. Images/cinvestav- Which Operations We might want to have Determine the height. Determine the number of nodes. Make a clone. Determine if two binary trees are clones. Maybe, we want Display the binary tree. Evaluate the arithmetic expression represented by a binary tree. Finally, we want Obtain the inx form of an expression. Obtain the prex form of an expression. Obtain the postx form of an expression. 73 / 100
  • 140. Images/cinvestav- Which Operations We might want to have Determine the height. Determine the number of nodes. Make a clone. Determine if two binary trees are clones. Maybe, we want Display the binary tree. Evaluate the arithmetic expression represented by a binary tree. Finally, we want Obtain the inx form of an expression. Obtain the prex form of an expression. Obtain the postx form of an expression. 73 / 100
  • 141. Images/cinvestav- Which Operations We might want to have Determine the height. Determine the number of nodes. Make a clone. Determine if two binary trees are clones. Maybe, we want Display the binary tree. Evaluate the arithmetic expression represented by a binary tree. Finally, we want Obtain the inx form of an expression. Obtain the prex form of an expression. Obtain the postx form of an expression. 73 / 100
  • 142. Images/cinvestav- Which Operations We might want to have Determine the height. Determine the number of nodes. Make a clone. Determine if two binary trees are clones. Maybe, we want Display the binary tree. Evaluate the arithmetic expression represented by a binary tree. Finally, we want Obtain the inx form of an expression. Obtain the prex form of an expression. Obtain the postx form of an expression. 73 / 100
  • 143. Images/cinvestav- Which Operations We might want to have Determine the height. Determine the number of nodes. Make a clone. Determine if two binary trees are clones. Maybe, we want Display the binary tree. Evaluate the arithmetic expression represented by a binary tree. Finally, we want Obtain the inx form of an expression. Obtain the prex form of an expression. Obtain the postx form of an expression. 73 / 100
  • 144. Images/cinvestav- Which Operations We might want to have Determine the height. Determine the number of nodes. Make a clone. Determine if two binary trees are clones. Maybe, we want Display the binary tree. Evaluate the arithmetic expression represented by a binary tree. Finally, we want Obtain the inx form of an expression. Obtain the prex form of an expression. Obtain the postx form of an expression. 73 / 100
  • 145. Images/cinvestav- Which Operations We might want to have Determine the height. Determine the number of nodes. Make a clone. Determine if two binary trees are clones. Maybe, we want Display the binary tree. Evaluate the arithmetic expression represented by a binary tree. Finally, we want Obtain the inx form of an expression. Obtain the prex form of an expression. Obtain the postx form of an expression. 73 / 100
  • 146. Images/cinvestav- Which Operations We might want to have Determine the height. Determine the number of nodes. Make a clone. Determine if two binary trees are clones. Maybe, we want Display the binary tree. Evaluate the arithmetic expression represented by a binary tree. Finally, we want Obtain the inx form of an expression. Obtain the prex form of an expression. Obtain the postx form of an expression. 73 / 100
  • 147. Images/cinvestav- Outline 1 Trees: Basic Concepts Subtrees, Leaves and Levels 2 Binary Trees Basic Concepts The Height Of a Binary Tree Arithmetic Expressions Properties Numbering Nodes in A Full Binary Tree Array Representation Linked Representation Traversals 74 / 100
  • 148. Images/cinvestav- Traversals Something Notable Many binary tree operations are done by performing a traversal of the binary tree. Actually In a traversal, each element of the binary tree is visited exactly once. Actions During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken. 75 / 100
  • 149. Images/cinvestav- Traversals Something Notable Many binary tree operations are done by performing a traversal of the binary tree. Actually In a traversal, each element of the binary tree is visited exactly once. Actions During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken. 75 / 100
  • 150. Images/cinvestav- Traversals Something Notable Many binary tree operations are done by performing a traversal of the binary tree. Actually In a traversal, each element of the binary tree is visited exactly once. Actions During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken. 75 / 100
  • 151. Images/cinvestav- Binary Tree Traversal Methods Thus, we have Preorder Inorder Postorder Level order 76 / 100
  • 152. Images/cinvestav- Binary Tree Traversal Methods Thus, we have Preorder Inorder Postorder Level order 76 / 100
  • 153. Images/cinvestav- Binary Tree Traversal Methods Thus, we have Preorder Inorder Postorder Level order 76 / 100
  • 154. Images/cinvestav- Binary Tree Traversal Methods Thus, we have Preorder Inorder Postorder Level order 76 / 100
  • 155. Images/cinvestav- Remarks First In a traversal of a binary tree, each element of the binary tree is visited exactly once. Second During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken. 77 / 100
  • 156. Images/cinvestav- Remarks First In a traversal of a binary tree, each element of the binary tree is visited exactly once. Second During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken. 77 / 100
  • 157. Images/cinvestav- Preorder Traversals Code p u b l i c s t a t i c v o i d p r e O r d e r ( B i n a r y T r e e N o d e t ) { i f ( t != n u l l ) { v i s i t ( t ) ; p r e O r d e r ( t . l e f t C h i l d ) ; p r e O r d e r ( t . r i g h t C h i l d ) ; } } 78 / 100
  • 158. Images/cinvestav- Example I : (visit = print) Something Notable 79 / 100
  • 159. Images/cinvestav- Example II : (visit = print) Something Notable 80 / 100
  • 160. Images/cinvestav- Actually the preorder can be used to express the prex form!!! Example 81 / 100
  • 161. Images/cinvestav- Inorder Traversals Code p u b l i c s t a t i c v o i d i n O r d e r ( B i n a r y T r e e N o d e t ) { i f ( t != n u l l ) { i n O r d e r ( t . l e f t C h i l d ) ; v i s i t ( t ) ; i n O r d e r ( t . r i g h t C h i l d ) ; } } 82 / 100
  • 162. Images/cinvestav- Example I : (visit = print) Something Notable 83 / 100
  • 163. Images/cinvestav- Example II : (visit = print) Something Notable 84 / 100
  • 164. Images/cinvestav- Example III : (visit = print) Inorder By Projection (Squishing) 85 / 100
  • 165. Images/cinvestav- Inorder Of Expression Tree Example Not only you get the inx form But, we have a implicit representation of the parenthesis 86 / 100
  • 166. Images/cinvestav- Inorder Of Expression Tree Example Not only you get the inx form But, we have a implicit representation of the parenthesis 86 / 100
  • 167. Images/cinvestav- Postorder Traversal Code p u b l i c s t a t i c v o i d p o s t O r d e r ( B i n a r y T r e e N o d e t ) { i f ( t != n u l l ) { p o s t O r d e r ( t . l e f t C h i l d ) ; p o s t O r d e r ( t . r i g h t C h i l d ) ; v i s i t ( t ) ; } } 87 / 100
  • 168. Images/cinvestav- Example I : (visit = print) Something Notable 88 / 100
  • 169. Images/cinvestav- Example II : (visit = print) Something Notable 89 / 100
  • 170. Images/cinvestav- Actually the preorder can be used to express the postx form!!! Example 90 / 100
  • 171. Images/cinvestav- Traversal Applications First One Make a Clone Second Determine the height. Third Determine number of nodes. 91 / 100
  • 172. Images/cinvestav- Traversal Applications First One Make a Clone Second Determine the height. Third Determine number of nodes. 91 / 100
  • 173. Images/cinvestav- Traversal Applications First One Make a Clone Second Determine the height. Third Determine number of nodes. 91 / 100
  • 174. Images/cinvestav- Now What if we want to traverse each node in every level rst - level order 92 / 100
  • 176. Images/cinvestav- Level Order Code Which one? The code L e t t be t h e t r e e r o o t . w h i l e ( t != n u l l ) { v i s i t t and p u t i t s c h i l d r e n on a FIFO queue ; remove a node from t h e FIFO queue and c a l l i t t ; // remove r e t u r n s n u l l when queue i s empty } 93 / 100
  • 177. Images/cinvestav- Binary Tree Construction Question Can you construct the binary tree, given two traversal sequences? However It depends on which two sequences are given. Properties 94 / 100
  • 178. Images/cinvestav- Binary Tree Construction Question Can you construct the binary tree, given two traversal sequences? However It depends on which two sequences are given. Properties 94 / 100
  • 179. Images/cinvestav- Binary Tree Construction Question Can you construct the binary tree, given two traversal sequences? However It depends on which two sequences are given. Properties 94 / 100
  • 180. Images/cinvestav- Preorder And Postorder If we have the following Something Notable Preorder = ab Postorder = ba We have a problem Preorder and postorder do not uniquely dene a binary tree. Nor do preorder and level order (same example). Nor do postorder and level order (same example). 95 / 100
  • 181. Images/cinvestav- Preorder And Postorder If we have the following Something Notable Preorder = ab Postorder = ba We have a problem Preorder and postorder do not uniquely dene a binary tree. Nor do preorder and level order (same example). Nor do postorder and level order (same example). 95 / 100
  • 182. Images/cinvestav- Preorder And Postorder If we have the following Something Notable Preorder = ab Postorder = ba We have a problem Preorder and postorder do not uniquely dene a binary tree. Nor do preorder and level order (same example). Nor do postorder and level order (same example). 95 / 100
  • 183. Images/cinvestav- Preorder And Postorder If we have the following Something Notable Preorder = ab Postorder = ba We have a problem Preorder and postorder do not uniquely dene a binary tree. Nor do preorder and level order (same example). Nor do postorder and level order (same example). 95 / 100
  • 184. Images/cinvestav- Preorder And Postorder If we have the following Something Notable Preorder = ab Postorder = ba We have a problem Preorder and postorder do not uniquely dene a binary tree. Nor do preorder and level order (same example). Nor do postorder and level order (same example). 95 / 100
  • 185. Images/cinvestav- Preorder And Postorder If we have the following Something Notable Preorder = ab Postorder = ba We have a problem Preorder and postorder do not uniquely dene a binary tree. Nor do preorder and level order (same example). Nor do postorder and level order (same example). 95 / 100
  • 186. Images/cinvestav- What if we have Inorder And Preorder Example inorder = g d h b e i a f j c preorder = a b d g h e i c f j Process Scan the preorder left to right using the inorder to separate left and right subtrees. Thus a is the root of the tree; gdhbei are in the left subtree; fjc are in the right subtree. 96 / 100
  • 187. Images/cinvestav- What if we have Inorder And Preorder Example inorder = g d h b e i a f j c preorder = a b d g h e i c f j Process Scan the preorder left to right using the inorder to separate left and right subtrees. Thus a is the root of the tree; gdhbei are in the left subtree; fjc are in the right subtree. 96 / 100
  • 188. Images/cinvestav- What if we have Inorder And Preorder Example inorder = g d h b e i a f j c preorder = a b d g h e i c f j Process Scan the preorder left to right using the inorder to separate left and right subtrees. Thus a is the root of the tree; gdhbei are in the left subtree; fjc are in the right subtree. 96 / 100
  • 189. Images/cinvestav- Then First preorder = a b d g h e i c f j 97 / 100
  • 190. Images/cinvestav- Then First preorder = a b d g h e i c f j 97 / 100
  • 191. Images/cinvestav- Cont... d is the next root; g is in the left subtree; h is in the right subtree. 98 / 100
  • 192. Images/cinvestav- Inorder And Postorder Thus Scan postorder from right to left using inorder to separate left and right subtrees. Example inorder = g d h b e i a f j c postorder = g h d i e b j f c a Thus Tree root is a; gdhbei are in left subtree; fjc are in right subtree. 99 / 100
  • 193. Images/cinvestav- Inorder And Postorder Thus Scan postorder from right to left using inorder to separate left and right subtrees. Example inorder = g d h b e i a f j c postorder = g h d i e b j f c a Thus Tree root is a; gdhbei are in left subtree; fjc are in right subtree. 99 / 100
  • 194. Images/cinvestav- Inorder And Postorder Thus Scan postorder from right to left using inorder to separate left and right subtrees. Example inorder = g d h b e i a f j c postorder = g h d i e b j f c a Thus Tree root is a; gdhbei are in left subtree; fjc are in right subtree. 99 / 100
  • 195. Images/cinvestav- Inorder And Postorder Thus Scan postorder from right to left using inorder to separate left and right subtrees. Example inorder = g d h b e i a f j c postorder = g h d i e b j f c a Thus Tree root is a; gdhbei are in left subtree; fjc are in right subtree. 99 / 100
  • 196. Images/cinvestav- Inorder And Level Order Thus Scan level order from left to right using inorder to separate left and right subtrees. Example inorder = g d h b e i a f j c level order = a b c d e f g h i j Thus Tree root is a; gdhbei are in left subtree; fjc are in right subtree. 100 / 100
  • 197. Images/cinvestav- Inorder And Level Order Thus Scan level order from left to right using inorder to separate left and right subtrees. Example inorder = g d h b e i a f j c level order = a b c d e f g h i j Thus Tree root is a; gdhbei are in left subtree; fjc are in right subtree. 100 / 100
  • 198. Images/cinvestav- Inorder And Level Order Thus Scan level order from left to right using inorder to separate left and right subtrees. Example inorder = g d h b e i a f j c level order = a b c d e f g h i j Thus Tree root is a; gdhbei are in left subtree; fjc are in right subtree. 100 / 100
  • 199. Images/cinvestav- Inorder And Level Order Thus Scan level order from left to right using inorder to separate left and right subtrees. Example inorder = g d h b e i a f j c level order = a b c d e f g h i j Thus Tree root is a; gdhbei are in left subtree; fjc are in right subtree. 100 / 100