0% found this document useful (0 votes)
0 views

BinaryTree (CompletePsuedoCode)

The document outlines the structure and operations of a binary tree, including creating nodes, adding and deleting nodes, searching for values, and performing in-order traversal. It defines a tree node type and provides procedures for adding to the tree and finding values. Additionally, it includes a sample implementation of these operations using a specified data structure.

Uploaded by

Mazen Zaheer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

BinaryTree (CompletePsuedoCode)

The document outlines the structure and operations of a binary tree, including creating nodes, adding and deleting nodes, searching for values, and performing in-order traversal. It defines a tree node type and provides procedures for adding to the tree and finding values. Additionally, it includes a sample implementation of these operations using a specified data structure.

Uploaded by

Mazen Zaheer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

BINARY TREE

- Creating a New List


- Adding A Node to List
- Deleting from List
- Searching Value
- Output All Data from linked list

TNode Data,Left,Right
Tree
Free
Root
AddToTree(NewData) Curr,New,Prev,TurnedLeft

Task Related to Binary Tree:-

- Create A tree Node Type


- Create a Binary Tree
- Connect Free Binary Tree
- Add TO Tree
- Find a value
- In Order Tree Traversal

TYPE TNode
DECLARE Data : STRING
DECLARE Left : INTEGER
DECLARE Right : INTEGER
END TYPE

DECLARE Tree [0:6] OF TNode


DECLARE Free : INTEGER
DECLARE Root : INTEGER
CONSTANT Null = -1

Free ß 0
Root ß Null

FOR x ß 0 TO 5
Tree[x].Leftßx+1
NEXT
END FOR
Tree[6].Left ß Null
ADDING TO A TREE

Procedure AddToTree(NewData : STRING)


DECLARE Prev,Curr,New : INTEGER
DECLARE TurnedLeft : BOOLEAN
IF Free<>Null
NewßFree
Free ß Tree[Free].Left
Tree[New].DataßNewData
Tree[New].LeftßNull
Tree[New].Right ßNull
IF Root=Null
RootßNew
ELSE
CurrßRoot
WHILE Curr<>Null
PrevßCurr
IF Tree[Curr].Data > NewData
TurnedLeftßTRUE
CurrßTree[Curr].Left
ELSE
TurnedLeftßFALSE
CurrßTree[Curr].Right
END IF
END WHILE
IF TurnedLeft=TRUE
Tree[Prev].LeftßNew
ELSE
Tree[Prev].RightßNew
END IF
END IF
ELSE
OUTPUT “Tree is full”
END IF
END PROCEDURE

FINDING A Value FROM TREE

PROCEDURE FindValue(ValueToFind:STRING)
DECLARE Curr : INTEGER
IF Root<>Null
Curr ß Root
WHILE Curr<>Null AND Tree[Curr].Data <> ValueToFind
IF Tree[Curr].Data>ValueToFind
CurrßTree[Curr].Left
ELSE
CurrßTree[Curr].Right
END IF
END WHILE

IF Curr=Null
OUTPUT “Not found”
ELSE
OUTPUT “found at”,Curr
END IF
ELSE
OUTPUT “Tree empty”
END IF
END PROCEDURE

IN ORDER TRAVERSAL

PROCEDURE InOrder(Root : INTEGER)


IF Root<>Null
CALL InOrder(Tree[Root].Left)
OUTPUT Tree[Root].Data
CALL InOrder(Tree[Root].Right)
END IF
END PROCEDURE

You might also like