Chapter 5: Binary Trees

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Binary search tree(BST) ordering properties

1. left child <= parent 2. right child => parent

What is the maximum number of calls to BSTRemoveNode when removing one of the tree's nodes?

2 maximum number of times - BSTRemoveNode is always called once by BSTRemove. BSTRemoveNode makes at most 1 recursive call in the case of removing an internal node with 2 children. That recursive call is guaranteed not to make additional recursive calls, so the maximum number of times BSTRemoveNode is called is 2.

What is the worst-case (largest) number of comparisons given a BST with N nodes? 2) Perfect BST with N = 31

- 5 - In the worst case, searching a perfect BST requires( height of BST + 1) comparisons so: [log2(31)] + 1 = 5

What is the worst-case (largest) number of comparisons given a BST with N nodes? 1) Perfect BST with N = 7

- [log2N] + 1 - In the worst case, searching a perfect BST requires (height of BST + 1) comparisons so: [log2(7)] + 1 = 3

BSP tree

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.

(T/F) The worst-case time complexity for BSTGetHeight is O(log N), where N is the number of nodes in the tree.

false - BSTGetHeight is called for every node in the tree, so the time complexity is O(N), not O(logN).

The minimum N-node binary tree height is

h = [log2N]

Trees are commonly used to represent

hierarchical data ex: a file system is a hierarchy

node's ancestors

include the node's parent, the parent's parent, etc., up to the tree's root

BST In-order traversal

inorder traversal visits all nodes in a BST from smallest to largest. - Starting from the root, the algorithm recursively prints the left subtree, the current node, and the right subtree

BST insert operation

inserts the new node in a proper location obeying the BST ordering property.

Children of a Binary Tree are called.. ?

left child and a right child

depth

A node's depth is the number of edges on the path from the root to the node. The root node thus has a depth 0.

BST GetHeight algorithm

Given a node representing a BST subtree, the height can be computed as follows: - If the node is null, return -1 - Otherwise recursively compute the left and right child subtree heights, and return 1 plus the greater of the child's subtrees' heights

A binary tree's height can be minimized by keeping all levels full, except possibly the last level. Such an "all-but-last-level-full" binary tree height is ______

H = [log2N]

Search for insert location

If the left(or right) child is not null, the algorithm assigns the current node with that child and continues searching for a proper insert location

Insert as right child

If the new node's key is greater than or equal to the current node, and the current node's right child is null, the algorithm assigns the node's right child with the new node

Insert as left child

If the new node's key is less than the current node, and the current node's left child is null, the algorithm assigns that node's left child with the new node

The maximum N-node binary tree height is

N - 1 (the -1 because the root is at height 0).

BST removal space complexity

O(1)

A perfect binary tree search is O(H), so ___

O(logN)

(T/F) Using a tree data structure to implement a file system requires that each directory node support a variable number of children

True - unlike binary trees that have a fixed number of children per node, aa file system tree must support a variable number of children per directory node.

(T/F) BSTGetHeight returns 0 for a tree with a single node.

True -a tree's height is the maximum edges from the root to any leaf. A tree with 1 node has 0 edges and therefore a height of 0.

(T/F) The base case for BSTGetHeight is when the node argument is null.

True, when the node argument is null, BSTGetHeight returns -1 and does not make any recrusive calls.

BST insert algorithm complexity

a BST with N nodes has at least log2N level and at most N levels. Therefore, the runtime complexity of insertion is best case O(logN) and worst case O(N). The space complexity of insertion is O(1) because only a single pointer is used to traverse the tree to find the insertion location.

parent

a node with a child is said to be that child's parent

Binary space partitioning (BSP)

a technique of repeatedly separating a region of space into 2 parts and cataloging objects contained within the regions.

binary tree

a tree in which each node has at most two children.

leaf

a tree node with no children

A binary tree is perfect if ____

all internal nodes have 2 children and all leaf node are at the same level

A binary tree is complete if ___

all levels except possibly the last are completely full, and the last level has all its nodes to the left side

BST removal best and worst case runtime complexity

best: O(logN) worst: O(N)

A binary tree is full if ____

each node is either a leaf (0 children) or has exactly two child nodes.

A BST with N node has at least ____

log2N level and at most N levels

inserting items in random order naturally keeps a BST's height near the ____.

minimum

internal node

node with at least one child

root

one tree node with no parent( the "top node)

BST search can be implemented using

recursion

Searching a BST starts by visiting the ____

root node

A tree's height is ____

the largest depth of any node. A tree with just one node has height 0.

edge

the link from a node to a child

all nodes with the same depth form a _____

tree leve


Kaugnay na mga set ng pag-aaral

Networking Essentials - Chapter 1 Test

View Set

EVR FINAL REVIEW chp. 19 Air Pollution

View Set

StTS- Ch. 1: Study of Minorities

View Set

16. Digestion and Absorption : MCQs

View Set

Geol 9 Chapter 1: Natural Disasters and the Human Population

View Set