You are on page 1of 24

Politechnika Śląska

Wydział Informatyki, Elektroniki i Informatyki

Computer Programming 3

« BINARY TREE »

author Sofowora Oluwamayokun


instructor dr inż. Krzysztof Pasterak
year 2021/2022
lab group Thursday, 14:30 – 16:00
deadline 2022-02-20
2 SOFOWORA OLUWMAYOKUN
1 BINARY TREE 3

1 BINARY TREE
Implementing a program for sorting numbers with a binary search tree. Description of task

Numbers to sort are stored in a text file(serialized). Each number is in a


separate line. The sorted numbers are printed into an output text file in an
ascending order. Requirements: Class template: Made use of class template
T that allowed me to operate with generic data types. No STL containers.
Smart pointers: Made use of smart pointers, like unique pointer and shared
pointer for accessing the resources external to the program and avoid memory
leaks Copy and move constructors:Made use of the copy and move construct-
ors to create. Assignment and move operator. Method for adding an element.
Method for searching an element. Method for sorting (in case of sequential
data structures):Made use of three methods for sorting;pre-order, in-order,
post-order methods. Method for (de)serialization (transformation from and
to a binary stream)
Main text. Justify the
-i input file name text (left and right
The program is called with parameters: alignment).
-o output file name

2 Analysis of the task


Analyse the task before
The task focuses on adding numbers to the binary tree, sorting these you start to implement
numbers with a binary search tree and copy and moving these numbers and it! Describe data
structures and
printing them to a file. algorithms.

2.1 Data structures


Describe data
A binary search tree is used to store values read from an input file. Each structures (lists, trees,
heaps, , . . . ). Explain
node of a tree has 0, 1, or 2 children. Less values are stored in the left sub- your choice. Add a
figure.
tree, greater values – in the right sub-tree. Fig 1 presents an example of a
binary search tree.
Use «figure»
environment for figures.

2.2 Algorithms
Describe applied
The program add numbers to a binary search tree and then prints num- algorithms.
bers in depth-first search. Adding numbers and printing needs average time
O(n log n). [1].
literature, and
references ( [2]).

3 External specification
This is a command line program. The program requires names of input
External specification,a
short manual for a
user.Specifying how to
use your program,
number of parameters,
file format of input and
output data.
4 SOFOWORA OLUWMAYOKUN

4 10

2 5 15 25

1 12 18 27

Figure 1: An example of a binary search tree storing integers. The numbers


have been inserted into the tree in the following sequence: 8, 10, 4, 25, 15,
12, 18, 2, 1, 27, 5.

and output files. Put input file name after -i switch and output file name
after -o switch, e.g:

program -i input-file -o output-file


program -o output-file -i input-file

Both files are text files. The switches may be provided in any order. The
program called with no parameters or with parameter -h prints help (a short
manual).
For input commands
and output messages Program call
use a proportional
typeface.
program
program -h

prints a short manual. Program called with incorrect parameters prints an


error message:

Incorrect parameters!

and prints help. Incorrect file names are detected and cause a message:

File <filename> not found!

Make use of the switch on the console to call functions; insert, delete and
print/traverse.

4 Internal specification
Technical The program is implemented with structural paradigm. User interface is
documentation .
separated from program’s logic.
4.1 Program overview 5

4.1 Program overview


Program overview
aiming at providing a The main function calls verifyParameters that verifies parameters of the
nthetic description of program. If the verification is negative, an appropriate message is printed.
internal flow.
In case of positive verification, data are read with readData function. The
function opens a file stream, reads numbers from the stream, and inserts
numbers into a binary search tree with add function. Finally the program
print s numbers into an output file and removes the binary search tree from
the memory.

4.2 Description of types and functions


Description of types and functions is moved to the appendix.

5 Testing
Describing how the
The program has been tested with various types of files. Incorrect files program is tested.
(with no numbers, numbers in incorrect format) are detected and an error
message is printed. An empty input file does not cause failure – an empty
output file is created.Also testing on the compiler. (int may be implemented
as 2 or 4 B type). Maximal input file size handled by the program is 1.57 GB.
Larger files result in a bad allocation error. The program has no memory
leaks.

6 Conclusions
The program implements a simple sorting algorithm with a binary search
tree. The most challenging tasks is manual memory management without
any memory leaks and serializing. It is especially difficult when the data are
read partially and then an error in data occurs.
For some data the program elaborates incorrect results on some machines.
This is caused by various integer representation – some compilers use 2 B
integers whereas some – 4 B. For large numbers in input files 2 B integers are
too small.

References
[1] Thomas H. Cormen, Charles E. Leiserson, and Ronald L. Rivest. Intro-
duction to algorithms. University Press, 2001.
6 SOFOWORA OLUWMAYOKUN

[2] Tobias Oetiker, Hubert Partl, Irene Hyna, and Elisabeth Schlegl. The
not so short introduction to LATEX 2ε . 2018.
REFERENCES 7

