ICS46 - Binary Search Tree

studied byStudied by 0 people
0.0(0)
learn
LearnA personalized and smart learning plan
exam
Practice TestTake a test on your terms and definitions
spaced repetition
Spaced RepetitionScientifically backed study method
heart puzzle
Matching GameHow quick can you match all your cards?
flashcards
FlashcardsStudy terms and definitions

1 / 18

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

19 Terms

1

Binary Search Tree: Recursive Insert_Node

static TreeNode insert(KeyType key, ValueType value, TreeNode t);


Node * BSTree::insert_node(Node * t, string key){
if(t == nullptr){
return new Node(key);}

if (key < t->key){
t->left = insert_node(t->left, key);
}
else if (key > t->key){
t->right = insert_node(t->right, key);
}
return t;
}
  • We will add to the tree depending on the value of the node is greater than or less than the parent node

    • Uses recursion

    • Inserts checks if nullptr

New cards
2

Binary Search Tree: Inserting in a Sorted order

O(N) (Would become a linked list)

New cards
3

Binary Search Tree: Inserting in a random order

O(Log n)

New cards
4

Binary Search Tree: Iterative Find_Node

Node * BSTree::find_node(Node * t, string key)

Node * BSTree::find_node(Node * t, string key){
if(t == nullptr){
return nullptr;}

Node * current = t;
while(current != nullptr){
if(current->key == key){
return current;
}
if(key < current->key){
current = current->left;}
else if(key > current->key){
current = current->right;}
}
return nullptr;
}

New cards
5

Binary Search Tree: Find() in a Sorted order

O(N)

New cards
6

Binary Search Tree: Find() in a Random order

O(Log n)

New cards
7

Binary Search Tree: Recursive Remove_Node

Node * BSTree::delete_node(Node * t, string key)

Node * BSTree::delete_node(Node * t, string key){
if(t == nullptr){return t;}

if (key < t->key)
t->left = delete_node(t->left, key);
else if (key > t->key){
t->right = delete_node(t->right, key);
}
// key == t->key

else {
// Case 1: Node has one or no child
if (t->left == nullptr || t->right == nullptr) {
Node * child = t->left ? t->left : t->right;
// NO CHILD CASE
if (child == nullptr) {
child = t;
t = nullptr;
} else { // ONE CHILD CASE
*t = *child; // COPY DATA FROM CHILD
}
delete child;
}
// Case 2: Node has two children
else {
// find min of right-subtree
Node * succ = left_most(t->right);
// swap the succ and the one you are deleting
swap(t->key, succ->key);
// Deltete node
t->right = delete_node(t->right, key);
}
}
return t;
}

3 Cases:

  • We are removing a leaf node → Remove a single node

  • We are removing a node w/ a single child → Replace with child node

  • We are removing a node w/ two children → swap with minimum of the right subtree and delete

New cards
8

Binary Search Tree: Remove() in a Random order

O(Log n)

New cards
9

Binary Search Tree: Remove() in a Sorted order

O(1)

New cards
10

Pre-Order Traversal

void BST::pre_order_print(ostream & out, Node * t){
if(t == nullptr){
return;
}
out << t-> key << " ";
pre_order_print(out, t->left);
pre_order_print(out, t->right);
}

Root → Left → Right

New cards
11

In-Order Traversal

void BST::in_order_print(ostream & out, Node * t){
if(t == nullptr){
return;
}
in_order_print(out, t->left);
out << t-> key << " ";
in_order_print(out, t->right);
}

Left → Root → Right

New cards
12

Post Order Traversal

void BST::post_order_print(ostream & out, Node * t){
if(t == nullptr){
return;
}
post_order_print(out, t->left);
post_order_print(out, t->right);
out << t-> key << " ";
}

Left → Right → Root

New cards
13

AVL Tree: Insert()

Node * AVLTree::insert_node(Node * t, string key)

Node * AVLTree::insert_node(Node * t, string key){
if(t == nullptr){
return new Node(key);}
if (key < t->key){
t->left = insert_node(t->left, key);
}
else if (key > t->key){
t->right = insert_node(t->right, key);
}
set_height(t);
return rebalance(t);
}
  • Same as BST, we just need to call rebalance

New cards
14

When do we use recursion for bst?

New cards
15

AVL Trree: Inserting in a Sorted order

O(Log N)

New cards
16

AVL Trree: Inserting in a Random order

O(Log N)

New cards
17

AVL Tree: Rebalance()

Node * AVLTree::rebalance(Node * t){
set_height(t);
int balance = get_balance(t);
if (balance > 1) {
if (get_balance(t->left) < 0){
t->left = left_rotate(t->left);
}
return right_rotate(t);
} else if (balance < -1) {
if (get_balance(t->right ) > 0){
t->right = right_rotate(t->right);
}
return left_rotate(t);
}
return t;
}
  • We check the value of get_balance to see if our graph is skewed to the left or right

    • Then we perform a left or right rotate accordingly

New cards
18

Left Rotate

Node * AVLTree::left_rotate(Node * x){
Node * y = x->right;
Node * T2 = y->left;
y->left = x;
x->right = T2;
set_height(x);
set_height(y);
return y;
}

New cards
19

Right Rotate

Node * AVLTree::right_rotate(Node * y){
Node * x = y->left;
Node * T2 = x->right;
x->right = y;
y->left = T2;
set_height(y);
set_height(x);
return x;

}

New cards
robot