We extend the concept of linked data structures to structure containing nodes with more than one self-referenced field. A binary tree is made of nodes, where each node contains a "left" reference, a "right" reference, and a data element. The topmost node in the tree is called the root. Every node (excluding root) in a tree is connected by a directed edge from exactly one other node. This node is called a parent. On the other hand, each node can be connected to arbitrary number of nodes, called children. Nodes with no children are called leaves, or external nodes. Nodes which are not leaves are called internal nodes. Nodes with the same parent are called siblings. One example of creation of binary tree is given below Fig2.
Tree terminologies
- The depth of a node is the number of edges from the root to the node.
- The height of a node is the number of edges from the node to the deepest leaf.
- The height of a tree is a height of the root.
- A leaf node which does not have any child node.
- The child nodes of a given parent node are called siblings.
- A strictly binary tree.is a binary tree in which each node has exactly zero or two children.
- A complete binary tree is a binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right.
Properties of Binary tree
- The number of external nodes is 1 more than the number of internal nodes. It is easy to see this if we start removing external nodes with their internal parent, one pair at a time. At the end of this process only the root with its two children will remain.
- The number of external nodes is at least h+1, where h is the height of the tree, and at most 2h. The later holds for a full binary tree, which is a tree where internal nodes completely fill every level.
- The number of nodes is at least h and at most 2h-1.
- A binary tree with n nodes has exactly n-1 edges.
Height of a Binary Tree
We know that binary tree with height h can have no more 2h-1 elements. Thus if n is the total number of nodes then
n <= 2h-1
n+1 <= 2h
log n+1 <= log 2h
log n+1 <= h*log 2
log
n+1 <= h*1
h >= log n+1
Representation Of a Binary Tree
Inorder Traversal | In a inorder traversal, work at a node is performed after processing it's left child and before processing it's right child. For example, inorder traversal is:D B F E A G C K J H L which is shown in Fig 1. |
---|---|
Preorder Traversal | In a preorder traversal, work at a node is performed before (pre) its children are processed. For example, preorder traversal is: A B D E F C G H J K L which is shown in Fig 1. |
Postorder Traversal | In a postorder traversal, the work at a node is performed after (post) its children are evaluated. For example, postorder traversal is: D F E B G K J L H C which is shown in Fig 1. |
Now as we have familiar with traversal of binary tree we will construct the tree from different traversals. Here we will consider two cases.
Case 1:
In order: E A K C F H D B G
Pre order: F A E C K D H G B
How to construct tree from these two traversal is given below in following Fig 2.
Fig. construct tree form inorder-preorder traversal
Post order: H D I E B J F K L G C A
In order: H D B I E A F J C K G L
How to construct tree from these two traversal is given below in following Fig 3.
Source code is given below to construct tree from different traversal.
Creating Tree From Different Traversal
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.