Java-二叉树-插入、删除、遍历
2017-08-25 16:48
351 查看
二叉树的具体特性和细节知识点,自行百度,直接上代码。
节点:节点内容、左子孩子、右子孩子、父亲
class Node {
private int data;
private Node leftChild;
private Node rightChild;
private Node parent;
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public Node(int data, Node leftChild, Node rightChild, Node parent) {
this.data = data;
this.leftChild = leftChild;
this.rightChild = rightChild;
this.parent = parent;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getLeftChild() {
return leftChild;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getRightChild() {
return rightChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
}
二叉树构造和操作:
public class BinaryTree {
private Node root;//根节点
//插入节点
public void insertNode(Node root, Node node) {
Node current = root;
while (true) {
if (node.getData() < current.getData()) {
if (current.getLeftChild() == null) {
node.setParent(current);
current.setLeftChild(node);
break;
} else {
current = current.getLeftChild();
}
} else {
if (current.getRightChild() == null) {
node.setParent(current);
current.setRightChild(node);
break;
} else {
current = current.getRightChild();
}
}
}
}
//删除节点
public void deleteNode(Node node) {
if (node.equals(root)) {
root = null;
} else if (node.getParent() != null) {
if (node == node.getParent().getLeftChild()) {
node.getParent().setLeftChild(null);
} else {
node.getParent().setRightChild(null);
}
}
}
//获取某节点的高度
public int geHeight(Node node) {
if (node == null) {
return 0;
} else {
int leftHeight = geHeight(node.getLeftChild());
int rightHeight = geHeight(node.getRightChild());
int max = Math.max(leftHeight, rightHeight);
return max + 1;
}
}
//获取某节点的子节点个数
public int getChildNodes(Node node) {
if (node == null) {
return 0;
} else {
int leftNodes = getChildNodes(node.getLeftChild());
int rightNodes = getChildNodes(node.getRightChild());
return leftNodes + rightNodes + 1;
}
}
//先序遍历树
public void PreOrder(Node root) {
if (root == null)
return;
System.out.print(root.getData() + " ");
PreOrder(root.getLeftChild());
PreOrder(root.getRightChild());
}
//中序
public void MidOrder(Node root) {
if (root == null) return;
MidOrder(root.getLeftChild());
System.out.print(root.getData() + " ");
MidOrder(root.getRightChild());
}
//后序
public void LastOrder(Node root) {
if (root == null) return;
LastOrder(root.getLeftChild());
LastOrder(root.getRightChild());
System.out.print(root.getData() + " ");
}
public BinaryTree() {
}
public BinaryTree(Node root) {
this.root = root;
}
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
}
测试:
public class Main {
public static void main(String[] args) {
BinaryTree bt = new BinaryTree(new Node(1, null, null, null));
int a[] = {5, 3, 2, 7, 4, 9, 8};
for (int i = 0; i < 7; i++) {
bt.insertNode(bt.getRoot(), new Node(a[i], null, null, null));
}
// System.out.println(bt.geHeight(root));//高度
// bt.PreOrder(root);
// System.out.println();
// bt.MidOrder(root);
// System.out.println();
// bt.LastOrder(root);
// System.out.println();
// bt.deleteNode(bt.getRoot());
// bt.PreOrder(bt.getRoot());
// System.out.println(bt.getChildNodes(bt.getRoot()));//子节点数
}
}
节点:节点内容、左子孩子、右子孩子、父亲
class Node {
private int data;
private Node leftChild;
private Node rightChild;
private Node parent;
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public Node(int data, Node leftChild, Node rightChild, Node parent) {
this.data = data;
this.leftChild = leftChild;
this.rightChild = rightChild;
this.parent = parent;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getLeftChild() {
return leftChild;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getRightChild() {
return rightChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
}
二叉树构造和操作:
public class BinaryTree {
private Node root;//根节点
//插入节点
public void insertNode(Node root, Node node) {
Node current = root;
while (true) {
if (node.getData() < current.getData()) {
if (current.getLeftChild() == null) {
node.setParent(current);
current.setLeftChild(node);
break;
} else {
current = current.getLeftChild();
}
} else {
if (current.getRightChild() == null) {
node.setParent(current);
current.setRightChild(node);
break;
} else {
current = current.getRightChild();
}
}
}
}
//删除节点
public void deleteNode(Node node) {
if (node.equals(root)) {
root = null;
} else if (node.getParent() != null) {
if (node == node.getParent().getLeftChild()) {
node.getParent().setLeftChild(null);
} else {
node.getParent().setRightChild(null);
}
}
}
//获取某节点的高度
public int geHeight(Node node) {
if (node == null) {
return 0;
} else {
int leftHeight = geHeight(node.getLeftChild());
int rightHeight = geHeight(node.getRightChild());
int max = Math.max(leftHeight, rightHeight);
return max + 1;
}
}
//获取某节点的子节点个数
public int getChildNodes(Node node) {
if (node == null) {
return 0;
} else {
int leftNodes = getChildNodes(node.getLeftChild());
int rightNodes = getChildNodes(node.getRightChild());
return leftNodes + rightNodes + 1;
}
}
//先序遍历树
public void PreOrder(Node root) {
if (root == null)
return;
System.out.print(root.getData() + " ");
PreOrder(root.getLeftChild());
PreOrder(root.getRightChild());
}
//中序
public void MidOrder(Node root) {
if (root == null) return;
MidOrder(root.getLeftChild());
System.out.print(root.getData() + " ");
MidOrder(root.getRightChild());
}
//后序
public void LastOrder(Node root) {
if (root == null) return;
LastOrder(root.getLeftChild());
LastOrder(root.getRightChild());
System.out.print(root.getData() + " ");
}
public BinaryTree() {
}
public BinaryTree(Node root) {
this.root = root;
}
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
}
测试:
public class Main {
public static void main(String[] args) {
BinaryTree bt = new BinaryTree(new Node(1, null, null, null));
int a[] = {5, 3, 2, 7, 4, 9, 8};
for (int i = 0; i < 7; i++) {
bt.insertNode(bt.getRoot(), new Node(a[i], null, null, null));
}
// System.out.println(bt.geHeight(root));//高度
// bt.PreOrder(root);
// System.out.println();
// bt.MidOrder(root);
// System.out.println();
// bt.LastOrder(root);
// System.out.println();
// bt.deleteNode(bt.getRoot());
// bt.PreOrder(bt.getRoot());
// System.out.println(bt.getChildNodes(bt.getRoot()));//子节点数
}
}
相关文章推荐
- 二叉树:实现java操作二叉排序树(生成、插入、遍历、删除)
- 二叉树(插入,删除,遍历等)java实现
- 二叉搜索树Java实现(查找、插入、删除、遍历)
- java实现单链表,功能有插入,删除,遍历。
- Java实现二叉树的创建、删除、遍历
- java专题——二叉树(插入、查找、删除)
- 二叉树的遍历、查找、插入以及删除
- 二叉树的各种操作:前序、中序、后序、层序遍历,二叉树搜索、插入和删除等操作
- Java-集合 list对象进行指定位置插入、删除、迭代、遍历输出(具体习题讲解)
- 数据结构二叉树的java实现,包括二叉树的创建、搜索、删除和遍历
- 二叉树的查找,插入,删除,遍历,最小值,最大值 操作
- 二叉树的先序中序后序递归非递归遍历,插入删除等操作
- C++ 二叉树(创建,遍历,查找,插入,删除)『菜鸟版』
- 二叉树2(二叉查找树的插入、查找、删除、遍历)
- 二叉树的先序中序后序递归非递归遍历,插入删除等操作
- 二叉树的学习(四种遍历方法,搜索,插入,删除等)
- Java实现二叉查找树的创建、查找、插入、删除、遍历
- java实现二叉查找树(插入、删除、遍历、查找)
- 二叉树的创建,插入,删除,输出,求高度,求度以及三种遍历方式实现
- Java 实现二叉搜索树的查找、插入、删除、遍历