Tree Data Structure.docx
Tree Data Structure.docx
Tree is a non-linear data structure which organizes data in a hierarchical structure and this is a recursive definition.
A tree is a connected graph without any circuits.
If in a graph, there is one and only one path between every pair of vertices, then graph is called as a tree.
Example:
Properties-
Example-
3. Parent-
● The node which has a branch from it to any other node is called as a parent node.
● In other words, the node which has one or more children is called as a parent node.
● In a tree, a parent node can have any number of child nodes.
Example-
● Node A is the parent of nodes B and C , Node B is the parent of nodes D, E and F.
● Node C is the parent of nodes G and H. Node E is the parent of nodes I and J
● Node G is the parent of node K
4. Child-
Example-
Here,
● Nodes B and C are the children of node A . Nodes D, E and F are the children of node B
● Nodes G and H are the children of node C. Nodes I and J are the children of node E
● Node K is the child of node G
5. Siblings-
Nodes which belong to the same parent are called as siblings.In other words, nodes with the same parent are sibling nodes.
Example:
Here,
● Nodes B and C are siblings. Nodes D, E and F are siblings
● Nodes G and H are siblings. Nodes I and J are siblings
6. Degree- Degree of a node is the total number of children of that node.
● Degree of a tree is the highest degree of a node among all the nodes in the tree.
Example-
Here,
● Degree of node A = 2 Degree of node B = 3 Degree of node C = 2 Degree of node D = 0
● Degree of node E = 2 Degree of node F = 0 Degree of node G = 1 Degree of node H = 0
● Degree of node I = 0 Degree of node J = 0 Degree of node K = 0
7. Internal Node- The node which has at least one child is called as an internal node.
● Internal nodes are also called as non-terminal nodes. Every non-leaf node is an internal node.
Example-
Here, nodes A, B, C, E and G are internal nodes.
8. Leaf Node-
● The node which does not have any child is called as a leaf node.
● Leaf nodes are also called as external nodes or terminal nodes.
Here, nodes D, I, J, F, K and H are leaf nodes.
9. Level-
In a tree, each step from top to bottom is called as level of a tree.
● The level count starts with 0 and increments by 1 at each level or step.
Example-
10. Height-
● Total number of edges that lies on the longest path from any leaf node to a particular node is called as height of that
node.
● Height of a tree is the height of root node.
● Height of all leaf nodes = 0
Example-
Example-
Here,
● Depth of node A = 0 Depth of node B = 1 Depth of node C = 1 Depth of node D = 2
● Depth of node E = 2 Depth of node F = 2 Depth of node G = 2 Depth of node H = 2
● Depth of node I = 3 Depth of node J = 3 Depth of node K = 3
12. Subtree- In a tree, each child from a node forms a subtree recursively. Every child node forms a subtree on its
parent node.
Example-
Example-
Unlabeled Binary Tree- A binary tree is unlabeled if its nodes are not assigned any label.
TYPES OF BINARY TREE:
A rooted binary tree is a binary tree that satisfies the following 2 properties-
● It has a root node.
● Each node has at most 2 children.
Example-
● A binary tree in which every node has either 0 or 2 children is called as a Full binary tree.
● Full binary tree is also called as Strictly binary tree.
Example-
Here,
● First binary tree is not a full binary tree.
● This is because node C has only 1 child.
3. Complete / Perfect Binary Tree-
A complete binary tree is a binary tree that satisfies the following 2 properties-
● Every internal node has exactly 2 children.
● All the leaf nodes are at the same level.
Example-
4. Almost Complete Binary Tree-
An almost complete binary tree is a binary tree that satisfies the following 2 properties-
● All the levels are completely filled except possibly the last level.
● The last level must be strictly filled from left to right.
Example-
A skewed binary tree is a binary tree that satisfies the following 2 properties-
● All the nodes except one node has one and only one child.
● The remaining node has no child.
OR
A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
Example-
DEPTH=4 =2^4-1=15
A B C D
1 2
Representation of binary trees
2 ways :
1: Sequential representation of Binary Tree(Array)
Let us consider that we have a tree T. let our tree T is a binary tree that us complete binary tree. Then there is an efficient way of
representing T in the memory called the sequential representation or array representation of T. This representation uses only a linear
array TREE as follows:
The root N of T is stored in TREE [1].If a node occupies TREE [k] then its left child is stored in TREE [2 * k] and its right child is
2^4-1=16-1=15
A B C D - E F - H
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1=16-1=15
Parent(B)= PARENT(2)=i/2=2/2=1
Right child(B)=2i+1=2+1=5
This is the more common representation. All binary trees consist of a root pointer that points in the direction of the
root node. When you see a root node pointing towards null or 0, you should know that you are dealing with an
empty binary tree. The right and left pointers store the address of the right and left children of the tree.
Binary search tree: q
null 15 252
nu 17 nul
ll l
15,17,13,8,9,17,16,18
Temp252
Step 1: root =NULL key = 15
Step 3: Key=13,key<15
Key<11,
key=8
key=9
tushar ,amit,beena,pranav,hemant,neeta,rohit,deepa
tushar ,amit,beena,pranav,hemant,neeta,rohit,deepa
abcdefghijklmnopqrstuvwxyz
Jan,dec,feb,aug,may,jun,apr,nov