0% found this document useful (0 votes)
8 views11 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)
8 views11 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/ 11

Trees

Topics covered
 Trees
 Definitions and terminology

 Binary trees
 Definitions and property

 Binary trees case studies:


 Depth first values problem
 Depth First Search (DFS)
 Breadth first values problem
 Breadth First Search (BFS)
 Tree “includes” problem
Tree
 Definition
 A finite set of one or more nodes
 One node is root
 Root node in tree below is A
 And each one of the remaining nodes has
 Only one parent
A

B C

D E
F G H
A

Terminology B C

 In this tree D E
F G H
 A is the root node
Subtree rooted at C
 B is the parent of D and E
 C is the sibling of B
 D and E are the children of B
 D, E, F, G, and H are leaves (or external nodes)
 A, B, and C are internal nodes

 The subtree rooted at C includes


 the C, F, G, and H nodes
A

Binary Tree B C

D E F G
 Properties
 Every node has at most two children
 Denoted by left and right children
 Subtree rooted at left child => left subtree
 Subtree rooted at right child => right subtree

 In a proper binary tree


 Each node has either zero or two children
 Otherwise, it is called improper
Depth of a node
 The depth of a tree node
 Is the number of ancestors of the node
 Depth of root node = 0
 In tree below, depth(D) = 2, since it has 2 ancestors, A and B

 Nodes of same depth form a level

Level 0 Depth = 0
A

Level 1 Depth = 1 C
B

Level 2 F G
D E Depth = 2
Height of a node/tree
 Height of a node
 If it is external, then height = 0
 Otherwise, height = 1 + height of max height of children

 Height of a tree = height of root


 It is also equal to depth of deepest node

Height of this tree = 2 A

B Height of C = 1 C

D E Height of F = 0 F G
Binary tree representation
BTNode A element

left right

B C

D E F

 BTNode<T> class
 T element
 BTNode<T> left
 BTNode<T> right
Binary tree case study 1:
depth first values A
 Depth first traversal
 of binary tree
B C
 Storing elements in an array list

 This results in following list D E F


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

 Solutions
 Iterative solution: Stack-based

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

 This results in following list D E F


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

 Solution
 Iterative solution:
 Queue-based
Binary tree case study 3:
tree includes
 Objective A
 Find a target node value in the tree

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

 Solution
 Recursive traversal-based solution

You might also like