1 / 36

Algorithms

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.

gryta
Télécharger la présentation

Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms Binary Trees

  2. 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.

  3. Tree • A data structure that emulates a tree structure with a set of linked nodes.

  4. 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.

  5. Creating a Sorted Binary Tree&Inserting New Data Items

  6. 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.

  7. 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.

  8. 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)

  9. - e)) + b) - (c * (d (a - + * - c a b d e

  10. Mathematical Expression Binary Trees • X = c*(d-e) • So two halves: • X = • c*(d-e)

  11. - e)) X = (c * (d = X * - c d e

  12. 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

  13. 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.

  14. 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).

  15. 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)

  16. 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)

  17. For more details and traversal methods see: • http://en.wikipedia.org/wiki/Tree_traversal

  18. 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

  19. 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.

  20. Deletion Solution 1 To delete Jam: Ash, Lie 1. Store the node’s subtree in temporary storage. Poa Jam Siv Ros Ash Lie

  21. 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

  22. 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

  23. 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.

  24. Plenary

  25. Plenary • Describe how to insert a new value correctly into this tree.

  26. Plenary • Compare with root. • If < go to left sub-tree. • Else go to right sub-tree. • Repeat until no sub-tree. • Insert at node.

  27. Plenary • Describe how to convert the mathematical expression • (a+b) – c*(d-e) • into a binary tree.

  28. - e)) + b) - (c * (d (a - + * - c a b d e

  29. Plenary • Describe the major algorithms for reading a binary tree.

  30. 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

  31. Plenary • Which method of reading a binary tree gives an alphabetical or numerical ordered list? • Can you describe an general algorithm for this method?

  32. 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:

  33. 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?

  34. 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)

  35. Plenary • Which method of reading a binary tree gives an hierarchical list? • Can you describe an general algorithm for this method?

  36. 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).

More Related