Code: Select all
type Node
value as integer
left as Node ptr
right as Node ptr
end type
function createNode( byval v as integer, byval l as Node ptr, byval r as Node ptr ) as Node ptr
dim new as Node ptr = 0
new = callocate( len(Node) )
new->value = v
new->left = l
new->right = r
return new
end function
function insert( byval v as integer, n as Node ptr ) as Node ptr
if (n = 0) then
n = createNode( v, 0, 0 )
elseif (v < n->value) then
n->left = insert( v, n->left )
elseif (v > n->value) then
n->right = insert( v, n->right )
end if
return n
end function
function findMin( byval n as Node ptr ) as Node ptr
if (n = 0) then return 0
while (n->left <> 0)
n = n->left
wend
return n
end function
function findMax( byval n as Node ptr ) as Node ptr
if (n = 0) then return 0
while (n->right <> 0)
n = n->right
wend
return n
end function
function preOrder( byval n as Node ptr )
if (n = 0) then return
print (n->value);
preOrder(n->left)
preOrder(n->right)
return
end function
function postOrder( byval n as Node ptr )
if (n = 0) then return
postOrder(n->left)
postOrder(n->right)
print (n->value);
return
end function
function inOrder( byval n as Node ptr )
if (n = 0) then return
inOrder(n->left)
print (n->value);
inOrder(n->right)
return
end function
function removeMin( n as Node ptr ) as Node ptr
if n = 0 then return
dim old as Node ptr = 0
if (n->left <> 0) then
n->left = removeMin( n->left )
else
old = n
n = n->right
deallocate n
end if
return n
end function
function removeMax( n as Node ptr ) as Node ptr
if n = 0 then return
dim old as Node ptr = 0
if (n->right <> 0) then
n->right = removeMax( n->right )
else
old = n
n = n->left
deallocate n
end if
return n
end function
function remove( byval v as integer, n as Node ptr ) as Node ptr
if n = 0 then return
dim old as Node ptr = 0
if v < n->value then
n->left = remove( v, n->left )
elseif v > n->value then
n->right = remove( v, n->right )
elseif (n->left <> 0) and (n->right <> 0) then
n->value = findMax( n->left )->value
removeMax( n->left )
else
old = n
n = iif(n->left = 0, n->right, n->left )
deallocate old
end if
return n
end function
function main()
dim root as Node ptr = 0
insert( 10, root )
insert( 11, root )
insert( 5 , root )
insert( 8 , root )
insert( 2 , root )
insert( 7 , root )
print "preOrder : ";
preOrder( root ) : print
print "postOrder : ";
postOrder( root ): print
print "inOrder : ";
inOrder( root ) : print
print
print "removing 5 from tree"
print
remove( 5, root )
print "preOrder : ";
preOrder( root ) : print
print "postOrder : ";
postOrder( root ): print
print "inOrder : ";
inOrder( root ) : print
sleep
end function
end( main() )
Code: Select all
type Node
value as integer
left as Node ptr
right as Node ptr
end type
function singleRightRotation( n as Node ptr ) as Node ptr
dim temp as Node ptr = 0
temp = n->left
n->left = temp->right
temp->right = n
return temp
end function
function singleLeftRotation( n as Node ptr ) as Node ptr
dim temp as Node ptr = 0
temp = n->right
n->right = temp->left
temp->left = n
return temp
end function
function doubleRightRotation( n as Node ptr ) as Node ptr
dim t1 as Node ptr = 0
dim t2 as Node ptr = 0
t1 = n->left
t2 = t1->right
n->left = t2->right
t1->right = t2->left
t2->right = n
t2->left = t1
return t2
end function
function doubleLeftRotation( n as Node ptr ) as Node ptr
dim t1 as Node ptr = 0
dim t2 as Node ptr = 0
t1 = n->right
t2 = t1->left
n->right = t2->left
t1->left = t2->right
t2->left = n
t2->right = t1
return t2
end function
function max( v1 as integer, v2 as integer ) as integer
return iif( v1 < v2, v2, v1 )
end function
function height( n as Node ptr ) as integer
if n = 0 then return 0
return 1 + max( height( n->left ), height( n->right ) )
end function
function ballanceFactor( n as Node ptr ) as integer
return height( n->right ) - height( n->left )
end function
function reballanceNode( n as Node ptr ) as Node ptr
if n = 0 then return
if ballanceFactor( n ) = -2 then
if ballanceFactor( n->left ) = -1 or ballanceFactor( n->left ) = 0 then
'
' single right rotation
'
n = singleRightRotation( n )
elseif ballanceFactor( n->left ) = 1 then
'
' double right rotation
'
n = doubleRightRotation( n )
end if
elseif ballanceFactor( n ) = 2 then
if ballanceFactor( n->right ) = -1 then
'
' double left rotation
'
n = doubleLeftRotation( n )
elseif ballanceFactor( n->right ) = 1 or ballanceFactor( n ) = 0 then
'
' single left rotation
'
n = singleLeftRotation( n )
end if
end if
return n
end function
function createNode( byval v as integer, byval l as Node ptr, byval r as Node ptr ) as Node ptr
dim new as Node ptr = 0
new = callocate( len(Node) )
new->value = v
new->left = l
new->right = r
return new
end function
function insert( byval v as integer, n as Node ptr ) as Node ptr
if (n = 0) then
n = createNode( v, 0, 0 )
elseif (v < n->value) then
n->left = insert( v, n->left )
elseif (v > n->value) then
n->right = insert( v, n->right )
end if
n = reballanceNode( n )
return n
end function
function findMin( byval n as Node ptr ) as Node ptr
if (n = 0) then return 0
while (n->left <> 0)
n = n->left
wend
return n
end function
function findMax( byval n as Node ptr ) as Node ptr
if (n = 0) then return 0
while (n->right <> 0)
n = n->right
wend
return n
end function
function preOrder( byval n as Node ptr )
if (n = 0) then return 0
print (n->value);
preOrder(n->left)
preOrder(n->right)
return
end function
function postOrder( byval n as Node ptr )
if (n = 0) then return 0
postOrder(n->left)
postOrder(n->right)
print (n->value);
return
end function
function inOrder( byval n as Node ptr )
if (n = 0) then return 0
inOrder(n->left)
print (n->value);
inOrder(n->right)
return
end function
function removeMin( n as Node ptr ) as Node ptr
if n = 0 then return
dim old as Node ptr = 0
if (n->left <> 0) then
n->left = removeMin( n->left )
else
old = n
n = n->right
deallocate old
end if
reballanceNode( n )
return n
end function
function removeMax( n as Node ptr ) as Node ptr
if n = 0 then return
dim old as Node ptr = 0
if (n->right <> 0) then
n->right = removeMax( n->right )
else
old = n
n = n->left
deallocate old
end if
reballanceNode( n )
return n
end function
function remove( byval v as integer, n as Node ptr ) as Node ptr
if n = 0 then return
dim old as Node ptr = 0
if v < n->value then
n->left = remove( v, n->left )
elseif v > n->value then
n->right = remove( v, n->right )
elseif (n->left <> 0) and (n->right <> 0) then
n->value = findMax( n->left )->value
removeMax( n->left )
else
old = n
n = iif(n->left = 0, n->right, n->left )
deallocate old
end if
reballanceNode( n )
return n
end function
function main()
dim root as Node ptr = 0
insert( 10, root )
insert( 85, root )
insert( 15, root )
insert( 70, root )
insert( 20, root )
insert( 60, root )
insert( 30, root )
insert( 50, root )
insert( 65, root )
insert( 80, root )
insert( 90, root )
insert( 40, root )
insert( 5, root )
insert( 55, root )
print "preOrder : "; : preOrder( root ) : print
print "delete 60" : remove( 60, root )
print "preOrder : "; : preOrder( root ) : print
print "delete 55" : remove( 55, root )
print "preOrder : "; : preOrder( root ) : print
print "delete 50" : remove( 50, root )
print "preOrder : "; : preOrder( root ) : print
print "delete 40" : remove( 40, root )
print "preOrder : "; : preOrder( root ) : print
sleep
end function
end( main() )