Thread Representation of A Binary Tree: Inorder Listing: Head, C, B, A, E, F, D, G, Head

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Thread Representation of a Binary Tree

Inorder Listing: Head, C, B, A, E, F, D, G, Head


Searching the address of inorder successor of a node
with address X
e
P-RPTR(X)|

Function INS(X)

1. P:= RPTR(X)
If RPTR(X) < 0 then
Return(P)

2. Repeat while LPTR(P) > 0


P:= LPTR(P)

3. Return(P)
Searching the address of inorder predecessor of a node
with address X
Function INP(X)

1. P:= LPTR(X)
If LPTR(X) < 0 then
Return(P)

2. Repeat while RPTR(P) > 0


P:= RPTR(P)

3. Return(P)

Inorder Traversal of a Threaded Binary Tree

1. P:= HEAD

2. Repeat while True


P:= INS(P)
If P=HEAD then
Exit
Else Write (INFO(P))

Inorder Listing: Head, C, B, A, E, F, D, G, Head


Insertion into a Threaded Binary Tree

Case 1: LEFT(X) = Null Case 2: Insert between X and LEFT(X)


INS-LEFT(X, ITEM)
1. P:=NEW
INFO(P):=ITEM
2. LPTR(P):=LPTR(X)
LPTR(X):= P
RPTR(P):= - X
3. If LPTR(P) > 0 then
RPTR(INP(P)):= -P
Return

You might also like