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

Binary Tree

A binary tree is a hierarchical data structure where each parent node has at most two child nodes. Binary trees can be used to represent expressions and for organizing sorted data. The document provides an example of a binary tree storing sorted data and traversing it to find a particular item. Pseudocode is given for creating a binary tree data structure and an algorithm for searching for an item in the tree.

Uploaded by

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

Binary Tree

A binary tree is a hierarchical data structure where each parent node has at most two child nodes. Binary trees can be used to represent expressions and for organizing sorted data. The document provides an example of a binary tree storing sorted data and traversing it to find a particular item. Pseudocode is given for creating a binary tree data structure and an algorithm for searching for an item in the tree.

Uploaded by

Riaz Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Binary trees

A binary tree is another frequently used ADT. It is a hierarchical data structure


in which each parent node can have a maximum of two child nodes. There
are many uses for binary trees; for example, they are used in syntax analysis,
compression algorithms and 3D video games.
Figure 19.10 shows the binary tree for the data stored in myList sorted in
ascending order. Each item is stored at a node and each node can have up to
two branches with the rule if the value to be added is less than the current
node branch left, if the value to be added is greater than or equal to the
current node branch right.

Left pointer Root node


Right pointer

27

Left subtree Right subtree


19 36

16 21 42

17 89

55

Leaf node

▲ Figure 19.10 Example of an ordered binary tree

A binary tree can also be used to represent an arithmetic expression. Consider


(a + b) * (c – a)

+ –

a b c a

▲ Figure 19.11 Example of an expression as a binary tree

The data structure for an ordered binary tree can be created in pseudocode as
follows:
TYPE node
DECLARE item : INTEGER
DECLARE leftPointer : INTEGER
DECLARE rightPointer : INTEGER
ENDTYPE
DECLARE myTree[0 : 8] OF node
DECLARE rootPointer : INTEGER
DECLARE nextFreePointer : INTEGER

457591_19_CI_AS & A_Level_CS_450-497.indd 481 26/04/19 9:14 AM


The populated contents of the data structure myTree is shown below.

myTree item leftPointer rightPointer Pointers to


[0] 27 1 2
items in the
Root pointer tree. −1 is
[1] 19 4 6
used as a
[2] 36 −1 3 null pointer
[3] 42 −1 5
[4] 16 −1 7
[5] 89 8 −1
[6] 21 −1 −1
[7] 17 −1 −1
[8] 55 −1 −1
▲ Figure 19.12

The root pointer points to the first node in a binary tree. A null pointer is a
value stored in the left or right pointer in a binary tree to indicate that there
are no nodes below this node on the left or right.
Finding an item in a binary tree
The algorithm to find if an item is in the binary tree myTree and return the
pointer to its node if found or a null pointer if not found, could be written as a
function in pseudocode, as shown.

DECLARE rootPointer : INTEGER


DECLARE itemPointer : INTEGER
DECLARE itemSearch : INTEGER
CONSTANT nullPointer = -1
rootPointer ← 0
FUNCTION find(itemSearch) RETURNS INTEGER
itemPointer ← rootPointer
WHILE myTree[itemPointer].item <> itemSearch AND
(itemPointer <> nullPointer) DO
IF myTree[itemPointer].item > itemSearch
THEN
itemPointer ← myTree[itemPointer].leftPointer
ELSE
itemPointer ← myTree[itemPointer].rightPointer
ENDIF
ENDWHILE
RETURN itemPointer

457591_19_CI_AS & A_Level_CS_450-497.indd 482 26/04/19 9:14 AM


Here is the identifier table for the binary tree search algorithm shown above.

Identifier Description
myTree Tree to be searched
node ADT for tree
rootPointer Pointer to the start of the tree
leftPointer Pointer to the left branch
rightPointer Pointer to the right branch
nullPointer Null pointer set to −1
itemPointer Pointer to current item
itemSearch Item being searched for
▲ Table 19.20

The trace table below shows the algorithm being used to search for 42 in
myTree.

rootPointer itemPointer itemSearch


0 0 42
2
3

▲ Table 19.21 Trace table

Inserting items into a binary tree


The binary tree needs free nodes to add new items. For example, myTree,
shown in Figure 19.13 below, now has room for 12 items. The last three nodes
have not been filled yet, there is a pointer to the next free node and the free
nodes are set up like a heap in a linked list, using the left pointer.

myTree item leftPointer rightPointer pointers to


Root pointer [0] 27 1 2 items in the
[1] 19 4 6 tree. −1 is used
[2] 36 −1 3 as a null pointer
[3] 42 −1 5
[4] 16 −1 7
[5] 89 8 −1
[6] 21 −1 −1 Leaves have
[7] 17 −1 −1 null left and
[8] 55 −1 −1 right pointers
next free
[9] 10
pointer
[10] 11
[11] −1

▲ Figure 19.13

457591_19_CI_AS & A_Level_CS_450-497.indd 483 26/04/19 9:14 AM


