<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-6%3Ajpar277</id>
	<title>SE250:lab-6:jpar277 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-6%3Ajpar277"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-6:jpar277&amp;action=history"/>
	<updated>2026-04-28T22:36:46Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://wiki.kram.nz/index.php?title=SE250:lab-6:jpar277&amp;diff=7165&amp;oldid=prev</id>
		<title>Mark: 18 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-6:jpar277&amp;diff=7165&amp;oldid=prev"/>
		<updated>2008-11-03T05:20:06Z</updated>

		<summary type="html">&lt;p&gt;18 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Overview ==&lt;br /&gt;
It took a while to read the code and understand what was going on. But once I understood the functions and what they were doing, I think this was a reasonably simple lab. It was a little time consuming looking up things here n there to help with the tasks and that resulted in me not completing the lab. Perhaps I will finish it some other time.&lt;br /&gt;
&lt;br /&gt;
== Task1 ==&lt;br /&gt;
&amp;lt;html&amp;gt;&amp;lt;img src=&amp;quot;http://img375.imageshack.us/img375/5721/graphbm5.jpg&amp;quot; border=&amp;quot;0&amp;quot; /&amp;gt;&amp;lt;/html&amp;gt;&lt;br /&gt;
This is the graph of data I got. The tree size goes from 1 to 500, as you can see from the graph, the max height seems to level off around 18 at a tree size of about 300.&lt;br /&gt;
&lt;br /&gt;
I realised that because the data generated and stored is random&amp;lt;html&amp;gt;&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;&amp;lt;/html&amp;gt;, we get different results everytime, instead of averaging multiple sets of data, I just took the set of data that came up once I figured out how to write the code properly (saved to a txt file, transferred the data to excel to produce the graph).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html&amp;gt;&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;&amp;lt;/html&amp;gt; This I feel is extremely weird, because we haven&amp;#039;t seeded the rand function, we should technically be getting the same results everytime shouldn&amp;#039;t we???&lt;br /&gt;
&lt;br /&gt;
== Task2 ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Minimum&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 Node* minimum( Node *node ) {&lt;br /&gt;
 	if (node == empty) {&lt;br /&gt;
 		return empty;&lt;br /&gt;
 	}&lt;br /&gt;
 	while (node-&amp;gt;left != empty) {&lt;br /&gt;
 		node = node-&amp;gt;left;&lt;br /&gt;
 	}&lt;br /&gt;
 	return node;&lt;br /&gt;
 }&lt;br /&gt;
This is the code used for minimum - took me a while to get my head around it I had looping issues the first 10 or so attempts &amp;gt;&amp;lt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Maximum&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 Node* maximum( Node *node ) {&lt;br /&gt;
 	if (node == empty) {&lt;br /&gt;
 		return empty;&lt;br /&gt;
 	}&lt;br /&gt;
 	while (node-&amp;gt;right != empty) {&lt;br /&gt;
 		node = node-&amp;gt;right;&lt;br /&gt;
 	}&lt;br /&gt;
 	return node;&lt;br /&gt;
 }&lt;br /&gt;
This is the code used for maximum.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Problem&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
This is the code I used in my main function.&lt;br /&gt;
 	Node *binaryTree;&lt;br /&gt;
 	Node *min;&lt;br /&gt;
 	Node *max;&lt;br /&gt;
 	int minKey;&lt;br /&gt;
 	int maxKey;&lt;br /&gt;
 &lt;br /&gt;
 	binaryTree = empty;&lt;br /&gt;
 	min = minimum(binaryTree);&lt;br /&gt;
 	max = maximum(binaryTree);&lt;br /&gt;
 &lt;br /&gt;
 	show_tree(0, binaryTree);&lt;br /&gt;
 &lt;br /&gt;
 	printf(&amp;quot;min key = %d, max key = %d&amp;quot;, min-&amp;gt;key, max-&amp;gt;key);&lt;br /&gt;
