0% found this document useful (0 votes)
12 views62 pages

Tree 1

This document discusses tree data structures and various tree traversal algorithms. It begins with an introduction to tree terminology and representations of trees. It then focuses on binary trees, their properties, and different ways to represent them. The document explains three common tree traversal algorithms - preorder, inorder, and postorder traversal - and provides examples of applying each to both general trees and binary expression trees. It also discusses level order traversal and additional binary tree operations like threaded binary trees and heaps.

Uploaded by

Tejus V S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
12 views62 pages

Tree 1

This document discusses tree data structures and various tree traversal algorithms. It begins with an introduction to tree terminology and representations of trees. It then focuses on binary trees, their properties, and different ways to represent them. The document explains three common tree traversal algorithms - preorder, inorder, and postorder traversal - and provides examples of applying each to both general trees and binary expression trees. It also discusses level order traversal and additional binary tree operations like threaded binary trees and heaps.

Uploaded by

Tejus V S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 62

Tree

Data Structure
Vani M.
Trees: Outline
 Introduction
 Representation Of Trees

 Binary Trees

 Binary Tree Traversals

 Additional Binary Tree Operations

 Threaded Binary Trees

 Heaps

 Binary Search Trees


Introduction (1/8)
 A tree structure means that the data are organized so
that items of information are related by branches

 Examples:
Introduction (2/8)
 Definition (recursively): A tree is a finite set of
one or more nodes such that
 There is a specially designated node called root.
 The remaining nodes are partitioned into n>=0 disjoint
set T1,…,Tn, where each of these sets is a tree. T1,…,Tn
are called the subtrees of the root.

 Every node in the tree is the root of some subtree


Introduction (3/8)
 Some Terminology
 node: the item of information plus the branches to each node.
 degree: the number of subtrees of a node
 degree of a tree: the maximum of the degree of the nodes in the
tree.
 terminal nodes (or leaf): nodes that have degree zero
 non terminal nodes: nodes that don’t belong to terminal
nodes.
 children: the roots of the subtrees of a node X are the children of
X
 parent: X is the parent of its children.
Introduction (4/8)
 Some Terminology (cont’d)
 siblings: children of the same parent are said to be
siblings.
 Ancestors of a node: all the nodes along the path from
the root to that node.
 The level of a node: defined by letting the root be at
level one. If a node is at level l, then it children are at
level l+1.
 Height (or depth): the maximum level of any node in the
tree
 Example
