Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Tree

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Mr.

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.

 The depth of a node is the number of edges from that node to


the tree’s root node. As such, the depth of the whole tree would
be the depth of its deepest leaf node. The root node has a
depth of 0.
Binary Tree:-
 It is a finite set of data items which is either empty or consists of a
single item called the root and two disjoint binary tree called as left
subtree and right subtree.
 It is a non-linear data structure.
 The maximum degree of any node is at most 2.
 It is used for easy and quick access to data, in router algorithms, to
implement heap data structure, in syntax tree etc.

Strictly Binary Tree:-


 If every non-terminal node in a binary tree consist of non-empty left
subtree and right subtree, then it is a strictly binary tree.
Complete Binary Tree:-
 In a complete binary tree, there is exactly one node at level 0, 2 nodes at level 1, 4 nodes at level
2 and so on.
Extended Binary Tree:-
 A binary tree T is said to be a 2-tree or an extended binary
tree if each node N has either 0 or 2 children.
 Nodes with 2 children are internal node.
 Nodes with 0 children are external node.
 Array Representation:-

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:-

Node Structure of a Binary Tree


Logical Representation of a Node:-
struct node
{
char data;
struct node *lchild;
struct node *rchild;
};
typedef struct node BTNODE;
 A) Inorder:-
 Traverse the left subtree in inorder(L).
 Visit the root node.
 Traverse the right subtree in inorder(R).
 B) Preorder:-
 Visit the root node.
 Traverse the left subtree in preorder(L).
 Traverse the right subtree in preorder(R).
 C) Postorder:-
 Traverse the left subtree in postorder(L).
 Traverse the right subtree in postorder(R).
 Visit the root node.
A BST is a binary tree which is either empty or satisfies the following rules:-
 The value of the key in the left child or left subtree is less than the value of the root.
 The value of the key in the right child or right subtree is more than the value of the root.
 All the subtrees of the left and right children observe these two rules as well.
 Binary Search Tree(BST) insertion take place at a leaf or leaf like node.

 To insert a node in a BST, we must check whether the tree already contains any
nodes.

 If the tree is empty, the node is placed in the root node.

 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.

You might also like