Chapter 10

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Assume a trie is built by executing the following code. trieRoot = new TrieNode() TrieInsert(trieRoot, "cat") TrieInsert(trieRoot, "cow") TrieInsert(trieRoot, "crow") 1) When inserting "cat", _____ new nodes are created. 1 3 4 2) When inserting "crow", _____ new nodes are created. 1 4 5 3) If TrieInsert(trieRoot, "cow") is called a second time, _____ new nodes are created. 0 1 4

1) 4 One node for each character in "cat", plus the terminal node, makes 4 new nodes. 2) 4 The node for 'c' already exists but does not have a child for 'r'. Four nodes are created: nodes for 'r', 'o', 'w', and the terminal node. 3) 0 Adding "cow" a second time causes the code to traverse down the tree, but no new nodes are created.

Given the Tree in Figure 10.6.2 above, in what order would the nodes be presented for the traversals below 1) Preorder traversal L G C F J K A I H E B D A B D E H I C F J K G L A C F G J K L B D E H I 2) Inorder traversal A D B E H I J K L C F G B A C D E H I J F K G L D B H E I A J F K C L G 3) Postorder traversal D H I E B J K F L G C A H I E D B L K J F G A C D B E H I J K F C L G A 4) Level-order traversal L K J I H G F E D C B A A C B G F E D L K J I H A B C D E F G H I J K L

1) A B D E H I C F J K G L The order definition is Node, Left, Right. Good job 2) D B H E I A J F K C L G The order definition is Left, Node, Right. Good job 3) D H I E B J K F L G C A The order definition is Left, Right, Node. Good job 4) A B C D E F G H I J K L The order definition is visit each node at each level of the tree from top (root) to bottom and left to right

1) A general tree can only have one child per node True False 2) A binary tree has at most two children per node True False 3) A balanced tree must have leaves within a level of each other True False 4) An n-ary tree can have more than n children per node True False

1) False A general tree has no limit to the number of child nodes 2) True A binary tree does have at most two children per node 3) True A balanced tree's leaves must be within a level of each other. 4) False An n-ary tree has a limit of no more than n children per node

1) it is very easy to abstract an interface for trees True False 2) The operations to add and remove nodes from a tree are not known until the purpose and organization of the tree is known. True False

1) False Because there are several type of tress, it is not easy to abstract an interface for trees. 2) True If the purpose and organization aren't known, adding or removing a node from a tree could cause errors and loss of data.

1) When traversing down a BSP tree, half the objects are eliminated each level. True False 2) A BSP implementation could choose to split regions in arbitrary locations, instead of right down the middle. True False 3) In the animation, if the parts of the screen were in 2 different regions, then all objects from the 2 regions would have to be analyzed when rendering. True False 4) BSP can be used in 3-D graphics as well as 2-D. True False

1) False The partitioning of space doesn't necessarily put half the objects in each half region. Ex: In the animation, C and D are the two halves split from the world's right half, but C contains 5 of the 7 objects and D the other 2. 2) True In practice, most BSP implementations partition according to how objects exist in the world. Regions are not always split down the middle, nor are the splits always horizontal or vertical. 3) True The screen isn't guaranteed to be contained within a single BSP region, so analyzing objects from multiple regions may be required. 4) True When used for 3-D rendering, each node still represents a region of space and the objects contained within. 3-D regions can be split in 2 using a plane. One child represents content on one side of the plane and the other child represents content on the other side.

1) The root is the top node of a tree True False 2) The height of a tree is the largest number of nodes from left to right True False 3) A parent node can only have one child True False 4) A child node can only have one parent True False 5) Nodes that have the same parent are known as cousins True False

1) The root is the top node of a tree and does not have a parent. 2) False The height of a tree is the length of the longest path. 3) False A parent node can have multiple children. 4) True A child node can only have one parent. 5) False Nodes that have the same parent are siblings

1) A file in a file system tree is always a leaf node. True False 2) A directory in a file system tree is always an internal node. True False 3) Using a tree data structure to implement a file system requires that each directory node support a variable number of children. True False

1) True A file cannot contain additional files, and therefore is always a leaf node in a file system tree. 2) False A directory containing 1 or more files or directories is an internal node, but an empty directory is a leaf node. 3) True Unlike binary trees that have a fixed number of children per node, a file system tree must support a variable number of children per directory node.

1) Because trees are non-linear, a linked list is a valid and obvious choice for trees. True False 2) The are tree implementations using an array to store the nodes. True False 3) The computational strategy for array implementation doesn't have an issue with wasted memory space True False 4) The simulated link strategy for array implementation has no drawback. True False

1) True Linked lists work well with tree implementations 2) True There are array implementations to store trees 3) False Wasted memory space is a drawback of the computational strategy. 4) False The simulated link strategy does have a drawback of increased overhead for deleting elements in a tree

10.7.1: A file system is a hierarchy that can be represented by a tree.

1. A tree representing a file system has the filesystem's root directory ("/"), represented by the root node. 2. The root contains 2 configuration text files and 2 additional directories: user1 and user2. 3. Directories contain additional entries. Only empty directories will be leaf nodes. All files are leaf nodes.

