0% found this document useful (0 votes)
6 views93 pages

Trees

Uploaded by

John
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)
6 views93 pages

Trees

Uploaded by

John
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/ 93

UNIT –III

Non-linear Data structures – Tree data structures: Introduction: Tree definition,


Terminology, Binary Trees: Definition, Types, Properties, Representation of
Binary Tree: Array representation, Linked representation, Binary Tree traversals-
Preorder, Inorder and postorder. Threaded binary Trees: Definition, types, Data
structure and memory representation of threaded tree, Binary Search Tree:
Definition, Construction- Searching, Insertion operations, deletion process,
Traversal examples
Expression Tree: Constructing expression tree for a given expression,
traversals, Evaluation of expression, programming examples

Nonlinear Data structures – Graphs


Representation of graphs: Definition, types and terminology, Matrix
representation, Adjacency list chain and sequential representation.
Hashing: Hash Table organizations, Hashing Functions, Over flow handling.
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
Properties of Binary trees

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


b) The maximum number of nodes in a binary tree of depth k is 2^k-1
Complete Binary tree
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