Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240: Data Structures Thursday, August 2nd Trees – Traversals, Representations, Recursion and Helpers.

Similar presentations


Presentation on theme: "CS 240: Data Structures Thursday, August 2nd Trees – Traversals, Representations, Recursion and Helpers."— Presentation transcript:

1 CS 240: Data Structures Thursday, August 2nd Trees – Traversals, Representations, Recursion and Helpers

2 Submissions If you need a Project 1 grade, you need to schedule an appointment with me after class. If you need a Project 1 grade, you need to schedule an appointment with me after class.

3 Traversals Traversals are how we extract data from the tree – or any data structure. Traversals are how we extract data from the tree – or any data structure. Queue and Stack look at the data before moving to the next data. Queue and Stack look at the data before moving to the next data. NLR-However, there is no R in this case. NLR-However, there is no R in this case. The three major orders: The three major orders: Preorder:NLR Preorder:NLR Inorder:LNR Inorder:LNR Postorder:LRN Postorder:LRN

4 Tree Traversals Preorder:NLR Preorder:NLR Inorder:LNR Inorder:LNR Postorder:LRN Postorder:LRN N means Node. More specifically, look at the node. N means Node. More specifically, look at the node. L means Left and R means Right. L means Left and R means Right. This is the order we examine data. This is the order we examine data. You may find it helpful to cross out each thing you do during a traversal. You may find it helpful to cross out each thing you do during a traversal. _ _ _

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

6 Representations Nodes: Nodes: Data is allocated on each insert and deallocated on each removal Data is allocated on each insert and deallocated on each removal Traversals are done using left/right Traversals are done using left/right Arrays/Vectors Arrays/Vectors Data allocation only occurs if the insertion is out of range. Deallocation only occurs if sufficient space can be recovered. 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 Traversals are done using index arithmetic

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

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

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

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

11 Trees and Graphs Trees are a subset of Graph. Trees are a subset of Graph. We can apply searching methods from Graph to Tree: We can apply searching methods from Graph to Tree: Breadth-first search: Provides a level-by-level output of the 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. 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. If we take the right sub-tree first: we get postorder or inorder traversal.

12 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. 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. 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 is a leaf node, remove it, parent points to NULL. If it has 1 child, parent points to the child. 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); 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; Empty: Starting at root, if NULL return. Otherwise, empty(target->left);, empty(target->right);, delete target;

13 Problems First, let us compare a Binary Search Tree with a Binary Tree – we have only really talked about Binary Search Trees so far. 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. We use Binary Search Trees because of the improved search performance. However, it can degenerate! However, it can degenerate!

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

15 Self-Balancing Trees AVL: A self-balancing tree that uses a “balance” factor to determine if an adjustment is needed. 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. 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. 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. Rebalance occurs whenever one of the 4 cases exists.

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

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

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

19 Various Tidbits Activation Stack Activation Stack Recursion and evaluation Recursion and evaluation Array vs pointer indexing Array vs pointer indexing Use of “this” Use of “this” Proving algorithms Proving algorithms Shallow vs Deep copy Shallow vs Deep copy


Download ppt "CS 240: Data Structures Thursday, August 2nd Trees – Traversals, Representations, Recursion and Helpers."

Similar presentations


Ads by Google