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

InsertionSort and BinarySearch

The document outlines the structure and operations of a binary tree, including creating a tree node, adding items, and finding items within the tree. It defines a tree node type and provides procedures for adding data and searching for values. Key procedures include 'AddToTree' for inserting new data and 'Find' for locating items in the tree.

Uploaded by

Mazen Zaheer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

InsertionSort and BinarySearch

The document outlines the structure and operations of a binary tree, including creating a tree node, adding items, and finding items within the tree. It defines a tree node type and provides procedures for adding data and searching for values. Key procedures include 'AddToTree' for inserting new data and 'Find' for locating items in the tree.

Uploaded by

Mazen Zaheer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Binary Tree

- Create a Tree Node


- Create a New Tree
- Add Items to A Tree
- Find Items in A Tree
- In Order Traversal with Recursion

CREATING A NEW TREE

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

DECLARE Tree : ARRAY[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 Curr,Prev,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 //Tree is empty
RootßNew
ELSE
CurrßRoot
WHILE Curr <> Null
PrevßCurr
IF Tree[Curr].Data<NewValue
TurnedLeftßFALSE
CurrßTree[Curr].Right
ELSE
TurnedLeftßTRUE
CurrßTree[Curr].Left
END IF
END WHILE

IF TurnedLeft=TRUE
Tree[Prev].LeftßNew
ELSE
Tree[PRev].RightßNew
END IF
END IF
ELSE
OUTPUT “No more space”
END IF
END PROCEDURE

FINDING an ITEM in a TREE

FUNCTION Find(Value:STRING) RETURN INTEGER


DECLARE Curr : INTEGER
IF Root<>-1
CurrßRoot
WHILE Tree[Curr].Data<>Value AND Curr<>Null
IF Tree[Curr].Data<Value
CurrßTree[Curr].Right
ELSE
CurrßTree[Curr].Left
END IF
END WHILE
RETURN Curr
END IF
END FUNCTION

You might also like