树的建立——Java

package com.muluo.tree;

public class TreeNode {
	int value;
	TreeNode leftNode;
	TreeNode rightNode;

	public TreeNode(int value) {
		this.value = value;
	}

	public void setLeftNode(TreeNode leftNode) {
		this.leftNode = leftNode;
	}

	public void setRightNode(TreeNode rightNode) {
		this.rightNode = rightNode;
	}

	// 前序遍历
	public void frontShow() {
		// 先遍历根节点
		System.out.print(value + " ");
		// 左节点
		if (leftNode != null) {
			leftNode.frontShow();
		}
		// 右节点
		if (rightNode != null) {
			rightNode.frontShow();
		}
	}

	// 中序遍历
	public void midShow() {
		if (leftNode != null) {
			leftNode.midShow();
		}
		System.out.print(value + " ");
		if (rightNode != null) {
			rightNode.midShow();
		}
	}

	// 后序遍历
	public void afterShow() {
		if (leftNode != null) {
			leftNode.afterShow();
		}
		if (rightNode != null) {
			rightNode.afterShow();
		}
		System.out.print(value + " ");
	}

	public TreeNode frontSearch(int i) {
		TreeNode target = null;
		if (this.value == i) {
			return this;
		} else {
			if (leftNode != null) {
				target = leftNode.frontSearch(i);
			}
			if (target != null) {
				return target;
			}
			if (rightNode != null) {
				target = rightNode.frontSearch(i);
			}
		}
		return target;
	}

	public void delete(int i) {
		TreeNode parent = this;
		if (parent.leftNode != null && parent.leftNode.value == i) {
			parent.leftNode = null;
		}
		if (parent.rightNode != null && parent.rightNode.value == i) {
			parent.rightNode = null;
		}
		parent = leftNode;
		if (parent != null) {
			parent.delete(i);
		}
		parent = rightNode;
		if (parent != null) {
			parent.delete(i);
		}
	}

	@Override
	public String toString() {
		return this.value + " ";
	}
}

package com.muluo.tree;

public class BinaryTreee {
	TreeNode root;

	// 设置根节点
	public void setRoot(TreeNode root) {
		this.root = root;
	}

	// 获取根节点
	public TreeNode getRoot() {
		return root;
	}

	public void frontShow() {
		if (root != null) {
			root.frontShow();
		}
	}

	public void midShow() {
		if (root != null) {
			root.midShow();
		}
	}

	public void afterShow() {
		if (root != null) {
			root.afterShow();
		}
	}

	public TreeNode frontSearch(int i) {
		return root.frontSearch(i);
	}

	public void delete(int i) {
		if (root.value == i) {
			root = null;
		} else {
			root.delete(i);
		}
	}
}

package com.muluo.tree;

public class TestBinarchTree {
	public static void main(String[] args) {
		// 创建一棵树(空树,没有结点)
		BinaryTreee binaryTree = new BinaryTreee();
		// 创建一个根节点
		TreeNode root = new TreeNode(1);
		// 把根节点赋值给树
		binaryTree.setRoot(root);
		// 创建左节点
		TreeNode lNode = new TreeNode(2);
		// 将新创建的结点设置为根节点的左结点
		root.setLeftNode(lNode);
		// 创建右节点
		TreeNode rNode = new TreeNode(3);
		// 将新创建的结点设置为根节点的右结点
		root.setRightNode(rNode);
		// 为2创建两个结点
		lNode.setLeftNode(new TreeNode(4));
		lNode.setRightNode(new TreeNode(5));
		// 为3创建两个结点
		rNode.setLeftNode(new TreeNode(6));
		rNode.setRightNode(new TreeNode(7));
		// 前序遍历
		System.out.println("前序遍历: ");
		binaryTree.frontShow();
		System.out.println();
		// 中序遍历
		System.out.println("中序遍历:");
		binaryTree.midShow();
		System.out.println();
		// 后序遍历
		System.out.println("后序遍历:");
		binaryTree.afterShow();
		System.out.println();
		// 查找元素
		System.out.println("查找元素:");
		TreeNode result = binaryTree.frontSearch(5);
		System.out.println(result);
		// 删除结点
		System.out.println("删除元素");
		binaryTree.delete(3);
		binaryTree.frontShow();
	}
}

你可能感兴趣的:(Java,数据结构与算法,算法,java)