The algorithm to insert an item at a new node in the binary tree myTree could be written as a procedure
in pseudocode as shown below.
TYPE node
DECLARE item : INTEGER
DECLARE leftPointer : INTEGER
DECLARE rightPointer : INTEGER
DECLARE oldPointer : INTEGER
DECLARE leftBranch : BOOLEAN
ENDTYPE
DECLARE myTree[0 : 11] OF node
// binary tree now has extra spaces
DECLARE rootPointer : INTEGER
DECLARE nextFreePointer : INTEGER
DECLARE itemPointer : INTEGER
DECLARE itemAdd : INTEGER
DECLARE itemAddPointer : Integer
CONSTANT nullPointer = -1
// needed to use the binary tree
PROCEDURE nodeAdd(itemAdd)
// check for full tree
IF nextFreePointer = nullPointer
THEN
OUTPUT "No nodes free"

457591_19_CI_AS & A_Level_CS_450-497.indd 484 26/04/19 9:14 AM


ELSE
//use next free node
itemAddPointer ← nextFreePointer
nextFreePointer ← myTree[nextFreePointer].leftPointer
itemPointer ← rootPointer
// check for empty tree
IF itemPointer = nullPointer
THEN
rootPointer ← itemAddPointer
ELSE
// find where to insert a new leaf
WHILE (itemPointer <> nullPointer) DO
oldPointer ← itemPointer
IF myTree[itemPointer].item > itemAdd
THEN // choose left branch
leftBranch ← TRUE
itemPointer ← myTree[itemPointer].leftPointer
ELSE // choose right branch
leftBranch ← FALSE
itemPointer ← myTree[itemPointer].rightPointer
ENDIF
ENDWHILE
IF leftBranch //use left or right branch
THEN
myTree[oldPointer].leftPointer ← itemAddPointer
ELSE
myTree[oldPointer].rightPointer ← itemAddPointer

ENDIF
ENDIF
// store item to be added in the new node
myTree[itemAddPointer].leftPointer ← nullPointer

myTree[itemAddPointer].rightPointer ← nullPointer

myTree[itemAddPointer].item ← itemAdd
ENDIF
ENDPROCEDURE

457591_19_CI_AS & A_Level_CS_450-497.indd 485 26/04/19 9:14 AM


Here is the identifier table.

Identifier Description
myTree Tree to be searched
node ADT for tree
rootPointer Pointer to the start of the tree
leftPointer Pointer to the left branch
rightPointer Pointer to the right branch
nullPointer Null pointer set to -1
itemPointer Pointer to current item in tree
itemAdd Item to add to tree
nextFreePointer Pointer to next free node
itemAddPointer Pointer to position in tree to store item to
be added
oldPointer Pointer to leaf node that is going to point
to item added
leftBranch Flag to identify whether to go down the
left branch or the right branch
▲ Table 19.22

The trace table below shows the algorithm being used to add 18 to myTree.

leftBranch nextFreePointer itemAddPointer rootPointer itemAdd itemPointer oldPointer


Already set to 9 9 Already set to 0 18
10 0 0
TRUE 1 1
TRUE 4 4
FALSE 7 7
−1

▲ Table 19.23 The tree, myTree will now be as shown below.

myTree item leftPointer rightPointer


[0] 27 1 2
[1] 19 4 6
[2] 36 −1 3
[3] 42 −1 5
[4] 16 −1 7
[5] 89 8 −1 pointer to
[6] 21 −1 −1
new node
in correct
[7] 17 −1 9
position
[8] 55 −1 −1
[9] 18 −1 −1 new leaf node
next free [10] 11
pointer now 10
[11] −1

▲ Figure 19.14

457591_19_CI_AS & A_Level_CS_450-497.indd 486 26/04/19 9:14 AM


Implementing binary trees in Python, VB.NET or Java requires the use of
objects and recursion. An example will be given in Chapter 20.

Graphs
A graph is a non-linear data structure consisting of nodes and edges. This is an
ADT used to implement directed and undirected graphs. A graph consists of a
set of nodes and edges that join a pair of nodes. If the edges have a direction
from one node to the other it is a directed graph.

Nodes
Edges

Undirected graph Directed graph

▲ Figure 19.15

As we saw in Chapter 18, graphs are used to represent real life networks, such as
» bus routes, where the nodes are bus stops and the edges connect two stops
next to each other
» websites, where each web page is a node and the edges show the links
between each web page
» social media networks, where each node contains information about a
person and the edges connect people who are friends.

Each edge may have a weight; for example, in the bus route, the weight could
be the distance between bus stops or the cost of the bus fare.
A path is the list of nodes connected by edges between two given nodes and a
cycle is a list of nodes that return to the same node.
For example, a graph of the bus routes in a town could be as follows. The
distance between each bus stop in kilometres is shown on the graph.

Town 0.5
School
centre
0.5 0.9

Shopping Train
1.5 2.0 2.5
centre station

0.5 1.2

Gardens River
0.7

▲ Figure 19.16

A path from School to Gardens could be Path = (School, Train station, River,
Gardens).

457591_19_CI_AS & A_Level_CS_450-497.indd 487 26/04/19 9:14 AM

You might also like