I am so stuck computer science
- 15-11-2016 00:35
- 15-11-2016 00:36
This is the problem i was never taught it and I have no clue what to do and there is nothing online
- 0 followers
- 0 badges
- 15-11-2016 07:24
Remember that when accessing the data within an array, the values of the nodes are 0 based, so if the array had the data of Q, S, G, D, and W input into its first 5 spots, with each character occupying a single space within the array, the value 'Q' would be located at the first node in the array, or: tree, 'S' would be at tree, G: tree, D: tree, and W: tree. Since it's 0 based, you need to consider tree as the first node in the array.
So, from here, adding the characters 'P' and 'T', they will occupy tree and tree.
Now, since this is a binary tree, the characters will be placed within the tree by their relative alphanumeric values (which you can lookup on an ASCII table). Since 'Q' is the first entry, it is placed at the top, but from here, each value that is input into the tree will be placed either to the left or to the right of 'Q', depending on whether its ASCII value is lesser or greater than 'Q' (which is #81 on the ASCII table).
So 'S' is put to the right, 'G' is put to the left, then 'D' to the left of 'G', and finally 'W' is placed at the end on the far right. When we insert 'P', the binary tree algorithm will check to see whether it is greater than or less than 'Q', the top of the tree. Since it is less than 'Q', it will branch to the left of that position, checking the value of 'G' next. It is greater than 'G', and there is no value in the space to the right of 'G', so it occupies that spot.
With 'T', it travels down the right side of the tree, as it's greater than 'Q', and greater than 'S'. Since it is less than 'W', it is placed to the left of the node pointing to the character 'W'.
From here, you shouldn't have any issues with determining the values that each node points to. For example, the data node tree has a value of 'G'. This node points to the values in node tree (character 'D') and node tree (character 'P'). So for the chart, you would write the left value as 3 and the right value as 5.
The one part I don't understand is why the chart says that the first node, tree, points to a value of 3 on the left, when it looks like it's pointing at 'G', which should be within tree, not tree. Might want to ask someone about that in your class.
Well, hopefully the information I gave you isn't wrong, but at least it can hopefully point you in the right direction. I'm struggling with CS as well; guess that's why they pay lots of money to people who can actually understand this stuff
- 20-11-2016 23:27