0% found this document useful (0 votes)
9 views14 pages

DS Mod 4 Notes

The document provides an overview of tree data structures, including basic terminology such as root, edge, parent, child, and types of binary trees like strict, complete, left skewed, and right skewed. It discusses binary tree representations through arrays and linked structures, as well as traversal methods (inorder, preorder, postorder) and specific types like expression trees and binary search trees. Additionally, it introduces the concept of threaded binary trees, explaining how null links can be replaced with pointers to optimize traversal.

Uploaded by

ravishravi153
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)
9 views14 pages

DS Mod 4 Notes

The document provides an overview of tree data structures, including basic terminology such as root, edge, parent, child, and types of binary trees like strict, complete, left skewed, and right skewed. It discusses binary tree representations through arrays and linked structures, as well as traversal methods (inorder, preorder, postorder) and specific types like expression trees and binary search trees. Additionally, it introduces the concept of threaded binary trees, explaining how null links can be replaced with pointers to optimize traversal.

Uploaded by

ravishravi153
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/ 14

Module 4

Trees
Tree is a data structure used to represent hierarchical relationship among the data
Consider the following Example:

Basic Terminology
Root
 Root is the origin of the tree data structure
 Every tree must have a root node
 There must be only one root node
Edge
 Connecting link between any two nodes
 Tree with n nodes consists of n-1 edges
Parent
 Node which is predecessor of any node
Child
 Node which is descendent of any node
Siblings
 Node which belongs to same parent
Leaf
 Node which does not have any child
Internal node
 Node which has at least one child
Degree of a node
 The total number of children of a node

Raksha Puthran, Assistant Professor, SDIT


Depth of a node
 Total number of edges from a root node to a particular node
Path
 Sequence of nodes and edges from one node to another node
Subtree
 Subtree represents the descendants of a node
Consider the following Example:

BINARY TREE
A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary
trees called the left subtree and right subtree.
Types of Binary tree
Strict binary tree
 Should have exactly 2 children or none

Raksha Puthran, Assistant Professor, SDIT


Complete binary tree
 All the nodes must have exactly two children at every level

Left skewed binary tree


 A tree with only left subtree.
 Skewed binary trees in which all the nodes are having a left child or no child at all

Right skewed binary tree


 A tree with only right subtree
 Skewed binary trees in which all the nodes are having a right child or no child at all

Binary Tree Representation


There are two ways to represent a binary tree
 Array representation
 Linked Representation
Array representation
Tree can be represented using an array called sequential representation
Following is the full binary tree of depth 4 with sequential node numbers:

Raksha Puthran, Assistant Professor, SDIT


The above numbering scheme used suggests first representation of a binary tree in memory
One dimensional array can be used to store the nodes.
Position 0 of this array is left empty. Consider the following two trees:

Following is the array representation of the above trees

Raksha Puthran, Assistant Professor, SDIT


Linked Representation
In array representation, insertion and deletion of nodes from the middle of the tree requires the
movement of many nodes.
This problem can be overcome through the use of a linked representation
Each node consists of three fields, leftChild, rightChild and data
Following is the node representation:

In C it defined as:
typedef struct node *treePointer
typedef struct
{
int data;
treePointer leftChild, rightChild;
} node;
Following is the linked representation of the above two trees (a) and (b)

Raksha Puthran, Assistant Professor, SDIT


BINARY TREE TRAVERSAL
There are three types of traversals:
INORDER (LEFT  ROOT  RIGHT)
PREORDER (ROOT  LEFT  RIGHT)
POSTORDER (LEFT  RIGHT ROOT)
Preorder traversal
void preorder(treePointer ptr)
{
if(ptr)
{
printf(“%d”, ptrdata);
preorder(ptrleftChild);
preorder(ptrrightChild);
}
}
Inorder traversal
void inorder(treePointer ptr)
{
if(ptr)
{
inorder(ptrleftChild);
printf(“%d”, ptrdata);
inorder(ptrrightChild);
}
}
Postorder traversal
void postorder(treePointer ptr)
{
if(ptr)
{
postorder(ptrleftChild);
postorder(ptrrightChild);

Raksha Puthran, Assistant Professor, SDIT


printf(“%d”, ptrdata);
}
}
Consider the following Example:

EXPRESSION TREE
Expression tree is a tree that contains an arithmetic expression with the operators and variables
Following is an example:

Raksha Puthran, Assistant Professor, SDIT


BINARY SEARCH TREE
A binary search tree is a binary tree. It may be empty. If it is not empty, then it satisfies the following
properties:
1) Each node has exactly one key and the keys in the tree are distinct
2) The key in the left subtree are smaller than the key in the root
3) The key in the right subtree are larger than the key in the root
4) The left and right subtree are also binary search tree

Raksha Puthran, Assistant Professor, SDIT


Raksha Puthran, Assistant Professor, SDIT
THREADED BINARY TREE
In the linked representation of any binary tree, there are more null links than actual pointers
One way to make use of the null links are by replacing the null links by pointers called threads
To construct a thread, we use the following rules:
If ptr leftChild is null, replace ptr leftChild with a pointer to the node that would be visited
before ptr in an inorder traversal. We replace the null link with a pointer to the inorder
predecessor of ptr.

Raksha Puthran, Assistant Professor, SDIT


If ptr rightChild is null, replace ptr rightChild with a pointer to the node that would be
visited after ptr in an inorder traversal. We replace the null link with a pointer to the inorder
successor of ptr.
Consider the following binary tree:

Following is the INORDER traversal:


HDIBEAFCG
Following is the two-way thread:

In the above diagram, two threads have been left dangling: one in the left child of H, the other in the
right child of G
In order that we leave no loose threads, we will assume a header node for all threaded binary tree
The original tree is the left subtree of the header node.
Following is an example of One-way thread:

Raksha Puthran, Assistant Professor, SDIT


Following is the complete memory representation with a header node

Raksha Puthran, Assistant Professor, SDIT


In C threaded binary tree is defined as:

Insertion of r as a right child of s in a threaded binary tree


Following are the two cases:

Raksha Puthran, Assistant Professor, SDIT


Following are the function for right insertion in a threaded binary tree and finding the INORDER
successor of a node:

}
threadPointer insucc(threadPointer tree)
{
threadPointer temp;
temp=treerightChild;
if(! treerightThread)
while(! TempleftThread)
temp=templeftChild;
return temp;
}

Raksha Puthran, Assistant Professor, SDIT

You might also like