0% found this document useful (0 votes)
16 views4 pages

01-Programming Ex1 - Trees

Uploaded by

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

01-Programming Ex1 - Trees

Uploaded by

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

Exercise 1: general (n-ary) tree

In this exercise, we shall build the structure of a discussion forum, where some discussions
are new and some are responses to other existing discussions.
The goal is to enable creating new discussions and discussion threads, updating
discussions, and deleting discussions (which entails deleting the subtree starting from the
deleted discussion).
Also, we want to allow for the option of printing the discussions, in an indented display, with
a response appearing indented relative to its parent discussion (each indentation is 3
spaces).
You are to create such a discussion tree, with every node containing the content of the
discussion, and the children of every node are the discussions which are a response to the
content of the given node. A discussion which has no responses will be a leaf node. A new
discussion (not a response) will be a root node of its own tree.

To summarize, these are the data structures to be created:


1. Node- a discussion node in a discussion tree, with the following attributes:
a. content- the contents of the current discussion
b. responses- a list objects of type Node, each being a response to the current
discussion.
2. Discussion tree with a Node* attribute, which is the beginning (root) of the discussion.
with the following methods:
a. Constructor- creates an empty tree
b. Desctructor- deletes the whole tree
c. Creates a root with given contents (and deleting the previous tree emanating
from this root, if exists)
d. Find method: receives a string, and returns a pointer to the node which
contains that string
e. Adds a node to the tree, as the child of another node, given two strings- one
of the father node which already exists in the tree, and one to appear in the
child which is to be added
f. Given a string, deletes the subtree starting from the node which contains that
string
g. Prints the complete tree, in a hierarchical display
h. Given a string, finds the node containing that string and prints the path from
the root to it
i. Given a string, searches for a node containing the given string, and prints the
tree starting from that node, all the way down to the leaves, in a hierarchical
display (i.e., prints the whole discussion from the given point)
3. Discussions list: a list of discussion trees, with the following methods:
a. Constructor- creates an empty list
b. Destructor- deletes all the trees in the list, resulting in an empty list
c. Creates a new discussion tree
d. Deletes a discussion tree (given a pointer to the tree's root)
e. Searches for a given string, in all the nodes of all the trees, and for each tree
in which the string was found, printing the path from the root till the occurring
string, and the rest of the subtree branching from the string's occurrence (if
the string appears more than once in a tree, the method does this only for the
first node that was found in each tree)
f. Adds a discussion to a given tree- the user enters three strings: a string of the
root node of the tree to which he wants to add, a string of the node to which
he wants to respond, and a string of his response. The method finds the
designated root node, and then calls the add method (e) of the discussion
tree object
g. Deletes a discussion from a given tree- the user enters two strings: a string of
the root node of the tree from which he wants to delete, and a string of the
node he wants to delete. The method finds the designated root node, and
then call the delete method (f) of the discussion tree object
h. Receives a string which is the root of a tree, and prints the whole tree, in a
hierarchical display
i. Receives a string which is the root of a tree, and a string of a discussion in
the tree (a node), and prints the whole sub-tree starting from that node, in a
hierarchical display.

Write a main program which will create an empty list of trees, and will allow, again and again
(displaying a menu within a main loop) to perform each of the methods defined in the
exercise, in the following way (it is recommended to use the main file from the Moodle):
● Create a new discussion tree- get as input the root node string, which begins the
discussion
● Respond to a discussion- get string S1, of a discussion root, get string S2 of a
discussion node in the same tree (it can be the root node itself, if the user wishes to
respond to that), and get string S3 of the response. The response must be added as
a son of S2’s node, in the tree whose root node is S1
● Delete a sub-tree- get string S1 of the discussion root, get string S2 of a discussion
node in the same tree, and delete the sub-tree rooted in S2’s node, belonging to the
tree whose root node is S1
● Print all discussions- in a hierarchical display
● Print a discussion sub-tree- get string S1 of a discussion root, get string S2 of a
discussion node in the same tree, and print the discussion starting from S2’s node, in
S1’s tree
● Search for a string in a discussion- get a string, and find its occurrences in the
discussion trees. For every occurrence found, print the path from the root of the tree
to the located node, and the complete subtree of the sub-discussion starting from the
located node (if in a given tree the string occurs more than once, the method does
this only for the first node that occurs in every tree)
● Quit- exit the main loop, delete all trees in the list (by calling their destructors)

Good luck!

Example execution output:


DISCUSSION TREE
Choose one of the following:
n: New discussion tree
s: Add a new response
d: Delete a sub-discussion
p: Print all discussion trees
r: Print a sub-tree
w: Search a string in all discussion trees
e: exit:
n
enter the discussion title (with no space) first-discussion
n
enter the discussion title (with no space) second-discussion
s
enter the discussion title (with no space) first-discussion
enter the last message (with no space) first-discussion
enter the new respond success
success
s
enter the discussion title (with no space) first-discussion
enter the last message (with no space) first-discussion
enter the new respond new-response
success
s
enter the discussion title (with no space) first-discussion
enter the last message (with no space) new-response
enter the new respond what
success
s
enter the discussion title (with no space) first-discussion
enter the last message (with no space) new-response
enter the new respond why
success
p
Tree #1
second-discussion

Tree #2
first-discussion
success
new-response
what
why

r
enter the discussion title (with no space) first-discussion
enter the last message (with no space) new-response

new-response
what
why
new-response=>first-discussion
w
enter a string (with no space) why

why
why=>new-response=>first-discussion

w
enter a string (with no space) new-response

new-response
what
why
new-response=>first-discussion
e
bye

You might also like