Recording 23: Binary Search Trees

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/5

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

6 Terms

1
New cards

What defines a Binary Tree?

A data structure where each node at most 2 children called left & right child and there’s no order among the node values

2
New cards

What defines a Binary Search Tree?

A special kind of binary tree

  • left subtree node is < root or nodes values

  • right subtree node is > root or nodes values

3
New cards

Given this list of items (6,10,13, 9, 15,18,12) , draw it’s respective Binary Search Tree?

4
New cards

What’s the time complexity for a balanced BST?

O(log2n) —> it’s spread out evenly making it faster as it doesn’t have to go through each branch

5
New cards

What is the time complexity of a unbalanced BST?

O(n) —> you would have to check every single branch in a tree which takes awhile

6
New cards

When provided starter code for a BST, be able to write an additional method of the same difficulty as count() or search().

  • //total number of nodes

private int count(TreeNode<E> currentRoot) {

if (currentRoot == null) {

            return 0;

        }

        return 1 + count(currentRoot.getLeft()) + count(currentRoot.getRight());

    }

public int count() {

return count(root);

}

//return true if item is present

private boolean search(TreeNode<E> currentRoot, E findMe) {

if (currentRoot == null) {

            return false;

        }

        int cmp = findMe.compareTo(currentRoot.getData());

        if (cmp == 0) {

            return true;

        } else if (cmp < 0) {

            return search(currentRoot.getLeft(), findMe);

        } else {

            return search(currentRoot.getRight(), findMe);

        }

}

public boolean search(E findMe) {

return search(root, findMe);

}