binary search trees Flashcards

(18 cards)

1
Q

What is a data structure?

A
  • Collection of data items manipulated by an algorithm or program
    • Data item is an instance of a data type
    • In java, data types include:
      ○ Primitive (int/char)
      ○ Built-in (string)
      ○ Compound (class)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an operation?

A
  • Data structures are interfaced though operations
    ○ Basic set: add, search, delete, iterate
    ○ Ordered set: min, max, rank
    ○ Stack: pop/push
    ○ Queue: first/last
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what are some operations that you can do to data?

A

add, search, remove, min/max

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

What is the add, search, remove, min/max of an array?

A

Big O
N N N N

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

What is the add, search, remove, min/max of a sorted array?

A

N
log N
N
1 (constant)

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

What is the add, search, remove, min/max of a linked list?

A

1
N
N
N

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

What is the add, search, remove, min/max of a sorted linked list?

A

N
N
N
1

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

What is a BST?

A

Binary Search Tree
Like sorted arrays, but also fast (logarithmic add delete)
Rooted binary tree satisfies search tree properties

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

What are the search tree properties?

A
  • has single distinguished root node
  • has an orientation downwards/outwards
  • nodes ponted by a node X in outward direction from root are the children of X
  • node pointed by X towards root are the parents
  • each node has AT MOST 2 children (binary)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the height of a BST?

A

(aka depth)
a rooted tree is the length of the longest path from root to leaf
( count down the edges between nodes for each “level” of a bst)

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

What is the height of an empty BST?

A

0

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

What is a complete RBT?

A

all levels of the tree except the last one (they are uniform)

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

What is the minimal height of a RBT?

A

the height of a complete (non-empty) BST

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

How are items sorted in a BST?

A
  • for each node X in tree:
    all data in left subtree are SMALLER values than data item X
    all data in right subtree are GREATER values than data item X
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to search for items in a BST?

A
  1. Traverse key recursively starting at root
  2. Base case:
    • If current node is NULL, return false
    • If k = key at current node, return true
  3. Recursive case:
    • If k < key at current node, traverse left
      • If k > key at current node, traverse right
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to add to bst?

A
  • compare each traversal to the item to be added
  • if less then, go left
  • if more than, go right
  • add to bst in lowest position
17
Q

What is the efficiency of the balanced bst?

A

add = O(log N)
search = O(log N)
min/max = O(log N)
remove = O(log N)

18
Q

How to delete from a bst?

A
  1. locate node you want to delete using search algorithm
  2. if node has no children (leaf) then: delete the node directly from tree
    if node has 1 child:
    - splice out node to remove
    - replace with child node
    if node has 2 children:
    - replace n with its predecessor to preserve search tree property
    -original is deleted from tree and new node points to other remaining child.