0% found this document useful (0 votes)
2 views88 pages

Understanding Trees and Binary Trees

The document discusses the structure and properties of trees and binary trees, highlighting their differences and applications in data organization. It explains concepts such as node degree, tree height, and traversal methods, along with the representation of binary trees in array and linked forms. Additionally, it covers arithmetic expressions and their evaluation using different notations like infix, postfix, and prefix.

Uploaded by

rp077216
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views88 pages

Understanding Trees and Binary Trees

The document discusses the structure and properties of trees and binary trees, highlighting their differences and applications in data organization. It explains concepts such as node degree, tree height, and traversal methods, along with the representation of binary trees in array and linked forms. Additionally, it covers arithmetic expressions and their evaluation using different notations like infix, postfix, and prefix.

Uploaded by

rp077216
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Trees

Nature Lover’s View Of A Tree

leaves

branches

root
Computer Scientist’s View
root leaves

branches

nodes
Linear Lists And Trees
• Linear lists are useful for serially ordered data.
 (e0, e1, e2, …, 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.
Hierarchical Data And Trees

• The element at the top of the hierarchy is the


root.
• Elements next in the hierarchy are the children
of the root.
• Elements next in the hierarchy are the
grandchildren of the root, and so on.
• Elements that have no children are leaves.
Java’s Classes (Part Of Figure 1.1)
Object
root
children of root
Number Throwable OutputStream

Integer Double Exception FileOutputStream


grand children of root

RuntimeException
great grand child of root
Definition

• A tree t is a finite nonempty set of elements.


• One of these elements is called the root.
• The remaining elements, if any, are
partitioned into trees, which are called the
subtrees of t.
Subtrees
Object
root

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException
Leaves
Object

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException
Parent, Grandparent, Siblings, Ancestors, Descendants

Object

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException
Levels
Object
Level 1
Level 2
Number Throwable OutputStream

Integer Double Exception FileOutputStream


Level 3

RuntimeException
Level 4
Caution

• Some texts start level numbers at 0 rather than


at 1.
• 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 shall number levels with the root at level 1.
height = depth = number of levels
Object
Level 1
Level 2
Number Throwable OutputStream

Integer Double Exception FileOutputStream


Level 3

RuntimeException
Level 4
Node Degree = Number Of Children
Object 3

2 Number 1 Throwable 1 OutputStream

0 0 1 0
Integer Double Exception FileOutputStream

RuntimeException 0
Tree Degree = Max Node Degree
Object 3

2 Number 1 Throwable 1 OutputStream

0 0 1 0
Integer Double Exception FileOutputStream

RuntimeException 0
Degree of tree = 3.
Binary Tree

• Finite (possibly empty) collection of elements.


• A nonempty binary tree has a root element.
• The remaining elements (if any) are partitioned
into two binary trees.
• These are called the left and right subtrees of
the binary tree.
Differences Between A Tree & A Binary Tree

• 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.
• A binary tree may be empty; a tree cannot
be empty.
Differences Between A Tree & A Binary Tree

• The subtrees of a binary tree are ordered;


those of a tree are not ordered.

a a

b b

• Are different when viewed as binary trees.


• Are the same when viewed as trees.
Arithmetic Expressions

• (a + b) * (c + d) + e – f/g*h + 3.25
• Expressions comprise three kinds of entities.
 Operators (+, -, /, *).
 Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d),
etc.).
 Delimiters ((, )).
Operator Degree

• 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
Infix Form

• Normal way to write an expression.


• Binary operators come in between their left and
right operands.
 a*b
 a+b*c
 a*b/c
 (a + b) * (c + d) + e – f/g*h + 3.25
Operator Priorities

• How do you figure out the operands of an


operator?
 a+b*c
 a*b+c/d
• This is done by assigning operator priorities.
 priority(*) = priority(/) > priority(+) = priority(-)
• When an operand lies between two operators,
the operand associates with the operator that
has higher priority.
Tie Breaker

• When an operand lies between two operators


that have the same priority, the operand
associates with the operator on the left.
 a+b-c
 a*b/c/d
Delimiters

• Subexpression within delimiters is treated


as a single operand, independent from the
remainder of the expression.
 (a + b) * (c – d) / (e – f)
Infix Expression Is Hard To Parse

• Need operator priorities, tie breaker, and


delimiters.
• This makes computer evaluation more
difficult than is necessary.
• Postfix and prefix 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.
Postfix Form
• The postfix form of a variable or constant is
the same as its infix form.
 a, b, 3.25
• The relative order of operands is the same
in infix and postfix forms.
• Operators come immediately after the
postfix form of their operands.
 Infix = a + b
 Postfix = ab+
Postfix Examples
• Infix = a + b * c
 Postfix = a b c * +

• Infix = a * b + c
 Postfix = a b * c +

• Infix = (a + b) * (c – d) / (e + f)
 Postfix = a b + c d - * e f + /
Unary Operators

• Replace with new symbols.


 + a => a @
 + a + b => a @ b +
 - a => a ?
 - a-b => a ? b -
Postfix Evaluation

• Scan postfix expression from left to right


pushing operands on to a stack.
• When an operator is encountered, pop as
many operands as this operator needs;
evaluate the operator; push the result on to
the stack.
• This works because, in postfix, operators
come immediately after their operands.
Postfix Evaluation

• (a + b) * (c – d) / (e + f)
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
b
a
stack
Postfix Evaluation

• (a + b) * (c – d) / (e + f)
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/ d
c
• ab+cd-*ef+/
• (a + b)
ab+cd-*ef+/
• ab+cd-*ef+/ stack
Postfix Evaluation

• (a + b) * (c – d) / (e + f)
• ab+cd-*ef+/
• ab+cd-*ef+/

(c – d)
(a + b)
stack
Postfix Evaluation

• (a + b) * (c – d) / (e + f)
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/ f
• ab+cd-*ef+/ e
(a + b)*(c – d)
stack
Postfix Evaluation

• (a + b) * (c – d) / (e + f)
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/
• ab+cd-*ef+/ (e + f)
• ab+cd-*ef+/ (a + b)*(c – d)
stack
Prefix Form
• The prefix form of a variable or constant is
the same as its infix form.
 a, b, 3.25
• The relative order of operands is the same
in infix and prefix forms.
• Operators come immediately before the
prefix form of their operands.
 Infix = a + b
 Postfix = ab+
 Prefix = +ab
Binary Tree Form

• a+b +
a b

• -a -
a
Binary Tree Form
• (a + b) * (c – d) / (e + f)
//

* +
e f
+ -
a b c d
Merits Of Binary Tree Form
• Left and right operands are easy to visualize.
• Code optimization algorithms work with the
binary tree form of an expression.
• Simple recursive evaluation of expression.
/

* +
e f
+ -
a b c d
Binary Tree Properties & Representation
Minimum Number Of Nodes
• Minimum number of nodes in a binary tree whose height is h.
• At least one node at each of first h levels.

minimum number of
nodes is h
Maximum Number Of Nodes
• All possible nodes at first h levels are present.

Maximum number of nodes


= 1 + 2 + 4 + 8 + … + 2h-1
= 2h - 1
Number Of Nodes & Height

• Let n be the number of nodes in a binary


tree whose height is h.
• h <= n <= 2h – 1
• log2(n+1) <= h <= n
Full Binary Tree
• A full binary tree of a given height h has 2h – 1
nodes.

Height 4 full binary tree.


Numbering Nodes In A Full Binary
Tree
• Number the nodes 1 through 2h – 1.
• Number by levels from top to bottom.
• Within a level number from left to right.
1

2 3

4 5 6 7
8 9 10 11 12 13 14 15
Node Number Properties
1

2 3

4 5 6 7
8 9 10 11 12 13 14 15

• Parent of node i is node i / 2, unless i = 1.


• Node 1 is the root and has no parent.
Node Number Properties
1

2 3

4 5 6 7
8 9 10 11 12 13 14 15

• 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.
Node Number Properties
1

2 3

4 5 6 7
8 9 10 11 12 13 14 15

• Right child of node i is node 2i+1, unless 2i+1


> n, where n is the number of nodes.
• If 2i+1 > n, node i has no right child.
Complete Binary Tree With n Nodes

• Start with a full binary tree that has at least


n nodes.
• Number the nodes as described earlier.
• The binary tree defined by the nodes
numbered 1 through n is the unique n node
complete binary tree.
Example
1

2 3

4 5 6 7
8 9 10 11 12 13 14 15

• Complete binary tree with 10 nodes.


Binary Tree Representation

• Array representation.
• Linked representation.
Array Representation
• Number the nodes using the numbering scheme
for a full binary tree. The node that is numbered
i is stored in tree[i].
a1

2 3
b c

4 5 6 7
d e f g
8 9 10
h i j

tree[] a b c d e f g h i j
0 5 10
Right-Skewed Binary Tree
a1
b 3
7
c
15
d

tree[] a - b - - - c - - - - - - - d
0 5 10 15

• An n node binary tree needs an array whose length is between n+1 and 2n.
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).
The Class BinaryTreeNode
package dataStructures;
public class BinaryTreeNode
{
Object element;
BinaryTreeNode leftChild; // left subtree
BinaryTreeNode rightChild;// right subtree
// constructors and any other methods
// come here
}
Linked Representation Example
root a

b c

d e

g
f
leftChild
element h
rightChild
Some Binary Tree Operations
• Determine the height.
• Determine the number of nodes.
• Make a clone.
• Determine if two binary trees are clones.
• Display the binary tree.
• Evaluate the arithmetic expression
represented by a binary tree.
• Obtain the infix form of an expression.
• Obtain the prefix form of an expression.
• Obtain the postfix form of an expression.
Binary Tree Traversal Methods

• In a traversal of a binary tree, each element of


the binary tree is visited exactly once.
• During the visit of an element, all action (make
a clone, display, evaluate the operator, etc.)
with respect to this element is taken.
Binary Tree Traversal Methods

• Preorder
• Inorder
• Postorder
• Level order
Preorder Traversal
public static void preOrder(BinaryTreeNode t)
{
if (t != null)
{
visit(t);
preOrder([Link]);
preOrder([Link]);
}
}
Preorder Example (visit = print)
a

b c

abc
Preorder Example (visit = print)
a

b c
f
d e
g h i j

abdghei cf j
Preorder Of Expression Tree
/

* +
e f
+ -
a b c d

/ * +a b - c d +e f

Gives prefix form of expression!


Inorder Traversal
public static void inOrder(BinaryTreeNode t)
{
if (t != null)
{
inOrder([Link]);
visit(t);
inOrder([Link]);
}
}
Inorder Example (visit = print)
a

b c

bac
Inorder Example (visit = print)
a

b c
f
d e
g h i j

gdhbei af j c
Inorder By Projection (Squishing)
a

b c
f
d e
g h i j

g d h b e i a f jc
Inorder Of Expression Tree
/

* +
e f
+ -
a b c d

a + b * c - d/ e + f

Gives infix form of expression (sans parentheses)!


Postorder Traversal
public static void postOrder(BinaryTreeNode t)
{
if (t != null)
{
postOrder([Link]);
postOrder([Link]);
visit(t);
}
}
Postorder Example (visit = print)
a

b c

bca
Postorder Example (visit = print)
a

b c
f
d e
g h i j

ghdi ebj f ca
Postorder Of Expression Tree
/

* +
e f
+ -
a b c d

a b +c d - * e f + /

Gives postfix form of expression!


Traversal Applications
a

b c
f
d e
g h i j

• Make a clone.
• Determine height.
•Determine number of nodes.
Level Order

Let t be the tree root.


while (t != null)
{
visit t and put its children on a FIFO queue;
remove a node from the FIFO queue and
call it t;
// remove returns null when queue is empty
}
Level-Order Example (visit = print)
a

b c
f
d e
g h i j

abcdef ghi j
Binary Tree Construction
• Suppose that the elements in a binary tree
are distinct.
• Can you construct the binary tree from
which a given traversal sequence came?
• When a traversal sequence has more than
one element, the binary tree is not uniquely
defined.
• Therefore, the tree from which the sequence
was obtained cannot be reconstructed
uniquely.
Some Examples
preorder a a
= ab b b

inorder b a
= ab a b

postorder b b
= ab a a

level order a a
= ab b b
Binary Tree Construction

• Can you construct the binary tree,


given two traversal sequences?
• Depends on which two sequences are
given.
Preorder And Postorder

preorder = ab a a
postorder = ba b b

• Preorder and postorder do not uniquely define a


binary tree.
• Nor do preorder and level order (same example).
• Nor do postorder and level order (same
example).
Inorder And Preorder
• inorder = g d h b e i a f j c
• preorder = a b d g h e i c f j
• Scan the preorder left to right using the
inorder to separate left and right subtrees.
• a is the root of the tree; gdhbei are in the
left subtree; fjc are in the right subtree.
a

gdhbei fjc
Inorder And Preorder
a

gdhbei fjc
• preorder = a b d g h e i c f j
• b is the next root; gdh are in the left
subtree; ei are in the right subtree.
a

b fjc
gdh ei
Inorder And Preorder
a

b fjc
gdh ei
• preorder = a b d g h e i c f j
• d is the next root; g is in the left
subtree; h is in the right subtree.
a
b fjc
d ei
g h
Inorder And Postorder

• Scan postorder from right to left using


inorder to separate left and right subtrees.
• inorder = g d h b e i a f j c
• postorder = g h d i e b j f c a
• Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.
Inorder And Level Order

• Scan level order from left to right using


inorder to separate left and right subtrees.
• inorder = g d h b e i a f j c
• level order = a b c d e f g h i j
• Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.
(a + b) * (c + d) + e – f/g*h + 3.25

You might also like