260 likes | 298 Vues
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.
E N D
CS 240: Data Structures Monday, July 28th Trees – Traversals, Representations, Recursion and Helpers
Changes • There are some changes to Lab 7 and Project 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.
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)
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
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.
Project 3 • End Turn: • Player can move armies from one (and only one) territory to another adjacent territory that they control.
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
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
Tree Traversals • Preorder: NLR • Inorder: LNR • Postorder: LRN • You may find it helpful to cross out each thing you do during a traversal. _ _ _
Tree Traversals • Lets look at some examples. • This means we have to create some trees.
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
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!
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
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.
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.
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.
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;
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!
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”.
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.
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.
AVL • This works out rather well. • However, almost half of insertions and removal require rotation.
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
Recursion • Activation Stack: • This is a model for evaluating recursion. Let’s take a look at it: • Factorial • Fibonacci • Power
A challenger appears! • Prepare for a quiz!