Chapter 12: Binary trees and hash tables

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

Function overloading is used when different functions have different implementations

False. It is used when functions with different signatures have the same implementation.

Unlike Vectors, attempting to access an array element with an out-of-bounds index will not stop the execution of the program

False. Vectors do not check the index to determine whether it is in bounds or not. However, neither do arrays.

What is the binary search tree property?

For each node x, values in left subtree of x < value in x < values in right subtree of x

inorder pred

rightmost child of the left child of the node you want to delete

inorder predecessor in deletion

rightmost child of the left child of the node you want to delete

access point

root

root

the node that has no incoming arcs, but that every other node in the tree can be reached from by following a unique path

height

the number of levels in a tree

binary tree

trees in which each node has at most 2 children and thus has at most 2 nonempty subtrees

*this is the current object

true

this is a pointer to the current object

true

References cannot be passed by pointers

F

worst case computing time for linear search

O(n)

hash table pros/cons

1. pro: very fast 2. con: wastes 97% of available space

worst case computing time for linked list binary search

O(n)

Bst search runtime

O(log2n)

worst case computing time for binary search

O(log2n)

inorder successor in deletion

leftmost child of the right child of the node you want to delete

linear probing

linear search of table begins at point of collision and continues until an empty slot is found in which the item can be stored.

delete leaf

make parent pointer point to null

delete node w/1 child

make parent to the node you want to delete point to the child

Is it practical to expect a hash table to prevent collisions completely?

no. Instead, must be satisfied where reasonably few collisions happen. Empirical studies suggest using tables whose capacities are 1.5-2 times the number of items that must be stored.

children

nodes that are directly accessible from a given node

descendants

nodes that can be reached by following a path from another node, which is called an ancestor

How does deleting a leaf, a node with 1 child, and a node with 2 children differ while performing delete operation on BST?

1 child: before deleting, make parent of node point to node's child. Then delete node. leaf: make parent point to null, then delete node 2 children: find inorder successor, swap node's value with inorder successor's value. Start search at the child of the node you want to delete, then delete the inorder successor.

algorithm for LVR binary tree traversal

1. Anchor: If binary tree is empty, do nothing 2. Else: a. L: Traverse left subtree b. V: visit the root c. R: traverse the right subtree

Recursive definition of a binary tree

1. Anchor: binary tree is empty or 2. Inductive step: consists of a node called a root which has pointers to two disjoint binary subtrees called left subtree and right subtree

Binary Search Tree as an ADT

1. Collection of data elements. For each node x, values in left subtree of x < value in x < value in right subtree of x Operations: 1. Construct an empty BST 2. Determine if BST is empty 3. Search BST for a given item 4. Insert a new item in the BST and maintain the BST property 5. Delete an item from the BST and maintain the BST property 6. traverse the BST visiting each node exactly once. At least one of the traversals(an inorder traversal) must visit the values in the nodes in ascending order

simple recursion to traverse binary tree

1. V: Visit a node 2. L: traverse the left subtree of a node 3. R: traverse the right subtree of a node

observations about inserting into BST:

1. can add at a node that is a leaf or has 1 child

Linked list representation of binary trees

1. each node has 2 links, one pointing to the left child of that node and one pointing to the right child of that node 2. pointers left and right in a node are pointers to nodes representing left and right children 3. leaf node characterized by having null values for both left and right

delete node w/2 children

1. find inorder successor 2. swap data 3. start search at child of the node you want to delete 4. delete inorder successor

array representation of binary trees

1. number nodes in tree from the root down, numbering the nodes on each level from left to right. 2. store contents of the ith node in the ith location of the array works for complete trees, where each level of tree is completely filled, except for bottom level, but the nodes are in leftmost positions

stack

LIFO

Postorder traversal

LRV

Inorder traversal

LVR

hash search time

O(1)

When the only difference in the implementation of two or more functions is the data used in the functions, the functions may possibly be converted into template functions

True,

Data is passed to templates using parameters

True. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function

Preorder traversal

VLR

tree

consists of a finite set of elements called nodes or vertices and a finite set of directed arcs that connect pairs of nodes.

hash function

a mapping

What is binary tree property?

a tree in which no node has more than 2 children

binary tree

at most 2 children

Why shouldn't we use binary search with linked lists?

binary search requires a direct-access storage structure to be efficient. locating the middle element of a linked list requires traversing sublist of elements that precede it (b/c linked lists can only be accessed sequentially). To use linked list implementation, add code that calculates loc, a loop that moves pointer locPtr from the first element of the sublist to the middle element.

balanced trees

binary trees w/property that for each node, height of its left subtree and height of its right subtree differ by at most one

call to C++'s binary search algorith in the STL <algorithm> library

binary_search(begin, end, item)

Deleting node from BST

cases 1. deleting node w/no children (a leaf) 2. deleting node w/1 child 3. deleting node w/2 children -inorder succ and pred

hash table

ddatabase

What is binary search

examines middle element of sublist and if it is not desired item, search one of the two halves of the sublist in the same way.

The arity of an operator can change

false

subtree

for each node in a tree, subtree of that node is formed by selecting one of its children and all descendants of that child

h(i) = i mod 31

int h(int i) { return i % 31; } hash function to store 31 values in 31 locations. EX: 64 stored in table[2] since h(64) = 64%31 = 2.

how does a hash function work?

integer array where each element is initialized with a dummy value. If we use eachinteger i in the set as an index, that is if we store i in table[i], then to determine whether a particular integer number has been stored, we need only check if table[number] is equal to number. The function h is defined by h(i) = i determines the location of an item i in the hash table.

leaves

vertices with no outgoing arcs. At bottom of tree

collision

when 2 different values are stored in the same location. EX: for h(i) = i mod 31, all integers of the form 31k + 2 will hash to location 2 and collide.


Ensembles d'études connexes

Microbiology ch 22 Skin Infections

View Set

The Endomembrane System and Membrane Trafficking

View Set