Binary Trees Flashcards
give the Tree ADT
e = element, v = node
root() insert(e) remove(v) find(e) parent(v) children(v) elements() isExternal(v)
describe the binary Tree ADT
at most two children. children replaced with left and right adds sibling(v)
what is depth and what is height
depth, number of ancestors excluding self
height, maximal depth
what is a proper/full binary tree
either 0 or 2 children
what is a complete tree
all levels full, except for last, where all nodes are as far left as possible
what is a perfect tree
proper tree where all external nodes have equal depth
what is an ordered tree
all elements left < right and stored element, and stored element < right
what is a sparse tree
lots of empty elements
how would a binary tree be represented using vector (array) represenation
A[0] = root
parent = A[(k-1)/2]
left child = A[2k+1]
right child = A[2k+2]
how would a binary tree be represented in a linked structure
each node has: value, left pointer, right pointer
what is a traversal
systematic way of visiting each node
describe pre-order traversal
node then children
describe post-order traversal
children then node
describe in-order traversal
left then node then right
describe depth first search
search children before sibling
describe breadth first search
search cell nodes at same level before incrementing level
what is a binary search tree
ordered binary tree
describe balance
achieved y rotations
assures us about height
describe when a single left rotation (LL) is performed and how
A becomes unbalanced when a node is inserted in the right subtree of A’s right subtree (B). We perform the left rotation by making A the left-subtree of B. A takes ownership of B’s left child as its right child
B takes ownership of A as its left child
describe when a single right rotation (RR) is performed and how
the unbalanced node is inserted in the left subtree of C’s left subtree (B).
B becomes the new root.
C takes ownership of b’s right child, as its left child.
B takes ownership of C, as it’s right child
describe a double left rotation
otherwise known as: left right
perform a right rotation on the right subtree
then left rotation on the root
describe a double right rotation
otherwise known as: right left
performed when attempting to balance a tree which has a left subtree, that is right heavy
left rotation on leftsubtree.
then a single right rotation
describe the height balance property
for every node, the heights of children may differ by at most 1