Trees (OCR A Level Computer Science)
Revision Note
Written by: Jennifer Page
Reviewed by: James Woodhouse
Implementing a Tree
How Do You Program a Tree?
In the following example:
The tree data structure is represented by a ‘TreeNode’ struct which has a ‘value’ field to store the value of the node and a ’children’ field to hold a collection of child nodes
The ‘createTreeNode’ function is used to create a new tree node and initialise its value and an empty children collection
The ‘addChild’ function is used to add a child to a parent node. It creates a new child node with the specified value and appends it to the ‘children’ collection of the parent node
Then the ‘createTreeNode’ creates the root node and adds children to it using the ‘addChild’ function. This allows the build up of the tree structure with multiple levels and branches
Pseudocode |
|
Python |
|
Java |
|
Algorithm to Traverse a Tree
Post Order Traversal
Post-Order traversal follows the order:
Left Subtree
Right Subtree
Root Node
Using the outline method, nodes are traversed in the order in which you pass them on the right
You can recap the theory notes on Tree Data Structures here.
Note: The algorithm below shows that this is identical to the other methods of traversing a tree (pre-order and in-order) except for the position of the output statement. You do not require the other two methods for OCR
Node is an object with 3 attributes; node.data contains a value, node.left contains a pointer to the left child node and node.right contains a pointer to the right child node
As mentioned above, the algorithm will traverse the left subtree, then the right subtree then the root
Pseudocode |
|
Python |
|
Java |
|
Algorithm to Add Data to a Tree
In your exam, the exam board quite often uses binary trees in their questions. The algorithm below will use a binary tree for that reason.
The code defines a binary tree and a function to add a new node to the tree.
The binary tree is represented using a class called ‘Node’, where each node has a data value, a left child, and a right child.
The add_node function takes two parameters: the root of the binary tree and the value of the new node to be added.
If the tree is empty (root is ‘None’), it creates a new node with the given value and sets it as the root.
Otherwise, it performs a traversal using a queue to find the first available position (either the left or right child of a node) to insert the new node.
Pseudocode |
|
Python |
|
Java |
|
Algorithm to Remove Data from a Tree
Once again, the algorithm below will use a binary tree
The find_node function is a function that searches for a node with a given value in the tree.
The remove_node function removes a node with a specified value from the tree, handling three cases:
Node has no children (Leaf Node):
Remove the node from the tree by setting it to ‘None’.
Node has one child:
Replace the node with its only child.
Node has two children:
Find the minimum value node in the right subtree (or the maximum value node in the left subtree).
Replace the node's data with the data of the replacement node.
Recursively remove the replacement node.
The find_minimum function is a function that helps to find the node with the minimum value in a given subtree.
Pseudocode |
|
Python |
|
Java |
|
Last updated:
You've read 0 of your 5 free revision notes this week
Sign up now. It’s free!
Did this page help you?