树是这样的数据结构,可以像链表那样快速地插入和删除,也可以像有序数组那样快速地查询;二叉树,只有两个字节点。
注:以上代码均出自java数据结构与算法第二版
class Node
{
public final int iData;
public final double fData;
public Node leftChild;
public Node rightChild;
public Node(int id, double fd)
{
iData = id;
fData = fd;
}
public void displayNode()
{
System.out.println(iData + fData);
}
}
class Tree
{
public Node root;
/**
* 左孩子小于结点,右孩子大于结点
*
* @param id
* @param dd
*/
public void insert(int id, double dd)
{
Node newNode = new Node(id, dd);
if(root == null)
root = newNode;
else
{
Node parent;
Node current = root;
while (true)
{
parent = current;
if(current.iData > id)
{
current = current.leftChild;
if(current == null)
{
parent.leftChild = newNode;
return;
}
}
else
{
current = current.rightChild;
if(current == null)
{
parent.rightChild = newNode;
return;
}
}
}
}
}
public Tree()
{
root = null;
}
public boolean delete(int key)
{
Node current = root;
Node parent = root;
boolean isLeftChild = true;
while (current.iData != key)
{
parent = current;
if(current.iData > key)
{
isLeftChild = true;
current = current.leftChild;
}
else
{
isLeftChild = false;
current = current.rightChild;
}
if(current == null)
return false;
}
// 分三种可能,无子结点,有一个子结点,有两个子节点
delWithNode(current, parent, isLeftChild);
return true;
}
public void delWithNode(Node current, Node parent, boolean isLeftChild)
{
if(current.leftChild == null && current.rightChild == null)
{
if(current == root)
root = null;
else
if(isLeftChild)
parent.leftChild = null;
else
parent.rightChild = null;
}
else
if(current.leftChild != null && current.rightChild == null)
{
if(current == root)
root = current.leftChild;
else
if(isLeftChild)
parent.leftChild = current.leftChild;
else
parent.rightChild = current.leftChild;
}
else
if(current.rightChild != null && current.leftChild == null)
{
if(current == root)
root = current.rightChild;
else
if(isLeftChild)
parent.leftChild = current.rightChild;
else
parent.rightChild = current.rightChild;
}
else
{
Node successNode = getSuccessor(current);
if(current == root)
root = successNode;
else
if(isLeftChild)
parent.leftChild = successNode;
else
parent.rightChild = successNode;
successNode.leftChild = current.leftChild; // 将要删除的结点的左子树传给新的节点
}
}
/**
* 当要删除的结点有左右两个子结点时获取要替换的结点
*
* @param delNode
* @return
*/
private Node getSuccessor(Node delNode)
{
Node successNode = delNode;
Node successParent = delNode;
Node current = successNode.rightChild;
while (current != null)
{ //查找右子树的最小值,即右子数的最左节点
successParent = successNode;
successNode = current;
current = current.leftChild;
}
if(successNode != delNode.rightChild)
{ // 如果要替换的结点不是其右结点,处理该结点右边的结点
successParent.leftChild = successNode.rightChild;
successNode.rightChild = delNode.rightChild;
}
return successNode;
}
/**
* 前序遍历:访问根;按前序遍历左子树;按前序遍历右子树
*
* @param localNode
*/
public void preOrder(Node localNode)
{
if(localNode != null)
{
System.out.print(localNode.iData + " ");
preOrder(localNode.leftChild);
preOrder(localNode.rightChild);
}
}
/**
* 中序遍历:按中序遍历左子树;访问根;按中序遍历右子树
*
* @param localNode
*/
public void inOrder(Node localNode)
{
if(localNode != null)
{
inOrder(localNode.leftChild);
System.out.print(localNode.iData + " ");
inOrder(localNode.rightChild);
}
}
/**
* 后序遍历:按后序遍历左子树;按后序遍历右子树;访问根
*
* @param localNode
*/
public void postOrder(Node localNode)
{
if(localNode != null)
{
postOrder(localNode.leftChild);
postOrder(localNode.rightChild);
System.out.print(localNode.iData + " ");
}
}
}
注:以上代码均出自java数据结构与算法第二版