Introduction (5/8)
A is the root node
B is the parent of D and E Property: (# edges) = (#nodes) - 1
C is the sibling of B
D and E are the children of B
D, E, F, G, I are external nodes, or leaves
A, B, C, H are internal nodes
The level of E is 3 Level
The height (depth) of the tree is 4
A
The degree of node B is 2 1
The degree of the tree is 3
The ancestors of node I is A, C, H B C
2
The descendants of node C is F, G, H, I

H
3
D E F G
I
4
Introduction (6/8)
 Representation of Trees
 List Representation
 we can write of Figure 5.2 as a list in which each of the subtrees is
also a list
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
 The root comes first,
followed by a list of sub-trees
Introduction (7/8)
 Representation of
Trees (cont’d)
 Left Child-
Right Sibling
Representation
Introduction (8/8)
 Representation of Trees (cont’d)
 Representation
as a Degree
Two Tree
Binary Trees (1/9)
 Binary trees are characterized by the fact that any
node can have at most two branches
 Definition (recursive):
 A binary tree is a finite set of nodes that is either empty or
consists of a root and two disjoint binary trees called the left
subtree and the right subtree

 Thus the left subtree and the right subtree are


distinguished
A A

B B
 Any tree can be transformed into binary tree
 by left child-right sibling representation
Binary Trees (2/9)
 The abstract data type of binary tree
Binary Trees (3/9)
 Two special kinds of binary trees:
(a) skewed tree, (b) complete binary tree
 The all leaf nodes of these trees are on two adjacent levels
Binary Trees (4/9)
 Properties of binary trees
 Lemma 1 [Maximum number of nodes]:
1. The maximum number of nodes on level i of a binary tree
is 2i-1, i 1.
2. The maximum number of nodes in a binary tree of depth k
is 2k-1, k1.
3. Lemma 2 [Relation between number of leaf nodes and degree-
2 nodes]:
For any nonempty binary tree, T, if n0 is the number of leaf
nodes and n2 is the number of nodes of degree 2, then n0 =
n2 + 1.

1. These lemmas allow us to define full and complete


binary trees
Binary Trees (5/9)
 Definition:
 A full binary tree of depth k is a binary tree of depth k
having 2k-1 nodes, k  0.
 A binary tree with n nodes and depth k is complete iff its
nodes correspond to the nodes numbered from 1 to n in
the full binary tree of depth k.
 From Lemma 1, the
height of a complete
binary tree with n nodes
is log2(n+1)
Binary Trees (6/9)
 Binary tree representations (using array)
 Lemma 3: If a complete binary tree with n nodes is
represented sequentially, then for any node with index
i, 1  i  n, we have
 parent(i) is at i /2 if i  1.
If i = 1, i is at the root and has no parent.
 LeftChild(i) is at 2i if 2i  n.
If 2i  n, then i has no left child.
 RightChild(i) is at 2i+1 if 2i+1  n. A 1
If 2i +1  n, then i has no right child
[1] [2] [3] [4] [5] [6] [7] B 2 C 3
A B C — D — E
Level 1 D E
Level 2 Level 3 4 5 6 7
Binary Trees (7/9)
 Binary tree representations (using array)
 Waste spaces: in the worst case, a skewed tree of depth k
requires 2k-1 spaces. Of these, only k spaces will be
occupied
 Insertion or deletion
of nodes from the
middle of a tree
requires the
movement of
potentially many nodes
to reflect the change in
the level of these nodes
Binary Trees (8/9)
 Binary tree representations (using link)
Binary Trees (9/9)
 Binary tree representations (using link)
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 Traversals
 How to traverse a tree or visit each node in the tree
exactly once?
 There are six possible combinations of traversal

LVR, LRV, VLR, VRL, RVL, RLV


 Adopt convention that we traverse left before
right, only 3 traversals remain

LVR (inorder), LRV (postorder), VLR (preorder)

left_child data right_child


V
L: moving left : R: moving right
visiting
node
Binary Tree Traversal Methods
 Preorder

 Inorder

 Postorder

 Level order
Binary Tree Traversals
 Arithmetic Expression using binary tree
 inorder traversal (infix expression)
A/B*C*D+E
 preorder traversal (prefix expression)
+**/ABCDE
 postorder traversal
(postfix expression)
AB/C*D*E+
 level order traversal
+*E*D/CAB
Preorder Traversal
void preOrder(treePointer ptr)

if (ptr != NULL)

{ visit(t);

preOrder(ptr->leftChild);

preOrder(ptr->rightChild);

}
Preorder Example (Visit = print)
a

b c

a b c
Preorder Example (Visit = print)
a

b c
f
d e
g h i j

a b d g h e i c f j
Preorder of Expression Tree
/

* +

e f
+ -
a b c d

/ * + a b - c d + e f

Gives prefix form of expression!


Binary Tree Traversals
 Preorder traversal (VLR) (recursive version)

output + * * / A B C D E
:

V
L
R
Inorder Traversal
void inOrder(treePointer ptr)
{ if (ptr != NULL)
{
inOrder(ptr->leftChild);
visit(ptr);
inOrder(ptr->rightChild);
}
}
Inorder Example (Visit = print)
a

b c

b a c
Inorder Example (Visit = print)
a

b c
f
d e
g h i j

g d h b e i a f j c
Inorder By Projection
a

b c
f
d e
g h i j

g d h b e i a f j c
Inorder of Expression Tree
/

* +

e f
+ -

a b c d

a + b * c - d / e + f

Gives infix form of expression


Binary Tree Traversals
 Inorder traversal (LVR) (recursive version)

output A / B * C * D + E
:
ptr
L
V
R
Binary Tree Traversals
 Iterative inorder traversal
 we use a stack to simulate recursion
5 4 11
8 3 14
2 17
1
A B
/ *C D
* E
+

L
V

output A / B*C * D + E node


:
Binary Tree Traversals
 Analysis of inorder2 (Non-recursive Inorder traversal)
 Let n be the number of nodes in the tree
 Time complexity: O(n)
 Every node of the tree is placed on and removed
from the stack exactly once
 Space complexity: O(n)
 equal to the depth of the tree which
(skewed tree is the worst case)
Postorder Traversal
void postOrder(treePointer ptr)
{if (ptr != NULL)
{
postOrder(ptr->leftChild);
postOrder(ptr->rightChild);
visit(t);
}
}
Postorder Example (Visit = print)
a

b c

b c a
Postorder Example (Visit = print)
a

b c
f
d e
g h i j

g h d i e b j f c a
Postorder of Expression Tree
/

* +

e f
+ -

a b c d

a b + c d - * e f + /

Gives postfix form of expression!


Binary Tree Traversals
 Postorder traversal (LRV) (recursive version)

output A B / C * D * E +
:

L
R
V
Traversal Applications
a

b c
f
d e
g h i j
• Make a clone.
• Determine height.
•Determine number of nodes.
Binary Tree Traversals
 Level-order traversal
 method:
 We visit the root first, then the root’s left child, followed by the
root’s right child.
 We continue in this manner, visiting the nodes at each new level
from the leftmost node to the rightmost nodes
 This traversal requires a queue to implement
Level Order
Let ptr be a pointer to the tree root.
while (ptr != NULL)
{ visit node pointed at by ptr and put its children
on a FIFO queue;
if FIFO queue is empty, set ptr = NULL;
otherwise, delete a node from the FIFO queue
and call it ptr;
}
Level-Order Example (Visit = print)
a

b c
f
d e
g h i j

a b c d e f g h i j
Binary Tree Traversals
 Level-order traversal (using queue)

output + * E * D / C A B
:
1 17 3 14 4 11 5
2 8
*+ E * D / C A B

FIFO

ptr
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 = ab
b a
a b

postorder = ab
b b
a a

level order = ab
a a
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.

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
gd ei
h
Inorder And Preorder
a

b fjc
gd ei
 preorder
h = abdgheicf j
 d is the next root; g is in the left
subtree; h is inathe right subtree.
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.
Additional Binary Tree Operations (1/7)
 Copying Binary Trees
 we can modify the postorder traversal algorithm only
slightly to copy the binary tree

similar as
Program 5.3
Additional Binary Tree Operations
(2/7)
 Testing Equality
 Binary trees are equivalent if they have the same
topology and the information in corresponding nodes
is identical V L R

the same topology and data as Program 5.6


Additional Binary Tree Operations (3/7)
 Variables: x1, x2, …, xn can hold only of two possible
values, true or false

 Operators: (and), (or), ¬(not)

 Propositional Calculus Expression


 A variable is an expression
 If x and y are expressions, then ¬x, xy, xy are expressions
 Parentheses can be used to alter the normal order of
evaluation (¬ >  > )
 Example: x1  (x2  ¬x3)
Additional Binary Tree Operations (4/7)
 Satisfiability problem:
 Is there an assignment to make an expression true?

 Solution for the Example x1  (x2  ¬x3) :


 If x1 and x3 are false and x2 is true
 false  (true  ¬false) = false  true = true

 For n value of an expression, there are 2n possible


combinations of true and false
Additional Binary Tree Operations (5/7)
(x1  ¬x2)  (¬ x1  x3)  ¬x3

postorder traversal 

data  

value   X3

X1   X3

X2 X1
Additional Binary Tree Operations (6/7)
 node structure
 For the purpose of our evaluation algorithm, we assume
each node has four fields:

 We define this node structure in C as:


Additional Binary Tree Operations (7/7)
 Satisfiability function
 To evaluate the tree is
easily obtained by
modifying the original L
recursive postorder R
V
traversal
TRUE
node

TRUE
FALSE

FALSE
TRUE T
TRUE

F
FALSE FALSE
T TRUE
TRUE

F T
FALSE TRUE

You might also like