Binary Search Tree - Emory`s Math Department

Emory University Logo Guidelines
-
Binary Search Tree
Data Structures and Algorithms
Emory University
Jinho D. Choi
Emory University Logo Guidelines
Abstract Binary Node
Type of the key
-
public abstract class AbstractBinaryNode<T extends Comparable<T>,
N extends AbstractBinaryNode<T,N>>
{
protected T key;
protected N parent;
protected N left_child;
protected N right_child;
Type of the child/parent node
P
public AbstractBinaryNode(T key)
{
setKey(key);
}
N
}
L
2
R
Emory University Logo Guidelines
Abstract Binary Node
public T getKey()
{
return key;
}
public void setKey(T key)
{
this.key = key;
}
public boolean hasParent()
{
return parent != null;
}
public N getParent()
{
return parent;
}
public void setParent(N node)
{
parent = node;
}
public boolean hasLeftChild()
{
return left_child != null;
}
-
public N getLeftChild()
{
return left_child;
}
public boolean hasRightChild()
{
return right_child != null;
}
public N getRightChild()
{
return right_child;
}
public boolean hasBothChildren()
{
return hasLeftChild() && hasRightChild();
}
public N getGrandParent()
{
return hasParent() ? parent.getParent() : null;
}
public boolean isLeftChild(N node)
{
return left_child == node;
}
public boolean isRightChild(N node)
{
return right_child == node;
}
3
Emory University Logo Guidelines
Abstract Binary Node
public N getSibling()
{
if (hasParent())
{
N parent = getParent();
return parent.isLeftChild((N)this) ? parent.getRightChild() :
parent.getLeftChild();
}
return null;
}
public N getUncle()
{
return hasParent() ? parent.getSibling() : null;
}
4
Emory University Logo Guidelines
Abstract Binary Node
public void setLeftChild(N node)
{
replaceParent(node);
left_child = node;
}
public void setRightChild(N node)
{
replaceParent(node);
right_child = node;
}
protected void replaceParent(N node)
{
if (node != null)
{
if (node.hasParent()) node.getParent().replaceChild(node, null);
node.setParent((N)this);
}
}
P
public void replaceChild(N oldChild, N newChild)
{
if
(isLeftChild (oldChild)) setLeftChild (newChild);
else if (isRightChild(oldChild)) setRightChild(newChild);
}
5
N
P
Emory University Logo Guidelines
Binary Node
public class BinaryNode<T extends Comparable<T>>
extends AbstractBinaryNode<T,BinaryNode<T>>
{
public BinaryNode(T key)
{
super(key);
}
}
6
-
Emory University Logo Guidelines
Abstract Binary Search Tree
public abstract class AbstractBinarySearchTree<T extends Comparable<T>,
N extends AbstractBinaryNode<T,N>>
{
protected N root;
public AbstractBinarySearchTree()
{
setRoot(null);
}
}
public N getRoot()
{
return root;
}
public boolean isRoot(N node)
{
return root == node;
}
public void setRoot(N node)
{
if (node != null) node.setParent(null);
root = node;
}
The node type is unknown!
abstract public N createNode(T key);
7
Emory University Logo Guidelines
Abstract BST - Add
53142867
5
-
3
1
8
4
6
2
7
8
Emory University Logo Guidelines
Abstract BST - Add
public N add(T key)
{
N node = null;
private N addAux(N node, T key)
{
int diff = key.compareTo(node.getKey());
N child, newNode = null;
if (root == null)
setRoot(node = createNode(key));
else
node = addAux(root, key);
if (diff < 0)
{
if ((child = node.getLeftChild()) == null)
node.setLeftChild(newNode = createNode(key));
else
newNode = addAux(child, key);
}
else if (diff > 0)
{
if ((child = node.getRightChild()) == null)
node.setRightChild(newNode = createNode(key));
else
newNode = addAux(child, key);
}
return node;
}
return newNode;
}
9
Emory University Logo Guidelines
Abstract BST - Remove
No child.
5
One child.
-
Two children?
3
1
8
4
6
2
7
10
Emory University Logo Guidelines
Abstract BST - Remove
public N remove(T key)
{
N node = findNode(root, key);
-
if (node != null)
{
if (node.hasBothChildren()) removeHibbard(node);
else
removeSelf(node);
}
return node;
}
protected N removeSelf(N node)
{
N parent = node.getParent();
N child = null;
if
(node.hasLeftChild()) child = node.getLeftChild();
else if (node.hasRightChild())child = node.getRightChild();
replaceChild(node, child);
return parent;
}
11
Emory University Logo Guidelines
Abstract BST - Remove
-
protected N removeHibbard(N node)
{
N successor = node.getRightChild();
N min = findMinNode(successor);
N parent = min.getParent();
G
1
min.setLeftChild(node.getLeftChild());
0
5
if (min != successor)
{
parent.setLeftChild(min.getRightChild());
min.setRightChild(successor);
}
4
2
replaceChild(node, min);
return parent;
}
3
12
6
Emory University Logo Guidelines
Binary Search Tree
public class BinarySearchTree<T extends Comparable<T>>
extends AbstractBinarySearchTree<T,BinaryNode<T>>
{
public BinaryNode<T> createNode(T key)
{
return new BinaryNode<T>(key);
}
}
13
Emory University Logo Guidelines
Agenda
•
•
Exercise
-
-
https://github.com/emory-courses/cs323/wiki/Binary-Search-Trees
Reading
-
https://en.wikipedia.org/wiki/AVL_tree
https://en.wikipedia.org/wiki/Red–black_tree
14