package com.gxmedu.binary_tree;
/**
* @author 郭学明
* @version 1.0
*/
public class BinaryTree {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
Node root = binaryTree.new Node(1, "宋江");
Node node2 = binaryTree.new Node(2, "吴用");
Node node3 = binaryTree.new Node(3,"卢俊义");
Node node4 = binaryTree.new Node(4,"林冲");
Node node5 = binaryTree.new Node(5,"关胜");
binaryTree.setRoot(root);
root.setLeft(node2);
root.setRight(node3);
node3.setLeft(node5);
node3.setRight(node4);
binaryTree.preorder();
// binaryTree.deleteNode(3);
// binaryTree.deleteOneNode(3);
System.out.println("======================");
binaryTree.preorder();
System.out.println("======================");
binaryTree.inorder();
System.out.println("======================");
binaryTree.postorder();
System.out.println("======================");
Node result = binaryTree.preorderSearch(4);
System.out.println(result);
}
private Node root;
public Node getRoot() {
return root;
}
public void setRoot(Node node){
this.root = node;
}
public void preorder(){
if(root != null){
root.preorder();
}else{
System.out.println("树为空,无法遍历");
}
}
public void inorder(){
if(root != null){
root.inorder();
}else{
System.out.println("树为空,无法遍历");
}
}
public void postorder(){
if(root != null){
root.postorder();
}else{
System.out.println("树为空,无法遍历");
}
}
public Node preorderSearch(int id){
if(root != null){
return root.preorderSearch(id);
}else{
return null;
}
}
public Node inorderSearch(int id){
if(root != null){
return root.inorderSearch(id);
}else{
return null;
}
}
public Node postorderSearch(int id){
if(root != null){
return root.postorderSearch(id);
}else{
return null;
}
}
public void deleteNode(int id){
if(root == null){
System.out.println("树为空,无法删除");
}else if(root.id == id){
root = null;
}else{
root.deleteNode(id);
}
}
public void deleteOneNode(int id){
if(root == null){
System.out.println("树为空,无法删除");
}else if(root.id == id){
root = root.inherit();
}else{
root.deleteOneNode(id);
}
}
public class Node{
private int id;
private String name;
private Node left;
private Node right;
/**
* 遍历后续线索化二叉树会用到
*/
private Node parent;
private boolean traverseRightChildTree;
public boolean isTraverseRightChildTree() {
return traverseRightChildTree;
}
public void setTraverseRightChildTree(boolean traverseRightChildTree) {
this.traverseRightChildTree = traverseRightChildTree;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
/**
* 在线索二叉树中会用到
*/
private int leftNodeType;
private int rightNodeType;
public int getLeftNodeType() {
return leftNodeType;
}
public void setLeftNodeType(int leftNodeType) {
this.leftNodeType = leftNodeType;
}
public int getRightNodeType() {
return rightNodeType;
}
public void setRightNodeType(int rightNodeType) {
this.rightNodeType = rightNodeType;
}
public void preorder(){
System.out.println(this);
if(this.left != null){
this.left.preorder();
}
if(this.right != null){
this.right.preorder();
}
}
public void inorder(){
if(this.left != null){
this.left.inorder();
}
System.out.println(this);
if(this.right != null){
this.right.inorder();
}
}
public void postorder(){
if(this.left != null){
this.left.postorder();
}
if(this.right != null){
this.right.postorder();
}
System.out.println(this);
}
public Node preorderSearch(int id){
if(this.id == id){
return this;
}
/**
* 还需要判断递归结果是否为空
*/
Node result = null;
if(this.left != null){
result = this.left.preorderSearch(id);
}
if(result != null){
return result;
}
if(this.right != null){
result = this.right.preorderSearch(id);
}
return result;
}
public Node inorderSearch(int id){
Node result = null;
if(this.left != null){
result = this.left.inorderSearch(id);
}
if(result != null){
return result;
}
if(this.id == id){
return this;
}
if(this.right != null){
result = this.right.inorderSearch(id);
}
return result;
}
public Node postorderSearch(int id){
Node result = null;
if(this.left != null){
result = this.left.postorderSearch(id);
}
if(result != null){
return result;
}
if(this.right != null){
result = this.right.postorderSearch(id);
}
if(result != null){
return result;
}
if(this.id == id){
return this;
}
return result;
}
public void deleteNode(int id){
if(this.left != null && this.left.id == id){
this.left = null;
return;
}
if(this.right != null && this.right.id == id){
this.right = null;
return;
}
if(this.left != null){
this.left.deleteNode(id);
}
if(this.right != null){
this.right.deleteNode(id);
}
}
/**
* 只删除一个节点,其位置将按左子节点到右子节点的顺序继承,这里需要额外写一个方法实现继承顺序
* @param id
*/
public void deleteOneNode(int id){
if(this.left != null && this.left.id == id){
this.left = this.left.inherit();
return;
}
if(this.right != null && this.right.id == id){
this.right = this.right.inherit();
return;
}
if(this.left != null){
this.left.deleteOneNode(id);
}
if(this.right != null){
this.right.deleteOneNode(id);
}
}
public Node inherit(){
if(this.left != null){
return this.left;
}
if(this.right != null){
return this.right;
}
return null;
}
public Node(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
}
java binary tree
最新推荐文章于 2024-06-14 15:02:13 发布