Académique Documents
Professionnel Documents
Culture Documents
Chapter Objectives
how to acquire the ability to manipulate or to use the different types if data
structures;
8-1
C1003 CHAPTER 8 : DATA STRUCTURES (II)
8.1 Introduction
So far, we have been studying mainly linear types of data structures: strings,
arrays, lists, stacks and queues. This section defines a non-linear data structure
called a tree. This structure is mainly used to represent data containing a
hierarchical relationship between elements. E.g. records, family trees and tables
of contents.
First we investigate a special kind of tree, called a binary tree, which can be
easily maintained in the computer. Although such a tree may seen to be very
restrictive, we will see later in the section that more general trees may be viewed
as binary trees.
8.2 Tree
Natural Tree
leaves
branches
roots
A natural tree
Computer tree
A simplified model of a real tree. Where each node has at most two others below
it. This is a binary tree.
Great-Great-Grand-parents Root
Great-Grand-parents Root
Parents Path
Sons and
daughters
8-2
C1003 CHAPTER 8 : DATA STRUCTURES (II)
The countries Malaysia, Namibia, Mauritius, Bahrain, Hong Kong, Pakistan, Sri
Lanka, Botswana, Singapore and India are to be placed in an alphabetical order
using pointers.
MALAYSIA 1
LAYER 1
Left Descendent Right Descendent
BAHRAIN 4 NAMIBIA 2
SINGAPORE 9
The text “TREE DIAGRAMS ARE QUITE EASY WHEN YOU KNOW HOW.”
is to be represented in the form of a tree.
TREE
DIAGRAMS WHEN
EASY
KNOW
HOW
8-3
C1003 CHAPTER 8 : DATA STRUCTURES (II)
8.3 Tables
In tables, there are basically 4 types of pointers:
i. Left Pointer;
ii. Right Pointer;
iii. Back Pointer/Parent Pointer;
iv. Trace Pointer.
The Pointers point to the left, right of the tree trace where they come from or to
the next node of the tree. This to preserve the structure of the tree.
The node number takes positive integers and ‘-1’ has special meaning.
NODE DATUM LP RP
1 Malaysia 4 2
2 Namibia 3 6
3 Mauritius -1 -1
4 Bahrain -1 5
5 Hong Kong 8 10
6 Pakistan -1 7
7 Sri Lanka 9 -1
8 Botswana -1 -1
9 Singapore -1 -1
10 India -1 -1
Malaysia 4 2
Namibia 3 6
Mauritius -1 -1
Bahrain -1 5
Hong Kong 8 10
Pakistan -1 7
Sri Lanka 9 -1
Botswana -1 -1
Singapore -1 -1
India -1 -1
8-4
C1003 CHAPTER 8 : DATA STRUCTURES (II)
Using the full set of pointers, the final tree can be represented below:
Malaysia 4 2 -1 3
Namibia 3 6 1 6
Mauritius -1 -1 2 2
Bahrain -1 5 1 8
Hong Kong 8 10 4 10
Pakistan -1 7 2 9
Sri Lanka 9 -1 6 -1
Botswana -1 -1 5 5
Singapore -1 -1 7 7
India -1 -1 5 1
MALAYSIA 1
BAHRAIN 4
HONG KONG 5
The Pointers
If T does contain a root R, then the two trees T1 and T2 are called, respectively,
the left and right subtrees of R. If T1 is non-empty, then its root is called the left
successor of R; similarly, if T2 is non-empty, then its root is called the right
successor of R.
8-5
C1003 CHAPTER 8 : DATA STRUCTURES (II)
Example: Software
o t
f r w
Observe that:
♦ O is a left successor and t is a right successor of the node s.
♦ The left subtle of the root s consists of the nodes o, f, r, a and e, and the right
subtree of s consists of the nodes t and w.
8-6
C1003 CHAPTER 8 : DATA STRUCTURES (II)
LT RT
B C
D E F
The inorder traversal of T traverses LT, processes A and traverses RT. However,
the inorder traversal of LT processes D, B and then E, and the inorder traversal of
RT processes C and then F. Hence DBEACF is the inorder traversal of T.
38
14 56
8 23 45 82
18 70
8-7
C1003 CHAPTER 8 : DATA STRUCTURES (II)
In other, proceed from the root R down through the tree T until finding ITEM in
T or inserting ITEM as a terminal node in T.
Example:
1. Compare ITEM = 20 with the root, 38, of the tree T. Since 20 < 38, proceed
to the left child of 38, which is 14.
2. Compare ITEM = 20 with 14. Since 20 > 14, proceed to the right child of
14, which is 23.
3. Compare ITEM = 20 with 23. Since 20 < 23, proceed to the left child of 23,
which is 18.
4. Compare ITEM = 20 with 18. Since 20 > 18 and 18 does not have a right
child, insert 20 as the right child of 18.
38
14 56
8 23 45 82
18 70
20
8-8
C1003 CHAPTER 8 : DATA STRUCTURES (II)
Points to Remember
♦ Binary trees are non-linear data structures in which the root and each node
on the tree contain a maximum of two children.
♦ A binary tree can be created within an array using left and right child
pointers similar to linked list concepts.
♦ Additions, changes and deletions of a left node are fairly easy.
♦ The 4 types of pointers in a binary can be represented in a form of a table.
♦ Three traversal of binary search tree, Preorder, Inorder and Postorder.
♦ A comparison.
8-9
C1003 CHAPTER 8 : DATA STRUCTURES (II)
Node 1 2 3 4 5 6 7 8
Content C J P T K B M S
[4]
b. Use a table to show the left, right, back and trace pointers for the above
tree. [8]
c. i. Write down all the pointers for the table below: [4]
ii. Re-arrange the pointers if contents 102 and 85 are removed. [2]
ii. From the above binary tree, build a table to show its Left, Right,
Back (parent).
8-10
C1003 CHAPTER 8 : DATA STRUCTURES (II)
b. i. Write down the pointers for the table below, where elements are
linked in numerical order: [2]
ii. Modify the table to reflect the additional of index 5 with key
value 66 and index 6 with key value 104. [4]
iii. Modify the table to reflect the deletion of index 2, key 100. [ 2 ]
Node 1 2 3 4 5 6 7 8 9 10 11
Contents Rice Soya Tea Coffee Melon Yams Bead Potatoes Cocoa Sugar Bananas
b. Represent the tree in the form of a TABLE including columns for the
LEFT and RIGHT pointers. [3]
c. Show how the TABLE would be used to search for SUGAR. [3]
d. Show the changes to the TABLE when MILK is added to the tree. [ 3 ]
8-11
C1003 CHAPTER 8 : DATA STRUCTURES (II)
Node 1 2 3 4 5 6 7 8 9 10
Hong
Contents Malaysia India Kuwait Nigeria Bahrain Pakistan Namibia Singapore Australia
Kong
9. A baker sells bread to ten shops every day. When he delivers the bread in the
morning, he collects the bread which was not sold the previous day, and
gives it to the local zoo for their animals. One day, he gets the following
number of loaves returned:
ShopA 3
ShopB 6
ShopC 2
ShopD 8
ShopE 0
ShopF 4
ShopG 7
ShopH 5
ShopI 12
ShopJ 1
a. Construct a binary tree for the above data, such that traversing the tree
will list the shops in increasing order of loaves returned. Mark each
node in the tree with both the shop name and the number of loaves e.g.
A(3) [8]
b. For your binary tree, build a table to show each node’s left, right and
back pointer. Use -1 to represent a null pointer. [6]
c. Redraw your table to reflect the deletion of shop I from the tree. [3]
d. Redraw your table to reflect the further deletion of shop D from your
tree. [3]
8-12