Binary Trees Flashcards

1
Q

Binary tree are both

A

1) Recursive

2) Ordered

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

Depth three items

A

1) inoreder
2) perorder
3) Postorder

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

Killer feature

A

Searching

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

Killer feature

A

Searching - 1/2 ‘s the search each pass

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

Binary Tree - How is data stored

A

Nodes (like leafs on a tree)

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

Binary Tree - How is data stored

A

Nodes (like leafs on a tree) with keys

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

Binary tree - Describe a full binary tree

A

Tree with zero or two children (no single children)

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

Binary tree - Describe a balanced binary tree

A

It has the minimum possible height

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

Binary tree - Breadth first

A

Search across the tree first (think the data is somewhere near the top)

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

What type of algorithms use Breadth first

A

shortest pass

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

Binary tree - depth First

A

Think the data is deep deep down the tree

example: maze problems

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

Binary Search Tree (BST) - how are nodes(keys) sorted

A
Nodes keys are sorted top to Bottom
all the nodes left of the parent are smaller then the parent, And all the nodes right to the parent are larger
                      5
                     /  \
                  3       7
                 / \      /  \
               2  4     6 8

Also every level search eliminates half of the data

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

Binary Search Tree (BST) search find 4 (how many hops)

                      5
                     /  \
                  3       7
                 / \      /  \
               2  4     6 8
A
5  4<> ?
                     /  \
   4<> ?      3       7
                 / \      /  \
               2  4     6 8

three hops

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

Binary insert - How to insert a (2)

      5
    /
 3
A

1) start at 5 (it is less than 5 so it goes on the left
2) Is it great then or less then 3 (it is less then so we go down)
3) so Insert it to the empty node below 3

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

Binary insert - How to insert a (4)

      5
    /
 3
A
5
        /  
     3 
   /    \
 2       4     

Four is greater then three so go to right node

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

Binary insert - How to insert a (7)

          5
        /  
     3 
   /    \
 2       4
A

7 is greater then 5 so branch to right

          5
        /   \
     3       7
   /    \
 2       4
17
Q

How to find the minimum in a binary Search Tree

A
easy (just go down the left side to get the minimum key value (smallest numbers are always on the left side)
                      5
                     /  \
                  3       7
                 / \      /  \
               2  4     6 8
              /
            null
18
Q

delete from a binary Search Tree (what are the cases)

A

1) No Child
2) One Child
3) Two children

19
Q

delete from a binary Search Tree - No Child

A

Just find it and set it to null

20
Q

delete from a binary Search Tree - One Child

A

Replace the node that being deleted with the child

21
Q

delete from a binary Search Tree - Two children

A

Most complex - convert tree to one that our node only has one child

1) find minimum on the correct side that will fit into the position
2) replace that node with the new node
3) delete the original new node

22
Q

delete from a binary Search Tree - Two children (delete 5)
5
/ \
2 3

A

1) Go down the right side
2) find minimum value
3) Copy it to the position of the 5
4) remove duplicate

23
Q

Depth first traversal - Inorder

A
Left first, Root, Right)
Start on Left, goto Root, Right
2,1,3
         1
        /   \
     2       3

order = root midle

24
Q

Why use Depth first traversal - Inorder

A

Good if inherent order in tree

example: print out all the elements in the tree

25
Q

Depth first traversal - Preorder

A
Start at top Left and then Right
1,2,3
         1
        /   \
     2       3

Pre = root first

26
Q

Why use Depth first traversal - Preorder

A

Good for Copying and Expression trees

27
Q

Depth first traversal - Postorder

A
Root goes last (bottom up,used in Delete )
Left,Right, Root
2,3,1
         1
        /   \
     2       3

Post = root last

28
Q

Run-time Binary Search Tree B-O number

find, insert, delete

A

O(log n)

Every time the binary tree recuses it halves the search group

1/2 1/4 1/8 1/16 …