Nonlinear Data Structures - Tree Data Structures 1
Nonlinear Data Structures - Tree Data Structures 1
A tree is a set of finite set of one or more nodes that shows parent-child
relation such that:
● Root node
● subtrees
Example
Basic Terminologies
Root Node:
The node obtained from the parent node is called child node.
Siblings:
The nodes obtained in the path from the specified node x while moving upward
towards the root are called ancestors.
Descendants:
The nodes in the path below the parent are called descendants.
● Left descendants
● Right descendants
Degree:
Leaf:
Internal nodes:
The nodes except leaf nodes in a tree are called internal nodes
External Nodes:
Level:
The distance of a node from the root is called level of the node
Height:
● List representation
● Left child - right sibling representation
● Binary tree representation
List representation
● The left pointer of a node in the tree will be the left child in this
representation.
● The remaining children of a node in the tree are inserted
horizontally to the left child in the representation
Binary Tree (Degree two trees)
representation
Binary tree is a special type of tree data structure in which every node can
have a maximum of 2 children. One is known as left child and the other is
known as right child.
A tree in which every node can have a maximum of two children is called as
Binary Tree.
3. skewed tree
4. Expression tree
In strictly binary tree, every node should have exactly two children or
none. That means every internal node must have exactly two children.
A binary tree in which every node has either two or zero number of
children is called Strictly Binary Tree.
Strictly binary tree is also called as Full Binary Tree Proper Binary Tree
or 2-Tree
Example
Complete Binary Tree
In complete binary tree every level except possibly the last level
is completely filled.
A skewed tree is a tree consisting of only left subtree or only right subtree.
Binary tree representation
Array representation
Linked representation
Observe the following points
Method 1:
Flag namely used can be used just to indicate whether a memory location is
used or not.
If flag filed used is 0, memory location is not used and indicates absence of
node at that position
Method 2:
One can initialize each location to some value indicating the absence of node
Construction of Binary Tree
14, 2, 8, 0 , 7, 10, 15
Here we do not have any order among elements, so we replace with last element.
Print level order traversal, the last node will be the deepest node
Level order traversal before Deletion of node: 0 1 2 3 4 5 6 7 8 9
In - Order Traversal
In this traversal, the root node is visited first, then its left child and later
its right child.
if(root==NULL) return;
printf(“%d”,root->info);
preorder(root->llink)
preorder(root->rlink);
}
PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3
In order Traversal
In this traversal method, the left subtree is visited first, then the root and
later the right sub-tree.
if(root==NULL) return;
Inorder(root->llink);
printf(“%d”,root->info);
Inorder(root->rlink);
}
InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
Post order Traversal
In this traversal method, the root node is visited last, hence the name. First we
traverse the left subtree, then the right subtree and finally the root node.
We start from A, and following Post-order traversal, we first visit the left subtree B.
B is also traversed post-order. The process goes on until all the nodes are visited.
The output of post-order traversal of this tree will be −
D→E→B→F→G→C→A
void Postorder(NODE root){
if(root==NULL) return;
Postorder(root->llink);
Postorder(root->rlink);
printf(“%d”,root->info);
}
PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
Binary Search Trees
A node's left child must have value less than its parent's value and
node's right child must have value greater than it's parent value.
Examples of BST
Construction of BST
2) Node to be deleted has only one child: Copy the child to the node and
delete the child
Deletion
Delete node 50
Inorder successor of 50 is 60
So Replace 50 with 60
Traversal
Inorder
Preorder
postorder
Traverse the given BST