DS Mod 4 Notes
DS Mod 4 Notes
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
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
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)
EXPRESSION TREE
Expression tree is a tree that contains an arithmetic expression with the operators and variables
Following is an example:
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:
}
threadPointer insucc(threadPointer tree)
{
threadPointer temp;
temp=treerightChild;
if(! treerightThread)
while(! TempleftThread)
temp=templeftChild;
return temp;
}