java有内置二叉树,二叉树java - osc_1jzw6di0的个人空间 - OSCHINA - 中文开源技术交流社区...

这个博客展示了如何实现二叉树的插入、删除、中序、前序和后序遍历。代码详细地定义了一个`TreeApp`类,包含了节点结构以及相关操作方法。此外,还实现了显示树的层次遍历以及一个删除节点的逻辑,该逻辑考虑了不同情况如节点是否有子节点等。最后,博客通过实例展示了这些操作。
摘要由CSDN通过智能技术生成

public classTreeApp {privateNode root; //根节点public TreeApp(){root = null;}public void insert(intid) { //插入

Node newNode= newNode(); //定义新的要插入的节点

newNode.data=id; //新节点里面的数据if(root == null){root =newNode;} //判断有没有根节点,没有根节点,这个新的为根节点else{

Node current=root;

Node parent;while(true)

{

parent=current;

{if(id

{

current=current.leftChild;if (current == null)

{

parent.leftChild=newNode;return;

}

}else{

current=current.rightChild;if(current == null)

{

parent.rightChild=newNode;return;

}

}

}

}

}

}//中序遍历

public voidinOrder(Node localRoot) {if(localRoot != null)

{

inOrder(localRoot.leftChild);

System.out.print(localRoot.data + " ");

inOrder(localRoot.rightChild);

}

}//前序遍历

public voidpreOrder(Node localRoot) {if(localRoot != null)

{

System.out.print(localRoot.data + " ");

preOrder(localRoot.leftChild);

preOrder(localRoot.rightChild);

}

}//后序遍历

public voidpostOrder(Node localRoot) {if(localRoot != null)

{

postOrder(localRoot.leftChild);

postOrder(localRoot.rightChild);

System.out.print(localRoot.data + " ");

}

}//得到要删除节点的后继节点

publicNode getSuccessor(Node delNode){

Node successorParent=delNode;

Node successor=delNode;

Node current=delNode.rightChild;while(current != null)

{

successorParent=successor;

successor=current;

current=current.leftChild;

}if(successor !=delNode.rightChild)

{

successorParent.leftChild=successor.rightChild;

successor.rightChild=delNode.rightChild;

}returnsuccessor;

}public boolean delete(intkey)

{

Node current=root;

Node parent=root;

boolean isLeftChild= true;while(current.data != key) //找到要删除的节点

{

parent=current;if(key

isLeftChild= true;

current=current.leftChild;

}else{

isLeftChild= false;

current=current.rightChild;

}if(current == null)return false; //没找到就返回错误

}//如果要删除的节点没有子节点

if (current.leftChild == null && current.rightChild == null)

{if (current ==root)

{

root= null;

}else if(isLeftChild)

{

parent.leftChild= null;

}elseparent.rightChild= null;

}//如果要删除的节点没有左子树

else if(current.leftChild == null)

{if(current ==root)

{

root=current.rightChild;

}else if(isLeftChild)

{

parent.leftChild=current.rightChild;

}elseparent.rightChild=current.rightChild;

}//如果要删除的节点没有右子树

else if(current.rightChild == null)

{if(current ==root)

{

root=current.leftChild;

}else if(isLeftChild)

{

parent.leftChild=current.leftChild;

}elseparent.rightChild=current.leftChild;

}//要删除的节点有左右两个子树,删除的节点要替换成它的中序后继

else{

Node successor=getSuccessor(current);if(current ==root)

{

root=successor;

}else if(isLeftChild)

{

parent.leftChild=successor;

}elseparent.rightChild=successor;

successor.leftChild=current.leftChild;

}return true;

}public voiddisplayTree()

{

Stack globalStack= newStack();

globalStack.push(root);int nBlanks = 32;

boolean isRowEmpty= false;

System.out.println("...........................................");while (isRowEmpty == false)

{

Stack localStack= newStack();

isRowEmpty= true;for(int j = 0; j < nBlanks;j++)

{

System.out.print(' ');

}while(globalStack.isEmpty()==false)

{

Node temp=(Node)globalStack.pop();if(temp != null) {

System.out.print(temp.data);

localStack.push(temp.leftChild);

localStack.push(temp.rightChild);if (temp.leftChild != null || temp.rightChild != null)

{

isRowEmpty= false;

}

}else{

System.out.print("--");

localStack.push(null);

localStack.push(null);

}for (int j = 0; j < nBlanks * 2 - 2; j++) {

System.out.print(' ');

}

}

System.out.println();

nBlanks/= 2;while(localStack.isEmpty() == false)

globalStack.push(localStack.pop());

}

}public static voidmain(String[] args) {

TreeApp myTree= newTreeApp();

myTree.insert(10);

myTree.insert(6);

myTree.insert(14);

myTree.insert(8);

myTree.insert(12);

myTree.insert(16);

myTree.insert(3);

myTree.insert(7);

myTree.insert(15);

myTree.insert(19);

myTree.insert(20);

myTree.insert(17);

System.out.print("前序遍历:");

myTree.preOrder(myTree.root);

System.out.println();

System.out.print("中序遍历:");

myTree.inOrder(myTree.root);

System.out.println();

System.out.print("后序遍历:");

myTree.postOrder(myTree.root);

System.out.println();

System.out.println("删除"+myTree.delete(8));

myTree.displayTree();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值