The problem I had was when I tried to produce the key for an empty loop, that because the tree is empty it comes up with an error when trying to access &amp;quot;min-&amp;gt;key&amp;quot; and &amp;quot;max-&amp;gt;key&amp;quot; because they don&amp;#039;t exist. Is there a way around this error???&lt;br /&gt;
&lt;br /&gt;
== Task3 ==&lt;br /&gt;
 Node* lookup( int key, Node* node ) {&lt;br /&gt;
 	if (node == empty) {&lt;br /&gt;
 		return empty;&lt;br /&gt;
 	}&lt;br /&gt;
 	while (key != node-&amp;gt;key) {&lt;br /&gt;
 		if (key &amp;gt; node-&amp;gt;key) {&lt;br /&gt;
 			node = node-&amp;gt;right;&lt;br /&gt;
 		} else {&lt;br /&gt;
 			node = node-&amp;gt;left;&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 	return node;&lt;br /&gt;
 }&lt;br /&gt;
This is the code I came up with, it works. But I feel like this function is rather useless at this stage - when we call this function, we have to have an if statement to check whether it actually found the key we were looking for.&lt;br /&gt;
Perhaps if we had other bits of data attached to it, this would be more useful.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Task4 ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Next in Preorder&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 Node* next_pre( Node* node ) {&lt;br /&gt;
 	if (node-&amp;gt;left != empty) {&lt;br /&gt;
 		return node-&amp;gt;left;&lt;br /&gt;
 	} else if (node-&amp;gt;right != empty) {&lt;br /&gt;
 		return node-&amp;gt;right;&lt;br /&gt;
 	} else {&lt;br /&gt;
 		if (node-&amp;gt;parent-&amp;gt;right != empty) {&lt;br /&gt;
 			if (node != node-&amp;gt;parent-&amp;gt;right) {&lt;br /&gt;
  				return node-&amp;gt;parent-&amp;gt;right;&lt;br /&gt;
 			}&lt;br /&gt;
 		} else {&lt;br /&gt;
 			return empty;&lt;br /&gt;
 		}		&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
This is the code I came up with, not sure if it works properly, would&amp;#039;ve been nice if we were given some test cases to use instead of creating our own.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Previous in Preorder&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 Node* prev_pre( Node* node ) {&lt;br /&gt;
 	if (node-&amp;gt;parent != empty) {&lt;br /&gt;
 		if (node == node-&amp;gt;parent-&amp;gt;right) {&lt;br /&gt;
 			if (node-&amp;gt;parent-&amp;gt;left != empty) {&lt;br /&gt;
 				return node-&amp;gt;parent-&amp;gt;left;&lt;br /&gt;
 			} else {&lt;br /&gt;
 				return node-&amp;gt;parent;&lt;br /&gt;
 			}&lt;br /&gt;
 		} else {&lt;br /&gt;
 			return node-&amp;gt;parent;&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 	return empty;&lt;br /&gt;
 }&lt;br /&gt;
Again, I&amp;#039;m not 100% sure if this works properly.&lt;br /&gt;
&lt;br /&gt;
== Task5 ==&lt;br /&gt;
 Node* next_in( Node* node ) {&lt;br /&gt;
 	if (node-&amp;gt;parent != empty) {&lt;br /&gt;
 		if (node == node-&amp;gt;parent-&amp;gt;left) {&lt;br /&gt;
 			if (node-&amp;gt;right != empty) {&lt;br /&gt;
 				return minimum(node-&amp;gt;right);&lt;br /&gt;
 			} else {&lt;br /&gt;
 				return node-&amp;gt;parent;&lt;br /&gt;
 			}&lt;br /&gt;
 		} else if (node-&amp;gt;parent-&amp;gt;parent != empty) {&lt;br /&gt;
 			return next_in(node-&amp;gt;parent-&amp;gt;parent);&lt;br /&gt;
 		}&lt;br /&gt;
 	} else {&lt;br /&gt;
 		if (node-&amp;gt;right != empty) {&lt;br /&gt;
 				return minimum(node-&amp;gt;right);&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 	return empty;&lt;br /&gt;
 }&lt;br /&gt;
Had to use recursion.&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>