ch 4 trees Flashcards

1
Q

depth

A

path from the root to n (root is at 0)

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

height

A

leaf n to root (leaf is at 0)

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

class Binarynode (what are the instance variables)

A
object element (the data in the node)
binarynode left (left child)
binarynode right (right child)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how do we evaluate expression trees

A

push the values onto a stack until you come across an operand
then calculate the last two values, push it back onto the stack

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

.contains

A
returns a boolean, is a recursive method
boolean contains(Anytype x, BinaryNode t)
if null, return false

int compareResult = x.compareTo(t.element)

if compareresult > 0
       return cotains (x, t.left);
if compareres8ult < 0
       return contains(x, t.right);
else
       return true //match
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

findMin

A

recursive, just the very left most node

findMin(binarynode t)
if t == null
return null

else if t.left == null
return t.left

return findmin(t.left)

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

insert

A
binarynode insert (anytype x, binarynode t)
if t == null
    return new binarynode (x, null, null)

int compareresult = x.compareto(t.element)

if compareresult < 0
    t.left = insert(x, t.left)
if compareresult > 0
    t.right = insert(x, t.right)
else
    do nothing // this means that the number already exists in the binary tree

return t;

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

remove

A

if it has one child, just change the pointer from the target node to the child

if it has two children, the new parent is the smallest node of the right subtree
if(node.left !=null&& node.right !=null) {// two
childrennode.data = findMin(node.right).data;node.right =
remove(node.right, node.data);

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

AVL Tree

A

tree with a balance condition

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

single rotation

A

middle node becomes the parent, other two become the left or right children

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

double rotation

A

zig zag zig etc

first swap the bottom two nodes so it turns into a scenario where it becomes a single rotation

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

tree traversals

A

inorder, preorder, postorder
running time O(N)

printTree(binaryNode t)//inorder traversal
if t != null
    printTree(t.left)
    system.out.println(t.element);
    printtree(t.right)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly