SE250:lab-7:apra102

From Marks Wiki
Jump to navigation Jump to search

Lab7

In this lab we just need to play around how the binary search tree works. Initially its was quite confusing for me but later when i started adding elements and rotating the tree i really had fun doing that. In the beginning i used Visual studio as usually it came up with so many errors then i used cygdrive which worked and showed me this '>' which it suppose to do. Then i went through all the commands by using help command. And then i created a tree by using INSERT command. Creating is not that hard but understanding is the thing i found hard. But with help of tutor i cam to know that the way i m assuming the tree is not the correct way, it is displaying in different way, the elements up to the root are actually left and down are right. Then i played around with the other commands like BALANCE, SKEW, RIGHT, LEFT, ROTATE etc..,

Commands:
QUIT -- exit the program
HELP -- display this message

ROTATE RIGHT -- right rotate on the selected node
ROTATE LEFT  -- left rotate on the selected node
BALANCE      -- make the tree have minimal height
SKEW         -- make the tree have maximal height

LEFT   -- move to the left child of the selected node
RIGHT  -- move to the right child of the selected node
PARENT -- move to the parent of the selected node
ROOT   -- move to the root of the tree

INSERT str -- add `str' to the tree if it is not present
MULTIPLE INSERT str
           -- add `str' to the tree (even if it is already present)
DELETE     -- delete the selected node
CLEAR TREE -- remove all nodes

AUTO DISPLAY ON
AUTO DISPLAY OFF
           -- turn on/off display of the tree before the > prompt
AUTO WRITE ON [file]
AUTO WRITE OFF
           -- turn on/off writing an image file of the tree before the > prompt
PRINT      -- print the elements of the tree, as a list
DISPLAY    -- display the structure of the tree
WRITE file -- create an image file showing the structure of the tree 

Commands can be abbreviated; so "rr" is ROTATE RIGHT, etc.

Task a

In the beginning i used numbers instead of alphabets. And I understood left, right commands easily but i still dint get how to use parent command. Left command is making the present left element of root as the parent and the present root becomes right child, vise versa for the RIGHT command. my example containing numbers from 1-9 when i m inserting the elements the tree is like this:


    2
       3
  4
     5
6(*)
    7
  8
    9


When i use the LEFT command tree changed in such a way that the left element of the tree becomes the root:

   2
       3
  4(*)
     5
6
    7
  8
    9


This made me so confused. I realized using numbers much more complicated than alphabets. I was thinking how does it know which is greater alphabet and how it arranges.