Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Tree.

Similar presentations


Presentation on theme: "Binary Tree."— Presentation transcript:

1 Binary Tree

2 Introduction 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.

3 Introduction The major operations required to be performed on a Binary tree are: Creation: Creation an empty binary tree to which the ‘root ‘ points Traversal: Visiting all the nodes in a binary tree Deletion: Deleting a node from a non-empty binary tree Insertion: Inserting a node into an existing (may be empty) binary tree Merge: Merging two binary tree

4 Introduction Copy: Copying a binary tree
Compare: Comparing two binary tree Finding a replica or mirror of a binary tree

5 Binary Tree Representation
It is often required to maintain binary tree in the computer memory. There are two traditional popular technique that are used to maintain binary tree in the memory: sequential representation/Array implementation of Binary tree linked list representation/Linked list implementation of Binary tree

6 Array implementation of Binary tree

7 Array implementation of Binary tree
The root node is number as 0 then its left child as 1 & right child as 2. Now node 1 is parent and its left child is number 3 and right child number 4. Now so on in each level.

8 Array implementation of Binary tree

9 Array implementation of Binary tree
Reverse process to create tree from table: Here, problem is to find the left child and right child of current node parent. For that to remember the formula for children: 2*root index +1--- for left child 2*root index  for right child

10 Array implementation of Binary tree
Advantages of representing binary tree using array: Any node can be accessed from any other node by calculating the index. Here, the data is stored without any pointers to its successor or predecessor. In the programming languages, where dynamic memory allocation is not possible (such as BASIC, FORTRAN), array representation is the only means to stored a tree.

11 Array implementation of Binary tree
Disadvantages of representing binary tree using array: Other than full binary trees, majority of the array entries may be empty. It allows only static representation. The array size cannot be changed during the exception. Inserting a new node to it or deleting a node from it’s inefficient with this representation. Because it requires considerable data movement up and down the array which demand excessive amount of processing time.

12 Linked list implementation of Binary tree
The problem associated with the sequential representation of binary tree can be overcome through the use of the linked list representation. In this representation each node required three fields, one for the link of left child, second field for representing the information associated with the node and the third field is used to represent the link of the right child.

13 Linked list implementation of Binary tree

14 Linked list implementation of Binary tree
a binary tree contains one root node and some non-terminal and terminal nodes. It is clear from the observation of a binary tree that the non-terminal node has their left child and right child nodes. But the terminal nodes have no left child and right child nodes. Their Lchild and Rchild pointer are set to NULL. Here, non-terminal nodes are called internal nodes and terminal nodes are called external nodes.

15 Linked list implementation of Binary tree

16 Linked list implementation of Binary tree
Advantages of representing binary tree using Linked list: The drawback of the sequential representation is overcome in this representation. We may or may not know the tree depth in advance. In additional, for unbalanced trees, the memory is not wasted. Insertion and deletion operations are more efficient in this representation. It is useful for dynamic data.

17 Linked list implementation of Binary tree
Disadvantages of representing binary tree using Linked list: In this representation, there is no direct access to any node. It has to be traversed from the root to reach to a particular node. As compared to sequential representation, the memory needed per node is more. This is due to two link fields (left child and right child for binary trees) in the node. The programming languages not supporting dynamic memory management would not be useful for this representation.

18 Traversal of a Binary Tree
Tree traversal is one of the most common operations performed on tree data structures. It is a way in which each node in the tree is visited exactly once in a systematic manner. The binary tree traversal would produce a linear order for the nodes in a binary tree; there are three popular ways of binary tree traversal.

19 Traversal of a Binary Tree
There are: P/D=process node, L=left child, R=right child Preorder traversal (PLR/DLR) Inorder traversal(LPR/LDR) Postorder traversal(LRP/DPD)

20 Traversal of a Binary Tree

21 Traversal of a Binary Tree
Pre-order Traversal perform the following three operation: visit the root Traverse the left sub-tree in pre-order Traverse the right sub-tree in pre-order Pre-order=>1,2,4,7,5,8,3,6,9

22 Traversal of a Binary Tree
In-order Traversal perform the following three operation: Traverse the left sub-tree in In-order visit the root Traverse the right sub-tree in In-order In-order=>4,7,2,8,5,1,6,9,3

23 Traversal of a Binary Tree
Post-order Traversal perform the following three operation: Traverse the left sub-tree in Post-order Traverse the right sub-tree in Post-order visit the root Post-order=>7,4,8,5,2,9,6,3,1


Download ppt "Binary Tree."

Similar presentations


Ads by Google