BSP tree

A BSP tree is a binary tree used to store information for binary space partitioning. Each node in a BSP tree contains information about a region of space and which objects are contained in the region. In graphics applications, a BSP tree can be used to store all objects in a multidimensional world. The BSP tree can then be used to efficiently determine which objects must be rendered on screen. The viewer's position in space is used to perform a lookup within the BSP tree. The lookup quickly eliminates a large number of objects that are not visible and therefore should not be rendered. A BSP tree is used to quickly determine which objects do not need to be rendered.

Depth, level, and height

A few additional terms: The link from a node to a child is called an edge. A node's depth is the number of edges on the path from the root to the node. The root node thus has depth 0. All nodes with the same depth form a tree level. A tree's height is the largest depth of any node. A tree with just one node has height 0.

internal node

A node that that is not the root and has at least one child

Leaf

A node the doesn't have any children

Balance vs Unbalanced tree

Another way to classify trees is if the tree is balanced or not. Depending on the algorithm being used, there are many definitions of a balanced tree. Typically, a balanced tree has some restriction on structure so the leaves are at "approximately" the same level (+ or - one level). A balanced tree can go from degenerate to fully balanced. A balanced n-ary tree with m elements will have a height of lognm. A balanced binary tree with n nodes has height log2n.

Binary space partitioning

Binary space partitioning (BSP) is a technique of repeatedly separating a region of space into 2 parts and cataloging objects contained within the regions.

Nodes

Conceptually, a tree is composed of a set of nodes in which elements are stored and edges that connect one node to another. Each node is at a particular level in the tree hierarchy. In the world trees have roots at the base, a trunk and limbs that grow upward and out to the leafs. For us however, a visual representation of a tree will be upside down.

Special types of binary trees

Certain binary tree structures can affect the speed of operations on the tree. The following describe special types of binary trees: A binary tree is full if every node contains 0 or 2 children. A binary tree is complete if all levels, except possibly the last level, contain all possible nodes and all nodes in the last level are as far left as possible. A binary tree is perfect, if all internal nodes have 2 children and all leaf nodes are at the same level.

Classifying Trees

Classifying trees is based on the maximum number of children any node in the tree may have. The maximum number of children is the order of the tree. 1) A tree with no limit to the number of children is known as a general tree 2) A tree with a limit of no more than n children per node is known as an n-ary tree 3) A tree with at most two children per node is known as a binary tree

four classic traversal methods

Each one starts at the root. Think of each node as the root of its own subtree. -Preorder: NLR - Visit node, traverse left subtree, traverse right subtree -Inorder: LNR - traverse left subtree, visit node, traverse right subtree -Postorder: LRN - traverse subtrees L to R, then visit node -Level-order: Visit each node at each level of the tree from top (root) to bottom and left to right The implementation of the Preorder, Inorder and Postorder traversals is recursive. Level order is a bit more complicated and requires additional data structures to implement.

Trie insert algorithm

Given a string, a trie insert operation creates a path from the root to a terminal node that visits all the string's characters in sequence. A current node pointer initially points to the root. A loop then iterates through the string's characters. For each character C: A new child node is added only if the current node does not have a child for C. The current node pointer is assigned with the current node's child for C. After all characters are processed, a terminal node is added and returned.

Trie remove algorithm

Given a string, a trie remove operation removes the string's corresponding terminal node and all non-root ancestors with 0 children.

Trie search algorithm

Given a string, a trie search operation returns the terminal node corresponding to that string, or null if the string is not in the trie.

Inorder traversal

Given the definition LNR, traverse the left subtree, visit the node, traverse the right subtree, an inorder traversal would produce the sequence: F D B E A C Starting with the root node, we traverse to the left node (B), but applying the algorithm on B moves us to the left node of it (D) and applying the algorithm again on D, we move to the left node of D which is F. At this point, based on the algorithm, we have yet to "visit" a node. We have simply traversed left each time. We attempt to perform the algorithm on F, but F doesn't have a left child, so we finally visit F. F doesn't have a right child, so we return to D. Now we visit D and since D doesn't have a right child, we return to B. We now visit B, and traverse to B's right child E. E doesn't have a left child, so E is visited and E doesn't have a right child, so we return to A. A is now visited and we traverse to A's right child C. C doesn't have a left child, so C is visited. C doesn't have a right child, so the algorithm ends.

Postorder traversal

Given the definition LRN, traverse the left subtree, traverse the right subtree, visit the node, a postorder traversal would produce the sequence: F D E B C A Just like Inorder, starting with the root node, we traverse to the left node (B), but applying the algorithm on B moves us to the left node of it (D) and applying the algorithm again on D, we move to the left node of D which is F. And like inorder, we haven't "visited" a node yet. once we attempt to apply the algorithm to the node F, it doesn't have a left or right child, so we visit the node F. That instance ends and we return to D. D doesn't have a right child, so we visit D. That instance ends and we return to B. B does have a right child, so we traverse to E. E doesn't have a left or right child, so we visit E and return to B. We now visit B and return to A. A has a right child, so we traverse to C. C doesn't have a left or right child, so we visit C and return to A. We now visit A and the algorithm ends

