二叉树

树是这样的数据结构,可以像链表那样快速地插入和删除,也可以像有序数组那样快速地查询;二叉树,只有两个字节点。
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数据结构与算法第二版
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值