360 likes | 488 Views
Algorithms. Binary Trees. Learning Objectives. Describe algorithms for the insertion, retrieval and deletion of data items stored in a binary tree. Describe the use of a binary tree to sort data. Convert between reverse Polish notation and the infix form of algebraic expressions using trees.
E N D
Algorithms Binary Trees
Learning Objectives • Describe algorithms for the insertion, retrieval and deletion of data items stored in a binary tree. • Describe the use of a binary tree to sort data. • Convert between reverse Polish notation and the infix form of algebraic expressions using trees.
Tree • A data structure that emulates a tree structure with a set of linked nodes.
Binary Tree • A tree data structure in which each node has at most two children. • Normally has some sort of order. • The root node is the node with no parents (top most node). • The child nodes are called left and right.
The following data are placed in a binary tree: Binary Tree Poa, Siv, Jam, Ash, Ros, Lie If tree is empty enter data item at root and stop. Poa Siv > Poa so go right. If no node, create new node and enter data. Jam < Poa so go left If no node, create new node and enter data. Jam Siv Lie < Poa so go left Lie > Jam so go right If no node, create new node and enter data. Ros Ash Lie Ros > Poa so go right Ros < Siv so go left If no node, create new node and enter data. Ash < Poa so go left Ash < Jam so go left If no node, create new node and enter data.
Trees – Insertion Algorithm • If tree is empty enter data item at root and stop. • Current node = root. • If new data item is less than value at current node go left else go right. • Current node = node reached (null if no node). • Repeat steps 3 and 4 until current node is null. • Create new node and enter data.
Mathematical Expression Binary Trees • (a+b) – c*(d-e) • (a+b) – (c*(d-e)) • So two halves: • (a+b) - • (c*(d-e)) • Due to BODMAS: • B Brackets first • O Orders (ie Powers and Square Roots, etc.) • DM Division and Multiplication (left-to-right) • AS Addition and Subtraction (left-to-right)
- e)) + b) - (c * (d (a - + * - c a b d e
Mathematical Expression Binary Trees • X = c*(d-e) • So two halves: • X = • c*(d-e)
- e)) X = (c * (d = X * - c d e
Binary Tree Traversal (Reading) • In-Order (gives an alphabetical/numerical ordered list/infix form of a mathematical expression): • Traverse the left-hand sub tree. • Read the root node • Traverse the right-hand sub tree. • Pre-Order (gives a hierarchical ordered list): • Read the root node • Traverse the left-hand sub tree. • Traverse the right-hand sub tree. • Post-Order (gives the Reverse Polish Notation form of a mathematical expression binary tree): • Left Child, Right child, root
Algorithm: • Traverse the left-hand sub tree. • Read the root node. • Traverse the right-hand sub tree. In-Order Traversal: Poa Jam Siv Ros Ash Lie Ros, Ash, Jam, Lie, Poa, Siv Output: This gives an logically ordered list (alphabetical/numerical) or infix form of a mathematical expression.
Algorithm: • Read the root node. • Traverse the left-hand sub tree. • Traverse the right-hand sub tree. Pre-Order Traversal: Poa Jam Siv Ros Ash Lie Poa, Ash, Lie, Siv, Ros Jam, Output: This gives a hierarchal list (root, parents and children ordered by importance – left to right).
Algorithm: • Left Child. • Right child. • Root. Post-Order Traversal: - + * - c a b d e a b c d e - - * + Output: This is the Reverse Polish form of the infix mathematical expression: (a+b) – c*(d-e)
Algorithm: • Left Child. • Right child. • Root. Post-Order Traversal: = X * - c d e X c d e - = * Output: Note that X has to come first otherwise X will not be changed to the final value of the calculation (and the final value will be changed to X). This is the Reverse Polish form of the infix mathematical expression: X=c*(d-e)
For more details and traversal methods see: • http://en.wikipedia.org/wiki/Tree_traversal
Deletion Problems • Data in a tree serves two purposes (one is to be the data itself) the other is to act as a reference for the creation of the subtree below it • If a node is deleted there is no way of knowing which direction to take at that node to find the details of the data beneath it. Poa ? Siv ? ? Ros Ash Lie
Deletion Solution 1 • Store the node’s subtree in temporary storage and then rewrite it to the tree after the node is deleted. • One member of the subtree will now take over from the deleted node as the root of that subtree. • See the next 2 slides for an animated example.
Deletion Solution 1 To delete Jam: Ash, Lie 1. Store the node’s subtree in temporary storage. Poa Jam Siv Ros Ash Lie
Deletion Solution 1 To delete Jam: Ash, Lie 1. Store the node’s subtree in temporary storage. 2. Rewrite it to the tree after the node is deleted. Poa Siv Ash Ros Lie
Deletion Solution 2 Mark the data as deleted so that the data is not included when read but it can maintain its action as an index for that part of the tree so that the subtree can be correctly negotiated. To delete Jam: Poa Jam Siv Ros Ash Lie
Plenary • The data Nile, Zambesi, Amazon, Indus, Thames, Volga, Danube and Mississippi are to be entered into a binary tree in that order so that later these names can be extracted in alphabetical order. • Draw the representation of the binary tree with this data held in it.
Plenary • Describe how to insert a new value correctly into this tree.
Plenary • Compare with root. • If < go to left sub-tree. • Else go to right sub-tree. • Repeat until no sub-tree. • Insert at node.
Plenary • Describe how to convert the mathematical expression • (a+b) – c*(d-e) • into a binary tree.
- e)) + b) - (c * (d (a - + * - c a b d e
Plenary • Describe the major algorithms for reading a binary tree.
Binary Tree Traversal (Reading) • In-Order: • Traverse the left-hand sub tree. • Read the root node • Traverse the right-hand sub tree. • Pre-Order: • Read the root node • Traverse the left-hand sub tree. • Traverse the right-hand sub tree. • Post-Order: • Left Child, Right child, root
Plenary • Which method of reading a binary tree gives an alphabetical or numerical ordered list? • Can you describe an general algorithm for this method?
Algorithm: • Traverse the left-hand sub tree. • Read the root node. • Traverse the right-hand sub tree. In-Order Traversal: Poa Jam Siv Ros Ash Lie Ros, Ash, Jam, Lie, Poa, Siv Output:
Plenary • Which method of reading a binary tree gives the Reverse Polish Notation of a mathematical expression? • Can you describe an general algorithm for this method?
Algorithm: • Left Child. • Right child. • Root. Post-Order Traversal: - + * - c a b d e a b c d e - - * + Output: This is the Reverse Polish Form of the infix mathematical expression: (a+b) – c*(d-e)
Plenary • Which method of reading a binary tree gives an hierarchical list? • Can you describe an general algorithm for this method?
Algorithm: • Read the root node. • Traverse the left-hand sub tree. • Traverse the right-hand sub tree. Pre-Order Traversal: Poa Jam Siv Ros Ash Lie Poa, Ash, Lie, Siv, Ros Jam, Output: This gives a hierarchal list (root, parents and children ordered by importance – left to right).