Preorder traversal

Given the definition NLR, visit the node, traverse the left subtree, traverse the right subtree, a preorder traversal would produce the sequence: A B D F E C. As stated earlier, you start a traversal at the root node A, you would then traverse to the left subtree B (the first child node), then visit its children. We repeat the algorithm and next we traverse to the first child of B, which is D. Repeating the algorithm again this time on the subtree of D, we visit the only child of D, which is F. F doesn't have any children, so that instance of the algorithm ends. D doesn't have another child, so that instance ends. We then continue with B's instance of the algorithm and visit B's right child (subtree) E. E doesn't have a child, so that instance ends. Both children of B have been traversed, so B's instance ends, and we continue with A's instance visiting A's right child (subtree) C. C doesn't have any children, so the algorithm ends.

Level-order traversal

Given the definition, visit each node at each level of the tree from the top (root node) to the bottom and left to right, a level-order traversal would produce the sequence: A B C D E F One implementation of a level-order traversal use a Queue and an unorder list, but there are others.

Trie time complexities

Implementations commonly use a lookup table for a trie node's children, allowing retrieval of a child node from a character in O(1) time. Therefore, to insert, remove, or search for a string of length M in a trie takes O(M) time. The trie's current size does not affect each operation's time complexity.

Binary tree basics:

In a list, each node has up to one successor. In a binary tree, each node has up to two children, known as a left child and a right child. "Binary" means two, referring to the two children. Some more definitions related to a binary tree: Leaf: A tree node with no children. Internal node: A node with at least one child. Parent: A node with a child is said to be that child's parent. A node's ancestors include the node's parent, the parent's parent, etc., up to the tree's root. Root: The one tree node with no parent (the "top" node).

Siblings

Nodes that have the same parent

Strategies for implementing a tree

One of the most obvious implementations of a tree would be a linked list structure, where each node could be defined as a TreeNode class object. The class could be defined so that each node has a reference (generic object) for the element to be stored as well as a pointer (reference) to each possible child of the node. Depending on the implementation, you could also include a pointer (reference) to a possible parent node (the root doesn't have a parent and would be null). This implementation would be more like a double linked list, where each node points to the next nodes as well as the previous node. Because of a trees non-linear structure, you may be hesitant to think about implementing a tree using an array. However, using an array can be useful.

Tree Traversal

The concept of traversing a linear structure was relatively "obvious", start at the beginning and go step by step (cell by cell) until you reach the end (or in reverse). Traversing a tree, however, is generally more interesting than a linear structure, as a tree is a hierarchy.

File systems

Trees are commonly used to represent hierarchical data. A tree can represent files and directories in a file system, since a file system is a hierarchy.

Children

The nodes at lower levels of the tree are the children of nodes at the previous level. A node can only have one parent, but a node may have multiple children

Root

The root of the tree is the only node at the top level of the tree (level 0). The root is the "entry point" into the tree and you would follow a path (an edge) from the root to get to another node. There will only be one root and it is the only node of the tree that doesn't have a parent.

Tree

Up to now, the data structures (collections) that have been discussed are all linear, which means that their elements are arranged in order one after another. A Tree is a nonlinear structure in which elements are organized into a hierarchy.

terminal node

a node that represents a terminating character, which is the end of a string in the trie.

Simulated Link Strategy for array implementation of trees

another possible array implementation is modeled after the way operating systems manage memory. Instead of assigning elements of a tree to array positions (indexes) by location in the tree, array positions (indexes) are allocated contiguously on a first come first placed basis. Each element of the array would be a node similar to the TreeNode class mentioned earlier, but instead of storing object reference as pointers to its children (and possibly parent), each node would store the array index (position) in the array of the children (and possibly parent). This strategy allows elements to be stored in contiguous order in the array without wasting memory space. Elements C, E, and F aren't empty, they store an element. The pointers are null because the elements do not have children. Although this approach does have a drawback as it increases the overhead for deleting elements in the tree, because it requires the shifting of the remaining elements or maintaining a freelist (A list of available positions in an array implementation of a tree).

Computational strategy for array implementation of trees

for some type of trees, in particular binary trees, a computational approach can be taken to store a tree using an array. For an element stored in position (index) n of the array, the element's left child will be store in position (index) (2 * n + 1) and it's right child will be stored in position (index) (2 * (n + 1)).

Basic pseudo code for Preorder (the others are similar):

if not a base case (depends on implementation) Visit current node Preorder(left child of current node) Preorder(right child of current node) endIf Keep in mind, that for the algorithms, a "visit" is when the value of the node is determined or accessed. A traversal is a new application of the algorithm (a recursive call).

A trie (or prefix tree)

is a tree representing a set of strings. Each node represents a single character and has at most one child per distinct alphabet character.


Ensembles d'études connexes

Ch. 14 Workers Compensation Insurance

View Set

Chapter 12: The Postpartum Woman

View Set

4203 xam 2 all review/eaq questions

View Set

LearningCurve 13b. Social Influence

View Set