Tree
Tree
Tree
Abhijit Sarkar
Asst. Professor
Dept. of CSE (Cyber Security)
Haldia Institute of Technology
Tree is a non-linear data structure in which items are arranged in a sorted sequence.
It is used to represent hierarchical relationship existing amongst several data items.
It is a finite set of one or more data items such that:
There is a special data item called the root of the tree.
And its remaining data items are partitioned into number of mutually exclusive (disjoint) subsets, each of
which is itself a tree. They are called as subtrees.
Degree of Node:- It is the number of subtrees of a node.
Example:- Degree of Node A: 3
Degree of Node C: 1
Degree of Node B: 2
Degree of Tree:- It is the maximum degree of nodes
in a given tree.
Example:- Degree of the shown tree: 3
Terminal Node:- A node with degree 0 is called terminal
node.
Example:- E,J,G,H,K,L,M.
Non-terminal Node:- Any node (except the root node) whose degree is not zero is
called non-terminal node.
Example:- B,F,C,D,I.
Siblings:- The children nodes of a given parent node
are called siblings.
Example:- E and F are siblings of parent node B.
Level:- The entire tree structure is levelled in such a way
that root node is always at level 0. Its immediate
children are at level 1 and their immediate
children are at level 2 and so on up to the terminal
nodes.
Edge:- It is a connecting line of two nodes.
Path:- It is a sequence of consecutive edges from the source node to destination node.
Example:- Path between A & J is (A,B),(B,F) and (F,J).
Forest:- It is a set of disjoint trees. In a given tree, if you remove the root node, then it
becomes a forest.
The height of a node is the number of edges on the longest
path from that node to a leaf node.
As such, the height of the tree would be the height of its root
node. Meanwhile, the leaf nodes have a height of 0.
A A
0 0
B 1 C 2 B 1 C 2
3 4 5 6
D E F G
Parent (n) floor((n-1)/2)
Leftchild(n) 2n+1
Rightchild(n) 2n+2
Siblings(n) n-1 or n+1
Linked List Representation:-
To insert a node in a BST, we must check whether the tree already contains any
nodes.
If the tree is not empty, then proper location is found and the added node becomes
either a left or a right child of an existing node.