0% found this document useful (0 votes)
17 views14 pages

Trees

Uploaded by

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

Trees

Uploaded by

khourycarl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Topics covered

 Binary trees case studies:


 Depth first values problem
 Depth First Search (DFS)
 Breadth first values problem
 Breadth First Search (BFS)
 Tree “includes” problem
 Tree “sum” problem
 Tree “min” problem
 Max path sum problem
 Binary tree traversal algorithms
 Full/complete binary trees
Binary tree case study 1:
depth first values
 Depth first traversal
 of binary tree
 Storing elements in an array list A

 This results in following list B C


 A, B, D, E, C, F

D E F
 Solutions
 Iterative solution: Stack-based

 Recursive solution
Binary tree case study 2:
breadth first values
 Breadth first traversal (level-order
traversal)
 of binary tree
A
 Storing elements in an array list

 This results in following list B C

 A, B, C, D, E, F
D E F
 Solution
 Iterative solution:
 Queue-based
Binary tree case study 3:
tree includes
A
 Objective
 Find a target node value in the tree
B C

 This results in
D E F
 True if value is found in tree
 False, otherwise

 Solution
 Recursive traversal-based solution
Binary tree case study 4:
tree sum
 Objective 3
 Find total sum of values in binary tree
11 4
 This results in 25
 For tree given on right-hand side
4 2 1

 Solution
 Recursive traversal-based solution
Binary tree case study 5:
tree min
 Objective 3
 Find minimum value in binary tree
11 4
 This results in 1
 For tree given on right-hand side
4 2 1

 Solution
 Recursive traversal-based solution
Binary tree case study 6:
max path sum
 Objective 3
 Find maximum path sum
11 4
 This results in 18
 For tree given on right-hand side
4 2 1

 Solution
 Recursive traversal-based solution
Binary tree traversal
 Processing all the nodes by
 visiting each individual node exactly once
 Pre-order (VLR)

 Post-order (LRV) V

 In-order traversal (LVR)


L R
Pre-order traversal
 In a pre-order traversal
 a node is visited before its left subtree

 And before its right subtree

Algorithm PreOrder(T,v)
visit(v)
if T.hasLeft(v)
PreOrder(T, T.left(v))
if T.isRight(v)
PreOrder(T, T.Right(v))
Post-order traversal
 In a post-order traversal
 a node is visited after its left subtree

 And after its right subtree

Algorithm PostOrder(T,v)
if T.hasLeft(v)
PostOrder(T,T.left(v))
if T.hasRight(v)
PostOrder(T,T.right(v))
visit(v)
In-order traversal
 In an in-order traversal
 a node is visited after its left subtree

 And before its right subtree

Algorithm InOrder(T,v)
if T.hasLeft(v)
InOrder(T,T.left(v))
visit(v)
if T.hasRight(v)
InOrder(T,T.right(v))
Full binary tree
 Full binary tree of height k
 Binary tree of depth k having 2 k+1 -1 nodes

 Only nodes at depth k are leaves


 and all the internal nodes have two children

 You can number nodes in the tree


 Root 1
 Nodes at any level are numbered from left to right
1
 Parent of node i is numbered floor(i/2)
 Left child of i is numbered 2i
2 3
 Right child of i is numbered 2i+1

4 5 6 7
Complete binary tree
 A complete binary tree of height k with n nodes
 Its nodes corresponds to the nodes

 numbered one to n in the full binary tree of height k

1 1 1

2 3 2 3 2 3

4 5 6 7 4 5 4 5 7

FULL/COMPLETE COMPLETE NOT COMPLETE


More properties for binary trees
 For a proper binary tree:
 nb of external nodes = nb of internal nodes + 1

 The height of a full binary tree with n nodes is


 log2(n+1)-1

 The height of a complete binary tree with n nodes is


 log2(n+1)-1 <= h < log2(n+1)

You might also like