Appendix
Description of types and
functions
My Project

Generated by Doxygen 1.9.3


i

1 Class Index 1
1.1 Class List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 File Index 3
2.1 File List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

3 Class Documentation 5
3.1 BST< T > Class Template Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 TreeNode< T > Class Template Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2.2 Constructor & Destructor Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2.2.1 TreeNode() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2.3 Member Function Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2.3.1 operator=() [1/2] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2.3.2 operator=() [2/2] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

4 File Documentation 9
4.1 functions.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.2 structures.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Index 13

Generated by Doxygen
Chapter 1

Class Index

1.1 Class List

Here are the classes, structs, unions and interfaces with brief descriptions:

BST< T > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
TreeNode< T > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Generated by Doxygen
2 Class Index

Generated by Doxygen
Chapter 2

File Index

2.1 File List

Here is a list of all documented files with brief descriptions:

functions.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ??
structures.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ??

Generated by Doxygen
4 File Index

Generated by Doxygen
Chapter 3

Class Documentation

3.1 BST< T > Class Template Reference

Public Member Functions

• bool isTreeEmpty ()
• void insertNode (std::shared_ptr< TreeNode< T > > new_node)
• std::shared_ptr< TreeNode< T > > iterativeSearch (int val)
• void print2D (std::shared_ptr< TreeNode< T > > r, int space)
• std::shared_ptr< TreeNode< T > > deleteNode (std::shared_ptr< TreeNode< T > > r, int v)
• std::shared_ptr< TreeNode< T > > minValue (std::shared_ptr< TreeNode< T > > node)
• string serialize (std::shared_ptr< TreeNode< T > > root)
• std::shared_ptr< TreeNode< T > > predeserialize (string data)
• std::shared_ptr< TreeNode< T > > preorder (string data, int &pos)
• void printInorder (std::shared_ptr< TreeNode< T > > r)
• string serialize (std::shared_ptr< TreeNode< T > > root)
• void printPostorder (std::shared_ptr< TreeNode< T > > r)
• string postserialize (std::shared_ptr< TreeNode< T > > root)
• std::shared_ptr< TreeNode< T > > postdeserialize (string &data)
• void serialize (std::shared_ptr< TreeNode< T > > node, ostream &sstream)
• std::shared_ptr< TreeNode< T > > deserialize (istream &sstream)
• void test (list< int > &l, bool display_output=false)
• void print (const std::string &outputFileName, std::shared_ptr< TreeNode< T > > root)
• void readfile (string filename)

Public Attributes

• std::shared_ptr< TreeNode< T > > root


• const int MARKER = numeric_limits<int>::min()

The documentation for this class was generated from the following file:

• B_TREE.cpp

Generated by Doxygen
6 Class Documentation

3.2 TreeNode< T > Class Template Reference

#include <structures.h>

Public Member Functions

• TreeNode (int v)
• TreeNode (const TreeNode< T > &other)
• TreeNode & operator= (const TreeNode< T > &other)
• TreeNode (const TreeNode< T > &&other)
• TreeNode & operator= (const TreeNode< T > &&other)
• std::shared_ptr< TreeNode< T > > make_copy (std::shared_ptr< TreeNode< T > > other)

Public Attributes

• T value
key or data
• std::shared_ptr< TreeNode< T > > left
Node pointers.
• std::shared_ptr< TreeNode< T > > right

3.2.1 Detailed Description

template<typename T>
class TreeNode< T >

A class node for a binary search tree.

3.2.2 Constructor & Destructor Documentation

3.2.2.1 TreeNode()

template<typename T >
TreeNode< T >::TreeNode (
int v ) [inline]

Parameters
v constructor

Generated by Doxygen
3.2 TreeNode< T > Class Template Reference 7

3.2.3 Member Function Documentation

3.2.3.1 operator=() [1/2]

template<typename T >
TreeNode & TreeNode< T >::operator= (
const TreeNode< T > && other ) [inline]

Parameters
other move assignment operator

3.2.3.2 operator=() [2/2]

template<typename T >
TreeNode & TreeNode< T >::operator= (
const TreeNode< T > & other ) [inline]

Parameters
other copy assignment operator

The documentation for this class was generated from the following file:

• structures.h

Generated by Doxygen
8 Class Documentation

Generated by Doxygen
Chapter 4

File Documentation

