Binary Tree
Binary Tree
27
16 21 42
17 89
55
Leaf node
+ –
a b c a
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
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.
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.
▲ Figure 19.13
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.
▲ Figure 19.14
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
▲ 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).