Procedure | Push (S,a) |
begin | |
S[I] := a; | |
I := I+1; | |
end |
function | Pop(S) |
begin | |
I := I-1; | |
return(S[I]); | |
end |
function | Top(S) |
begin | |
return(S[I-1]); | |
end |
Procedure | enqueue(Q,a) |
begin | |
Q[T] := a; | |
T = T - 1 modulo N; | |
end |
begin | |
H := H-1 modulo N; | |
return (Q[H+1 modulo N]); | |
end |
record | name |
begin | |
field declarations; | |
field declarations; | |
... | |
field declarations; | |
end |
record | employee |
begin | |
char name[1:30] | |
integer SSN; | |
char address[1:100] | |
real salaray; | |
end |
record | company |
begin | |
char name[1:50] | |
char address[1:100] | |
employee Z[1:1000] | |
end |
Examples: | X.SSN := 123456789; |
X.salary := X.salary + 1000; |
Examples: | charpointer p; | intpointer q[1:n]; | employeepointer r; |
charptr p; | intptr q[1:n]; | employeeptr r; |
record | employee |
begin | |
char name[1:30] | |
integer SSN; | |
employeeptr next; | |
end |
In the three algorithms below, T is a pointer to the tree root, and a a is a key.
function | search(T,a) | |||
begin | ||||
nodeptr p; | ||||
p=T; | ||||
while (p != nil | and | p --> key != a) | do | |
if | a < p --> key | then | ||
p := p --> left; | ||||
else | ||||
p := p --> right; | ||||
endif | ||||
endwhile | ||||
return (p); | ||||
end search |
procedure | insert(T,a) |
procedure delete(T,a) begin nodeptr p,q,r,s; integer direction; p = T; while (p != nil and p-->key != a) do if a < p-->key then q := p; p := p-->left; direction := 0; else q := p; p := p-->right; direction := 1; endif endwhile if p == nil then return; elseif p-->left == nil and p-->right == nil then /* p has no children */ /* delete node pointed to by p and exit */ if direction == 0 q-->left = nil; else q-->right = nil endif free (p); elseif p-->left == nil then /* p has only one child, the right one */ if direction == 0 then q-->left := p-->right; /* shortcut from parent to grandchild */ else q-->right := p-->right; endif elseif p-->right == nil then /* p has only one child, the left one */ if direction == 0 then q-->left := p-->left; else q-->right := p-->left; endif else /* p has two children*/ /* find the maximum node in the left subtree*/ /* subtree of p */ s := p-->left; q := p; /* now q will be the parent of s , and direction*/ /* will indicate the type of child s is to q*/ direction = 0; while s-->right != nil do q := s; s := s-->right; direction := 1; endwhile /*Now s points to the maximum node in the */ /*left subtree of p*/ p-->key := s-->key; /* now node s must be deleted. But since s has */ /* no right child, the deletion is done by */ /* deletion or shortcutting */ if s-->left == nil then /* s is a leaf*/ if direction == 0 then q-->left := nil; else q-->right := nil; endif free (s); return; else /* s has a left child */ if direction == 0 then q-->left := s-->left; else q-->right := s-->left; endif free(s) return; endif endif end delete
x= root of H; | |
a=key of x; /* to be returned at the end*/ | |
remove a from node x; | |
take the last node (node n), remove its key (call it b), and store b in the root; | |
remove node n; | |
/* now we have to restor the heap*/ |
while | (x has a key bigger than one of its children) do | |
swap x with the smaller child; | ||
make x point to that child; | ||
endwhile |
/* note the while loop will stop when x becomes smaller */ |
/* than both its children, or when x becomes a leaf*/ |
end delete-min |
create a node of label n+1, and insert a into that node; | |
let x point to that node; | |
while (x is smaller than its parent) do |
swap the key of x with key of the parent of x; | |
let x point to its parent; |
endwhile |
/* this will loop until either x becomes larger than its parent, or it becomes the root*/ |
PARENT[i]=PARENTiI]+PARENT[j]; /* this computes the total number of nodes in the new tree */ | |
PARENT[j]=i; | |
end |
integer | r=x; | |
while | (PARENT[r] > 0) do | |
r = PARENT[r]; | ||
endwhile |
/*now r is a root*/ | |
return(r); |
if |PARENT[i]| >= |PARENT[j]| then |
PARENT[i]=PARENTiI]+PARENT[j]; | |
PARENT[j]=i; |
else |
PARENT[j]=PARENTiI]+PARENT[j]; | |
PARENT[i]=j; |
endif |
integer r,s,t; | |
r=x; | |
while PARENT[r] > 0 |
r = PARENT[r]; |
endwhile | |
/*now r is a root*/ | |
s=x; | |
while s != r do |
t := s; | |
s := PARENT[s]; | |
PARENT[t] := r; |
endwhile | |
return (r); |