dsa bst & avl trees

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

larger example - removing a node with 2 children

- find next largest value, then get min value - call findMind() on right child - remove root and delete moved value

Balanced Trees: AVL Trees

- height of a tree determines performance in recursive operations (find, add, remove) - balanced tree will hold more nodes for a given height Balance factor: a node's BF is the difference between the heights of its left and right subtrees AVL Tree: A binary search tree in which, for every node the balance factor is either 0 or +1 or -1 (height of an empty tree is defined as -1)

Inserting into a BST (L,N, D, F, B, P)

- tree maintains order as values inserted -- node = insert (root, value) return value d.left = insert(root, value) - DOESN"T ALLOW DUPLICATES

BST Delete

. item not present: do nothing - present in lead: remove leaf (prnune - item in non-leaf with one child: replace curr node with that child - item in non- leaf with two children: find smallest in right subtree, recursively remove, use it as parent of the two subtrees) - can be implemented by passing a parameter: bst.remove(value) OR - using an iterator - iterator based on what kind of desired order (post, inorder, pre, level order) -values must remain in order - to remove parent node, must adjust parent node

An AVL tree is a BST that either is empty or in which:

1. heights of left and right tree differ by at most 1 2. left and right subtrees are avl trees

avl insert, delete algorithm

1. perform operation (insert or delete) // may shift heigts by 1 2. traverse towards root, updating the BF/height for each node 3. once imbalace detected (<-1 or >1) use rotation to fix 4. heights for all parent nodes will be adjusted when leaf/lowest children fixed

Three possible cases to deletion

1. value to be deleted is a leaf node: just adjust parent link - tree unaffected 2. value only has one child : child takes parent place - point to it, basically deleted - swap nodes, and prune last one 3. (hard) value to be deleted has two children: swap - must be fixed can be - next largest - next smallest - nearest value (inorder traversal (predecessor/successor) PICK EITHER OR BUT BE CONSISTENT

Binary Search Tree

A data structure very similar to a tree with the following additional restrictions. Each node can have only 0, 1 or 2 leaf nodes. All left nodes and all of its descendants have smaller values that the root node, while all right nodes and all of its descendants have larger values than the root node.

Balanced Trees: AVL Trees

Balance factor: a node's BF is the difference between the heights of its left and right subtrees - extra work when adding and removing - find works the same bc everything in L subtree less than the root, everything in R subtree is greater than root Adding is O(log n) + O(1), so doesn't really affect performance

T/F primitives implement compareTo

False

What happens when it's null?

Item is not in tree

performance of a balanced tree is

O(log n)

if items are added in order, the tree forms like a list and find/add is O of what

O(n)

T/F if BST is full or complete, it's height is LOGARITHMIC in n

True

T/F items not in the subtree of the item rotated about are unaffected by the rotation

True

T/F the more links you have to do down, the further you have to do something

True ( insert, find, delete)

T/F balanced tree with a height of 20 nodes can hold 1,048, 575 nodes)

True, a million values with just 20 tries

Will inserted item always be a leaf?

Yes

Are AVL Trees BSTs?

Yes, very special - maintain a balanced trees (Balance Factor)

Height of a BST can be ...

anything

rotation takes _________ time

constant

Overview of Binary Search Tree

def: T is a binary search tree if either is true - T is empty or Root has two subtrees: - each is BST - value in root > all values of the L subtree - value in root < all values in the right subtree

how to find min and max values of a tree?

go all the way left (min) go all the way right (max)

Way to tell it's a BST from given tree:

if do inorder traversal and values come out in order, its a BST

if target is less than root data, recursively call _____ ____. if target is more than the root data, recursively call ____ ____.

left root, right root,

Claim: the height of any AVL tree is __________ in n

logarithmic

For inserting an item, what's the worst case? (worst height) What is the average height?

n log n

Comparable Interface

public interface Comparable <T> - defines natural order of type - one method required: public int compareTo (T o ) - compare to returns: 1, 0, -1

Fixing the Out of balance State: If LL go ____ If RR go _____ Double Rotation: right-left heavy: go ____ left-right heavy: go ______

right, left right about the child, left about the parent, left about the child, and right about the parent

balance is maintained thru:

rotations: -adustment to tree: resigning pointers hence why it's O(1)

T/F operations done to a BST are proportional to height of tree

true

T/F Balance factor is the absolute value of the height of the L subtree - height of the R subtree

true must be <= 1

T/F AVL Trees are Self-Balancing

true left and right subtrees CANNOT have a height difference > 1

T/F insertion in an AVL is like in a BST

yes, just remember BF


Kaugnay na mga set ng pag-aaral

A&P Ch 19.1-19.3 - Blood functions, composition & Plasma

View Set

Course 5: Agile Project Management

View Set

Essentials of Physical Anthropology: Chapter 8, ANTH 1101: Final Exam, Anthropology 101 Exam 1, Anthropology 101 Exam 2, Anthropology 101 Final, Anthropology 101- Exam 3, Anthropology 101, Anthropology 101, Anthropology 101, Anthropology 101, ANTHROP...

View Set