Sunday 6 February 2011

data structure - tree

1. Algorithm For Create of Binary Tree:-

Function CREATE(T,VAL)
[ROOT=Dummy header for the root of the binary tree initialized by NULL,
NEWNODE=Variable points to the new element,
T=Temporary variables for traverse the tree,
INFO=Information part of node.]

Step 1: [Allocate the memory for new node .]
NEWNODE<=NODE

Step 2: [Read the element.]
Read(VAL)

Step 3: [Store element into new node.]
INFO(NEWNODE )VAL

Step 4: [Temporary store right and left tree is NULL.]
LEFT(NEWNODE)RIGHT(NEWNODE)NULL

Step 5: [Check if the root is NULL.]
If ROOT=NULL
Then
ROOTNEWNODE
Return

Step 6: [Search the place to insert the element.]
While T!=NULL
(Check for element is less than information of T)
if VAL < INFO (T)
then
PARENTT
TLEFT(T)
Else
PARENTT
TRIGHT(T)

Step 7: [Check element is less than information of parent.]
If VAL < INFO(PARENT)
Then
LEFT(PARENT)NEWNODE
Else
RIGHT(PARENT)NEWNODE


2. Algorithm For Inorder Traversal of Binary Tree:-

Function INORDER(T)
[T = Temporary pointer variable initialized with root,




INFO=Information part of node,
LEFT=Pointer to left most node,
RIGHT=Pointer to right most node,]

Step 1: [Repeat step 2,3,4 and check that T is not equal to NULL ]
If T!=NULL
Then

Step 2: [Call function itself as a left most node.]
INORDER(LEFT(T))

Step 3: [Print information part of node.]
Write INFO(T)

Step 4: [Call function itself as a right most node.]
INORDER(RIGHT(T))


3. Algorithm For Preorder Traversal of Binary Tree:-

Function PREORDER(T)
[T = Temporary pointer variable initialized with root,
INFO=Information part of node,
LEFT=Pointer to left most node,
RIGHT=Pointer to right most node,]

Step 1: [Repeat step 2,3,4 and check that T is not equal to NULL ]
If T!=NULL
Then

Step 2:[Print information part of node.]
Write INFO(T)

Step 3: [Call function itself as a left most node.]
PREORDER(LEFT(T))

Step 4: [Call function itself as a right most node.]
PREORDER(RIGHT(T))



4. Algorithm For Postorder Traversal of Binary Tree:-

Function POSTORDER(T)
[T = Temporary pointer variable initialized with root,
INFO=Information part of node,
LEFT=Pointer to left most node,
RIGHT=Pointer to right most node,]

Step 1: [Repeat step 2,3,4 and check that T is not equal to NULL ]
If T!=NULL
Then

Step 2: [Call function itself as a left most node.]
POSTORDER(LEFT(T))

Step 3: [Call function itself as a right most node.]
POSTORDER(RIGHT(T))

Step 4: [Print information part of node.]
Write INFO(T)


5. Algorithm For Depth of Binary Tree:-

Function DEPTH(T, LEVEL)
[T = Temporary pointer variable initialized with root,
INFO=Information part of node,
LEFT=Pointer to left most node,
RIGHT=Pointer to right most node.]

Step 1: [Repeat step 2,3,4 and check that T is not equal to NULL ]
If T!=NULL
Then

Step 2: [Check D is less than LEVEL.]
If D Then
(Store LEVEL into D)
DLEVEL

Step 3: [Call function itself as a left most node.]
DEPTH(LEFT(T),LEVEL+1)

Step 4: [Call function itself as a right most node.]
DEPTH(RIGHT(T),LEVEL+1)

Step 5: [Print the LEVEL.]
Write ‘level’,D


6. Algorithm For Search of Binary Tree:-

Function SEARCH(T,KEY)
[T = Temporary pointer variable initialized with root,
INFO=Information part of node,
LEFT=Pointer to left most node,
RIGHT=Pointer to right most node.]

Step 1:[Read the KEY]
Read(KEY)
Step 2: [Repeat step 2,3,4 and check that T is equal to NULL ]
If T = NULL
Then
(Prompt the message key not found)
write ‘Key not found’
return

step 3: [Check the that information of T is equal to key.]
If INFO(T)=KEY
Then
(Prompt the message key found)
write ‘Key found’
return

step 4: [Check that key is less than information of T.]
If KEY Then
(Call function itself as a left most node.)
SEARCH(LEFT(T),KEY)
Else
(Call function itself as a right most node.)
SEARCH(RIGHT(T),KEY)


7. Algorithm For Modify of Binary Tree:-

Function MODIFY (T,KEY,VAL)
[T = Temporary pointer variable initialized with root,
INFO=Information part of node,
LEFT=Pointer to left most node,
RIGHT=Pointer to right most node.
VAL=New element.]

Step 1: [Read the KEY]
Read (KEY)

Step 2: [Read the value.]
Read(VAL)

0Step 3: [Repeat step 4,5,6 and check that T is equal to NULL ]
If T = NULL
Then
(Prompt the message key not found)
write ‘Key not found’
return

step 4: [Check the that information of T is equal to key.]
If INFO(T)=KEY
Then
(To store value in information of T)
INFO(T)VAL
return

step 5: [Check that key is less than information of T.]
If KEY Then
(Call function itself as a left most node.)
MODIFY(LEFT(T),KEY,VAL)
Else
(Call function itself as a right most node.)
MODIFY(RIGHT(T),KEY,VAL)

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms