Module 2
Trees
Add Child


CC315

MyTree parent children item
a none {b,c} A
b a {d} B
c a {} C
d b {} D
x none {y,z} X
y x {} Y
z x {} Z
MyTree parent children item
a none {b,c} A
b a {d} B
c a {x} C
d b {} D
x c {y,z} X
y x {} Y
z x {} Z

Special Considerations

  • Child can't already have parent
  • Child can't be a child of attempted parent

Pseudocode


function ADDCHILD(CHILD)
    IF CHILD has PARENT
        throw exception
    IF CHILD is CHILD of PARENT
        return FALSE
    ELSE
        append CHILD to PARENT`s children
        set CHILD`s parent to PARENT
        return TRUE
end function