4.1 functions.h
1
3 #ifndef FUNCTIONS_H
4 #define FUNCTIONS_H
5
6 #include <iostream>
7 #include <memory>
8 #include <string>
9 #include "structures.h"
10
14 bool isTreeEmpty();
15
22 template<typename T>
23 void insertNode(std::shared_ptr<TreeNode<T>> new_node);
24
30 template<typename T>
31 std::shared_ptr<TreeNode<T» iterativeSearch(int val);
32
38 template<typename T>
39 std::shared_ptr<TreeNode<T» deleteNode(std::shared_ptr<TreeNode<T>> r, int v);
40
45 template<typename T>
46 std::shared_ptr<TreeNode<T» minValue(std::shared_ptr<TreeNode<T>> node);
47
52 template<typename T>
53 string serialize(std::shared_ptr<TreeNode<T>> root);
54
59 template<typename T>
60 std::shared_ptr<TreeNode<T» predeserialize(string data);
61
67 template<typename T>
68 std::shared_ptr<TreeNode<T» preorder(string data, int& pos);
69
74 template<typename T>
75 string serialize(std::shared_ptr<TreeNode<T>> root);
76
81 template<typename T>
82 string postserialize(std::shared_ptr<TreeNode<T>> root);
83
88 template<typename T>
89 std::shared_ptr<TreeNode<T» postdeserialize(string& data);
90
96 template<typename T>
97 void serialize(std::shared_ptr<TreeNode<T>> node, ostream& sstream);
98
104 template<typename T>
105 std::shared_ptr<TreeNode<T» deserialize(istream& sstream);
106
110 template<typename T>
111 void test(list<int>& l, bool display_output = false);
112
117 template<typename T>
118 void printPostorder(std::shared_ptr<TreeNode<T>> r);
119
124 template<typename T>
125 void printInorder(std::shared_ptr<TreeNode<T>> r);
126
131 template<typename T>
132 void printPreorder(std::shared_ptr<TreeNode<T>> r);

Generated by Doxygen
10 File Documentation

133
139 template<typename T>
140 void print2D(std::shared_ptr<TreeNode<T>> r, int space);
141
146 template<typename T>
147 void print(const std::string& outputFileName, std::shared_ptr<TreeNode<T>> root);
148
153 void readfile(std::string filename);
154
163 params_result verifyParameters(int param_count, char** params, std::string& inputFileName, std::string&
outputFileName);
164
165 #endif

4.2 structures.h
1 #ifndef STRUCTURES_H
2 #define STRUCTURES_H
3 #include <memory>
4
6 template<typename T>
7 class TreeNode
8 {
9 public:// directly accessible
10 T value;
11 std::shared_ptr<TreeNode<T» left;
12 std::shared_ptr<TreeNode<T» right;
13 TreeNode()
14 {
15 value = 0;
16 left = NULL;
17 right = NULL;
18 }
19 TreeNode(int v)
20 {
21 value = v;
22 left = NULL;
23 right = NULL;
24 }
25 TreeNode(const TreeNode<T>& other):left(nullptr),right(nullptr),value(other.value)
26 {
27 if (other.left != nullptr)
28 {
29 left = new TreeNode<T>(*other.left);
30 }
31 if (other.right != nullptr)
32 {
33 right = new TreeNode<T>(*other.right);
34 }
35 }
36 TreeNode& operator=(const TreeNode<T>& other)
37 {
38 value = other.value;
39
40 std::shared_ptr<TreeNode<T» left = l;
41 left = new TreeNode<T>(*other.left);
42 delete left;
43
44 std::shared_ptr<TreeNode<T» right = r;
45 right = new TreeNode<T>(*other.right);
46 delete right;
47
48 return *this;
49 }
50 TreeNode(const TreeNode<T>&& other) :left(nullptr), right(nullptr), value(other.value)
51 {
52 value = other.value;// Copy the data pointer and its length from the
53 right = other.right;
54 left = other.left;
55 // Release the data pointer from the source object so that the destructor does not free the memory
multiple times.
56 other.value = 0;
57 other.right = nullptr;
58 other.left = nullptr;
59 }
60 TreeNode& operator=(const TreeNode<T>&& other)
61 {
62 if (this != &other) {
63 delete[] this->value; // Free this string’s original data.
64 this->value = other.value; // Copy the other string’s data pointer into this string.
65 other.value = nullptr; // Finally, reset the other string’s data pointer.
66 }
67 return *this;

Generated by Doxygen
4.2 structures.h 11

68 }
69 ~TreeNode()
70 {
71 if (left != nullptr)
72 {
73 delete left;
74 }
75 if (right != nullptr)
76 {
77 delete right;
78 }
79 }
80 std::shared_ptr<TreeNode<T» make_copy(std::shared_ptr<TreeNode<T>> other)
81 {
82 unique_ptr<TreeNode<T» new_node = new TreeNode<T>(*other); // copy constructor invoked
83 return new_node;
84 }
85 };
86
88 enum params_result
89 {
90 PARAMS_OK,
91 PARAMS_HELP,
92 PARAMS_ERROR
93 };
94
95 #endif

Generated by Doxygen
12 File Documentation

Generated by Doxygen
Index

BST< T >, 5

operator=
TreeNode< T >, 7

TreeNode
TreeNode< T >, 6
TreeNode< T >, 6
operator=, 7
TreeNode, 6

Generated by Doxygen

You might also like