trees Flashcards

1
Q

A tree with 𝑁 nodes always has ___ edges

A

A tree with n nodes has n -1 edges

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

searching in binary search tree pseudocode

A

if the tree is empty
return NULL
else if the item in the node equals the target
return the node value
else if the item in the node is greater than the target
return the result of searching the left subtree
else if the item in the node is smaller than the target
return the result of searching the right subtree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

removal in binary search tree pseudocode

A

if the tree is empty return false

Attempt to locate the node containing the target using the binary search
if the target is not found return false
else the target is found, so remove its node:

Case 1: if the node has 2 empty subtrees
- replace the link in the parent with null

Case 2: if the node has a left and a right subtree
- replace the node’s value with the max value in the
left subtree
- delete the max node in the left subtree

Case 3: if the node has no left child
- link the parent of the node to the right (non-empty) subtree

Case 4: if the node has no right child
- link the parent of the target to the left (non-empty) subtree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

time complexity of BST operations

A

The complexity of operations search, insert and remove in BST is 𝑂(β„Ž) , where β„Ž is the height.

𝑂(log⁑𝑛) when the tree is balanced, i.e., β„Ž=𝑂(log⁑𝑛). The updating operations may cause the tree to become unbalanced.

The tree can degenerate to a linear shape and the operations will become 𝑂(𝑛)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is the AVL property

A

An AVL tree is a binary search tree such that (AVL property):
The height of the left and right sub-trees of the root differ by at most 1
The left and right sub-trees are AVL trees
treat nil tree as height -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

AVL insertion

A
  1. insert as in simple BST
  2. work your way up tree from the node inserted, restoring AVL property (and updating heights as you go).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

AVL tree max number of rotations that may need to be done after inserting node

A

In general, process may need several rotations before done with an Insert. 𝑂(log⁑𝑛) rotations may be required

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

AVL delete

A
  1. Perform standard BST delete
  2. Work your way up tree from the node deleted, restoring AVL property (and updating heights as you go).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

AVL tree advantages and disadvantages

A

Advantages
𝑂(log⁑𝑛) worst-case searches, insertions and deletions

Disadvantages
Complicated Implementation
Must keep balancing info in each node
To find node to balance, must go back up in the tree: easy if pointer to parent, otherwise difficult
Deletion complicated by numerous potential rotations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly