Lecture 09.1
Lecture 09.1
1. Tree
2. Basic Terminology
3. Binary Tree
• Complete Binary Tree
• Full Binary Tree
• Balanced Tree
• Tree Traversal
Tree
Introduction
President
A tree must have a node designated as root. If a tree has only one
node, that node is the root node. Root is never referenced by any other
node.
Having more than one node indicates that the root have some (at
least one) references to its children nodes and the children nodes
(might) have references to their children nodes and so on.
Tree
An example
VALUE ROOT
A
CHILD CHILD
B C D
E F G H I J
K L M N O
Tree
Terminologies
A tree must have a node designated as root. If a tree has only one
node, that node is the root node. Root is never referenced by any other
node.
Having more than one node indicates that the root have some (at
least one) references to its children nodes and the children nodes
(might) have references to their children nodes and so on.
Tree
Definition
If
AIfnode
link from
F isline
straight reached
node L to
through
node
is generally Cnode
usedisto
considered,
B, than thethan
represent linklinks
the there
from
node
will be
betweenK to
a the
cycle
node among
F willofnot
nodes anodes
be considered.
tree. C, G, and L.
Tree
Terminologies
Nodes LEVEL
0 A
Parent Nodes & Child Nodes
Leaf Nodes: nodes with no child
Root Node: node with no parent
Sub Tree: the tree rooted by a 1 B C D
child
Level of a tree:
Root at level 0;
Each children have 2 E H I J
Height of root A the
is 4;level F G
one more
Height than its
of nodes parent.
B, C, D is 3;
Height/depth
Height of E,of
F, the
G, H,tree:
I, J isTotal
2;
number
Height of
of Levels
nodes K, L , M, N, O is 1.
Height of a node: Total number
of levels from bottom
[Tree height – node level]. 3 K L M N O
Height of this tree is 4, as there are four levels (0…3).
Tree
m-ary tree
A Tree is an m-ary Tree when each of its node has no more than m children.
A
A 3-ary tree
B D
C D
E F H I B
H I J
K G N O E F G
L M 2-ary tree K L M N O
Binary Tree
Definition and example
E F H I
K G N O
L M Binary tree
Complete and Full Binary Tree
• A complete/full binary tree is a binary
tree, which is completely filled with A
nodes from top to bottom and left to
right.
• The tree starts from the root (top),
B D
goes to the next level with first the
left child and then the right child (left
to right). The process repeats for each
next level with each node till the last E F H I
(bottom) level.
• In complete binary tree some of the
nodes only for the bottom level might
be absent (here nodes after N). K L G M N C J P
• In Full binary tree the last/bottom
level must also be filled up.
Full Binary Tree
#Nodes at each level L = 2L
Level=0, #Nodes in a full binary tree with L labels is
A
# of nodes=20=1
If total number of nodes is n,
Level=1, Height of the tree is Log2n
B D
# of nodes=21=2
Level=2,
E F H I
# of nodes=22=4
Level=3,
K L G M N C J P
# of nodes=23=8
Complete Binary Tree
Exercise
A full binary tree is always a complete binary tree but vice versa may not be true.
Why?
A complete binary tree has L labels. How many nodes it may have in both
minimum and maximum cases?
A complete binary tree has L labels. At most, how many more nodes are required
to make it a full binary tree?
Tree Traversal
Introduction
B A D
A
B H D
E F
M E F C H
K L
K L M C
Tree Traversal
Inorder
E B F A D
K L E F M H
K L M C
Tree Traversal
Inorder
K E L B F M A H D
K L M CH
C
Tree Traversal
Inorder
K E L B F M A H C D
C
Tree Traversal
Inorder
Traversing order A
left subtree
parent
B D
right subtree
E F H
K L M C
K E L B F M A H C D
Tree Traversal
Preorder
Traversing order A
parent
left subtree
B D
right subtree
E F H
K L M C
A B E K L F M D H C
Tree Traversal
Postorder
Traversing order A
left subtree
right subtree
parent B D
E F H
K L M C
K L E M F B C H D A
Tree Traversal
Exercise
1. https://en.wikipedia.org/wiki/Binary_Search_Tree
Books
“Schaum's Outline of Data Structures with C++”. By John R. Hubbard
“Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
“Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
“Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
“Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
“C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
“Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,