BEGINNER
1. Nodes
2. Parent Nodes & Child Nodes
3. Leaf Nodes
4. Root Node
5. Sub Tree
6. Level of a tree
7. m-ary Tree
8. Binary Tree (BT)
9. Binary Search Tree (BST)
10. BST - Insert, Delete
2. DATA STRUCTURE - TREE
Many applications are
hierarchical in nature.
Linear data structures are not
appropriate for these type of
applications.
Tree 2
President
Vice President
Executive Executive
Vice President
Executive
Vice President
Executive Executive
Mashrafi Khan
Mairufa Khan
Junayed
Ahmed
Masrufa
Ahmed
Fahim Khan
Ashfar
Khan
Moin Khan
Imran
Khan
Bushra
Khan
Faizul Khan
Mahboob
Khan
Mashrufa
Khan
Mashraba
Khan
Hierarchical structure of a company
Genealogy tree of a family
3. COMMON USE OF TREE AS A DATA STRUCTURE
Representing hierarchical data
Storing data in a way that makes it easily searchable
Representing sorted lists of data
As a workflow for compositing digital images for visual effects
Routing algorithms
Tree 3
4. TREE - DEFINITION
As a data structure, a tree consists of one
or more nodes, where each node has a
value and a list of references to other (its
children) nodes.
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 4
A
B C D
E F JIHG
K ML ON
ROOT
VALUE
CHILDCHILD
5. TREE - DEFINITION
Generally it is considered that in a tree,
there is only one path going from one
node to another node.
There cannot be any cycle or loop.
The link from a node to other node is
called an edge.
An arrowed edge
indicates flow from P to Q.
An straight line edge
indicates flow from P to Q and Q to P.
Tree 5
A
B C D
E F JIHG
K ML ON
If node F is reached through node B, than the link from
node K to node F will not be considered.
If link from node L to node C is considered, than there
will be a cycle among nodes C, G, and L.
P Q
P Q
A straight line is generally used to represent the links
between the nodes of a tree.
6. TREE - DEFINITION
Nodes
Parent Nodes & Child Nodes
Leaf Nodes: nodes with no child
Root Node: node with no parent
Sub Tree: the tree rooted by a child
Level of a tree:
Root at level 0;
Each children have the level one more than
its parent.
Height/depth of the tree: Total
number of Levels
Height of a node: Total number of
levels from bottom
[Tree height – node level].
Tree 6
A
B C D
E F JIHG
K ML ON
LEVEL
0
1
2
3
Height of this tree is 4, as there are four levels (0…3).
Height of root A is 4;
Height of nodes B, C, D is 3;
Height of E, F, G, H, I, J is 2;
Height of nodes K, L , M, N, O is 1.
7. m-ARY TREE
A Tree is an m-ary Tree when each of its node has no more than m children.
Tree 7
A
B C D
E F JIHG
K ML ON
A
B D
E F IH
GK
ML
ON
2-ary tree 3-ary tree
8. BINARY TREE (BT)
Each node of a binary Tree has at most 2 children.
Tree 8
A
B D
E F IH
GK
ML
ON
Binary tree
9. BINARY SEARCH TREE (BST)
Is a Binary Tree such that:
Every node entry has a unique key (i.e. no duplication item).
All the keys in the left subtree of a node are less than the key of the node.
All the keys in the right subtree of a node are greater than the key of the node.
Tree 9
43
31 64
20 40 8956
3328 47 59
Fred
Dan Mary
Alan Eve SueKate
EricBill Greg Len
Integer Key String Key
10. BST - INSERT
Tree 10
43
31 64
20 40 8956
3328 47 59
> <
< <
<><
>
>
>
Fred
Dan Mary
Alan Eve SueKate
EricBill Greg Len
> <
< >
> <<
>
>
<
Integer Key String Key
43 31 64 40 20 89 56 47 33 28 59Fred Mary Kate Dan Len Alan Eve Bill Sue Greg Eric
12. BST - DELETE
Delete 47 (leaf node);
Delete 40 (have only one child);
Delete 64 and 31 (have both child).
Tree 12
43
31 64
20 40 8956
3828 47 59
32
33
5932
Leaf Node: Just DeleteLeaf Node: Just Delete
Node with one child:
connect the parent to
the child and Delete
Node with one child: connect the
parent to the child and Delete
Node with two
children on right
subtree: Replace with
the child with highest
value either from left
or right subtree
Node with two
children on left
subtree: Replace with
the child with lowest
value either from left
or right subtree