introduction of tree. introduction a tree is a non-linear data structure in which items are arranged...

27
Introduction Of Tree

Upload: hilary-stewart

Post on 31-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Introduction Of

Tree

Page 2: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Introduction A tree is a non-linear data structure in which items are

arranged in sequence. It is used to represent hierarchical relationship existing

amongst several data items. The graph theoretic definition of tree is: It is a finite set

of one or more data items (nodes) such that.1. There is a special data item called the root of the tree.

2. And its remaining data items are partitioned into number of mutually exclusive (disjoint) subsets, each of which is itself a tree. And they are called sub trees.

Page 3: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Introduction Another way we can say, A class of graphs that is

acyclic is termed as trees. A tree is shown bellow:

Page 4: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Tree Terminology There are number of designed data item in a tree

which is listed below:

1. Root

2. Node

3. Degree of a node

4. Degree of a tree

5. Terminal node

6. Non-terminal node

Page 5: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Tree Terminology

7. Siblings

8. Level

9. Edge

10. Path

11. Depth

12. Forest

Page 6: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Tree Terminology Root It is specially designed data item in a tree. It is the

first in the hierarchical arrangement of data items. In the above tree, A is the root item.

Node Each data item in a tree is called a node. It is the

basic structure in a tree. It specifies the data information and links (branches) to other data items. There are 13 nodes in the above tree.

Page 7: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Tree Terminology Degree of a node It is the number of sub trees of a node in a given

tree. In the above tree. The degree of node A is 3 The degree of node C is 1 The degree of node B is 2 The degree of node H is 0 The degree of node I is 3

Page 8: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Tree Terminology Degree of a tree It is the maximum degree of nodes in a given tree.

In the above tree the node A has degree 3 and another node I is also having its degree 3. In all this value is the maximum. So, the degree of the above tree is 3.

Page 9: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Tree Terminology Terminal node A node with degree zero is called a terminal node or

a leaf node. In the above tree, there are 7 terminal nodes. They are E, J, G, H, K, L and M.

Non-terminal node Any node (except the root node) whose degree is

not zero is called non-terminal node. Non-terminal nodes are the intermediate nodes in traversing the given tree from its root node to the terminal node (leaves). There are 5 non-terminal nodes. 

Page 10: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Tree Terminology Siblings The children nodes of a given parent node are called

siblings. They are also called brothers. In the above tree, E and F are siblings of parent node B. K, L and M are siblings of parent node I.

Page 11: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Tree Terminology Level The entire tree structure is level in such a way that

the root node is always at level 0. Then, its immediate children are at level 1 and their immediate children are at level 2 and so on up to the terminal nodes.

Page 12: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Tree Terminology Edge It is a connecting line of two nodes. That is the line

draw from one node to another node is called an edge.

Path It is sequence of consecutive edges from the source

node to the destination node. In the above tree the path between A and J is given by the node pairs.

(A,B) (B,F) and (F,J)

Page 13: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Tree Terminology Depth It is the maximum level of any node in a given tree.

In the above tree the root node A has the maximum level. That is the number of levels one can descend the tree from its root to the terminal nodes (leaves). The term height is also used to denote the depth.

Page 14: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Tree Terminology Forest It is a set of disjoint trees. In a given tree, if we

remove its root node then it becomes a forest in the above tree, there is forest with three trees.

Page 15: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Binary Tree

A binary tree is finite set of data items which is either empty of consists of a single item called the root and two disjoint binary trees called the left sub tree and right sub tree.

A binary tree is very important and the most commonly used non-linear data structure. In a binary tree the maximum degree of any node is at most two.

That means, there may be zero degree node (empty tree) or a one degree node and two degree node.

Page 16: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Binary Tree

Show bellow binary tree consisting of 10 elements.

Page 17: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Binary Tree

In the above binary tree, A is the root of the tree. The left sub tree consists of the tree with root B. and the right sub tree consists of the tree with root C.

Further B has its left sub tree with root D and right sub tree with root E.

Similarly, C has its left sub tree with root F and its right sub tree with root G.

Page 18: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Binary Tree

In the next level, D has an empty left sub tree and its right sub tree with root H.

F has neither its left sub tree nor right sub tree. Similarly, G as its root and has an empty right sub tree.

Page 19: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Strictly Binary trees

If every non-terminal node in a binary tree consists of non empty left sub tree and right sub tree(that means exactly two child), then a tree is called strictly binary tree.

Page 20: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Strictly Binary trees

Page 21: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Complete Binary Tree

A binary tree with n nodes and of depth d is a strictly binary tree all of whose terminal nodes are at level d.

In a complete binary tree, there is exactly one node at level 0, two node at level 1, and four nodes at level 2 and so on.

That means 2m nodes at each level. So, level 0 has 20 nodes that mean only one root.

Next level 1 has 21 nodes that mean two nodes; again next level 2 has 22 nodes that mean 4 nodes.

Page 22: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Complete Binary Tree

Page 23: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Full Binary Tree

A binary tree is a full binary tree if it contains the maximum possible number of nodes in all levels.

Next slide figure a full binary tree of height 2. In a full binary tree, each node has two children or no

child at all. The total number of nodes in a full binary tree of

height h is 2h+1-1 considering the root at level 0.

Page 24: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Full Binary Tree

Page 25: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Full Binary Tree

It can be calculated by adding the number of nodes of each level as in the following equation: 2h+1-1

In our example h=2, so 22+1-1=8-1=7 nodes in the full binary tree.

Page 26: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Extended Binary Trees

A binary tree T is said to be extended binary tree if each node N has either 0 or 2 children.

In such a case the nodes with 2 children are called internal nodes, and the nodes with 0 children are called external nodes.

Sometimes the nodes are distinguished in diagrams by using circles for internal nodes and squares for external nodes.

Page 27: Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical

Type of Tree :Extended Binary Trees