0% found this document useful (0 votes)
31 views

Nonlinear Data Structures - Tree Data Structures 1

1. The preorder traversal is 11, 6, 4, 5, 8, 10, 19, 17, 31, 43, 49 2. The inorder traversal is 4, 5, 6, 8, 10, 11, 17, 19, 31, 43, 49 3. The postorder traversal is 5, 4, 10, 8, 6, 17, 49, 43, 31, 19, 11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Nonlinear Data Structures - Tree Data Structures 1

1. The preorder traversal is 11, 6, 4, 5, 8, 10, 19, 17, 31, 43, 49 2. The inorder traversal is 4, 5, 6, 8, 10, 11, 17, 19, 31, 43, 49 3. The postorder traversal is 5, 4, 10, 8, 6, 17, 49, 43, 31, 19, 11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

NONLINEAR DATA

STRUCTURES - TREE DATA


STRUCTURES 1
What is aTree?

A tree is a recursively defined as follows

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:

A first node written at the top is root node.

Root node does not have parent.


Child:

The node obtained from the parent node is called child node.

A parent node can have zero or more child nodes.

Siblings:

2 or more nodes having the same parent are called siblings.


Ancestors:

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:

The number of subtrees of a node is called its degree.

Leaf:

A node in a tree that has a degree of zero is called leaf node

Internal nodes:

The nodes except leaf nodes in a tree are called internal nodes
External Nodes:

The NULL link of any node in a tree is an external node.

Level:

The distance of a node from the root is called level of the node

Height:

It is the maximum level of any leaf in the tree


Representation of Trees

● List representation
● Left child - right sibling representation
● Binary tree representation
List representation

● The root node comes first


● Its immediately followed by a list of subtrees of that node.
● It is recursively repeated for each subtree
Example
List representation
Left child right sibling representation

What is left child right sibling 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

To obtain degree-two tree representation of a tree, rotate the right-


sibling pointers in the left child-right sibling tree clockwise by 45
degrees.

In a degree-two representation, the two children of anode are referred


as left and right children.
What is a Binary Tree?

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.

In a binary tree, every node can have either 0 children or 1 child or 2


children but not more than 2 children.
Properties of Binary trees

The maximum number of nodes on leaf i of a binary 2^i for i>=0


Types of binary trees

1. strictly binary tree (Full binary tree)

2. complete binary tree

3. skewed tree

4. Expression tree

5. Binary search tree


Strictly binary 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 binary tree in which every internal node has exactly two


children and all leaf nodes are at same level is called Complete
Binary Tree.
Example
Skewed tree

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

1. The nodes are numbered sequentially from 0


2. The node with position 0 is the root node
3. Given the position of ant node i, 2i+1 gives the position of the left child and
2i+2 gives the position of right child
4. If i is position of left child, i+1 give the position of right child
5. If i is position of right child, i-1 give the position of left child
6. Given the position of ant node i, the parent position is given by (i-1)/2. If i is
odd it points to left child otherwise it points to right child.
Array representation using 2 different
methods

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

Note : 0 Indicates NULL link


Construction of Binary Tree

15, 7, 25, 35, -1, 78, 5

Note : -1 Indicates NULL link


Deletion in Binary Tree
Given a binary tree, delete a node from it by making sure that tree shrinks from the
bottom (i.e. the deleted node is replaced by bottom most and rightmost node).

This different from BST deletion.

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

Level order traversal after Deletion of node: 0 1 2 3 9 5 6 7 8


Binary Tree Traversals

Visiting each node of a tree exactly once in a systematic order is


called traversing.

In - Order Traversal

Pre - Order Traversal

Post - Order Traversal


Preorder Traversal

In this traversal, the root node is visited first, then its left child and later
its right child.

1. Process the root node


2. Traverse the left subtree
3. Traverse the right subtree
We start from A, and following pre-order traversal, we first visit A itself and
then move to its left subtree B. B is also traversed pre-order. The process
goes on until all the nodes are visited. The output of pre-order traversal of
this tree will be −
A→B→D→E→C→F→G
Function to traverse the tree in preorder

Void preorder(NODE root){

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 a binary tree is traversed in-order,


the output will produce sorted key values
in an ascending order.
We start from A, and following in-order traversal, we move to its left
subtree B. B is also traversed in-order. The process goes on until all the
nodes are visited. The output of inorder traversal of this tree will be −
D→B→E→A→F→C→G
void Inorder(NODE root){

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

Given a sequence of numbers:

11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31

Draw a binary search tree by inserting the above numbers


Insertion
Insert 25 and 200
Deletion

1) Node to be deleted is leaf: Simply remove from the tree


Deletion

2) Node to be deleted has only one child: Copy the child to the node and
delete the child
Deletion

3) Node to be deleted has two children:

● Find inorder successor of the node.


● Copy contents of the inorder successor to the node that you want to delete.

Note that inorder predecessor can also be used.


Inorder successor

● Find the inorder traversal of the tree


● Suppose that you want to find the inorder successor of x
● Now the node immediately following the node x is the inorder successor

The Inorder traversal is 10,20,30,100,500

Inorder sucessor of node 100 is 500


Inorder predecessor

● Find the inorder traversal of the tree


● Suppose that you want to find the inorder successor of x
● Now the node previous to the node x is the inorder predecessor

Inorder traversal 10,20,30,100,500

Inorder Predecessor of 100 is 30


Example for deletion

Delete node 50

Find the inorder traversal is 40,50,60,70,80

Inorder successor of 50 is 60

So Replace 50 with 60
Traversal

Inorder

Preorder

postorder
Traverse the given BST

You might also like