1 / 26

Tree Traversals: Preorder, Inorder, Postorder Explained

Learn about tree traversals, including preorder, inorder, and postorder, and how they are used to extract data from a tree. Understand representations of trees and the Tree ADT. Explore terms such as root, leaf, parent, child, and level. Discover how trees are a subset of graphs and how searching methods can be applied to trees.

Download Presentation

Tree Traversals: Preorder, Inorder, Postorder Explained

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. CS 240: Data Structures Monday, July 28th Trees – Traversals, Representations, Recursion and Helpers

  2. Changes • There are some changes to Lab 7 and Project 3.

  3. Project 3 • Some clarificatons: • These are clarifications, as long as your game has reasonable rules it is ok. However, you should indicate the rules of your game so that I know what I’m looking at. • Wikipedia links to various rulebooks for Risk. Consider look at the 40th Anniversary Edition.

  4. Project 3 • Starting the game: • Divide all of the countries amongst the players (2 required, multiple players is fine). Distribute any excess countries in a reasonable manner (Such as reverse order from last player to first). Alternatively, players can choose countries one at a time until none remain. • You don’t need to use “Global Domination Rules”. For 2 players, give 40 armies, 3 players 35 armies, 4 players 30 armies and so on. • Players then distribute their armies on their territories (a minimum of one of each territory)

  5. Project 3 • Then, alternate turns until the world is owned by 1 player. • At the beginning of each turn: • The player gets max(floor(Territory/3) + continent bonus,3) armies as reinforcements each turn that they can place on any of their territories. • Then, the player can attack, or end their turn

  6. Project 3 • Attacking: • Choose a source territory and a target territory. • The attacker may roll min(3, armies-1) dice. • The defender may roll min(armies,2) dice. • Match the highest defender die with attacker die. Compare, ties are in favor of defender. Loser loses 1 army. Remove these dice and repeat with any remaining dice. • You can only attack from a territory with more than 1 army. • If the attacker wins and the target is now empty, they must move at least 1 army into the target, up to N-1.

  7. Project 3 • End Turn: • Player can move armies from one (and only one) territory to another adjacent territory that they control.

  8. Traversals • Traversals are how we extract data from the tree – or any data structure. • They are defined by: • N – When we look at the node • Other Symbol – Determines which node is next • Queue and Stack look at the data before moving to the next data. • NX - Where X is simply the next node

  9. Traversals • For Trees – We have two possible next nodes: Left (L) and Right (R). • The three major orders: • Preorder: NLR • Inorder: LNR • Postorder: LRN • The other 3 exist, but are rarely used

  10. Tree Traversals • Preorder: NLR • Inorder: LNR • Postorder: LRN • You may find it helpful to cross out each thing you do during a traversal. _ _ _

  11. Tree Traversals • Lets look at some examples. • This means we have to create some trees.

  12. Nodes: Data is allocated on each insert and deallocated on each removal Traversals are done using left/right Arrays/Vectors Data allocation only occurs if the insertion is out of range. Deallocation only occurs if sufficient space can be recovered. Traversals are done using index arithmetic Representations

  13. Representations • Nodes have a left and right pointer. • Vector has to solve for left and right. • Starting at location 0: • Left is location*2 + 1 • Right is (location+1)*2 • With vectors it is easier to find the parent node, just reverse the operation. • Drawing time!

  14. Tree ADT • Any node in a Tree is also a Tree. • This also applies to list, any node in a list is also a list. • This is why recursive code works on either of these. • Tree x; • x.insert(15); //calls insert(15,root); • x.insert(30); //calls insert(30,root); • Since root is full, we compare 30 to 15. It is larger, so insert on the right subtree: • insert(30,root->right); 15 30

  15. Private vs Public • Many of our methods are public so that the user can use them. • There are some methods that we don’t want to give the user access to. • insert(T value); vs insert(T value, Node<T> * insertat); • This allows us to create methods for our own use that the user won’t have access to.

  16. Tree Terms • So far, we really only talked about “root” which is equivalent to “first” from our linked lists. • Root also refers to the top-most node in the tree we are looking at. • All nodes are root relative to themselves. • A leaf node is a node who has no children. • Left = Right = NULL; • If a node is not a leaf node, it is a parent node. • A child is a node with a parent. • Level refers to the depth of the node relative to the root we are looking at.

  17. Trees and Graphs • Trees are a subset of Graph. • We can apply searching methods from Graph to Tree: • Breadth-first search: Provides a level-by-level output of the Tree • Depth-first search: If we take the left sub-tree first: we get preorder or inorder traversal. • If we take the right sub-tree first: we get postorder or inorder traversal.

  18. How do we? • Insert: Traverse until you find NULL – going left if we are small, going right if we are larger. At NULL, create a new node, return location to caller – who must now point to the new node. • Remove: Traverse until you find the value you want to remove. • If it is a leaf node, remove it, parent points to NULL. • If it has 1 child, parent points to the child. • If it has 2 children, find smallest value on right sub-tree. Copy that value into the current node. Then, remove(current->data,current->right); • Empty: Starting at root, if NULL return. Otherwise, empty(target->left);, empty(target->right);, delete target;

  19. Problems • First, let us compare a Binary Search Tree with a Binary Tree – we have only really talked about Binary Search Trees so far. • We use Binary Search Trees because of the improved search performance. • However, it can degenerate!

  20. Balancing • Therefore, we need to balance our trees. • Search time is slow when the BST is “skinny”. Therefore, we like our BSTs to be “beefy”.

  21. Self-Balancing Trees • AVL: A self-balancing tree that uses a “balance” factor to determine if an adjustment is needed. • Each node is created with the balance factor set to 0. • Balance is equal to the difference between the height/depth of a node’s sub-trees. • Rebalance occurs whenever one of the 4 cases exists.

  22. AVL Cases • Left-Left: Parent = -2, Left Child – 1 • Rotate Right around Parent • Left-Right: Parent = -2, Left Child = +1 • Rotate left around child, right around Parent • Right-Right: Parent = +2, Right Child = +1 • Rotate Left around Parent • Right-Left: Parent = +2, Right Child = -1 • Rotate right around child, left around Parent.

  23. AVL • This works out rather well. • However, almost half of insertions and removal require rotation.

  24. More trees • Huffman Trees • This is an algorithm for generating codes such as morse code. • For any given set of data and values. • Pair up the two smallest (replace them with a tree containing) • Root = sum of the two values • Left child = one of the data • Right child = the other

  25. Recursion • Activation Stack: • This is a model for evaluating recursion. Let’s take a look at it: • Factorial • Fibonacci • Power

  26. A challenger appears! • Prepare for a quiz!

More Related