1.节点定义
2.相关实现方法
3.遍历二叉树
节点定义:
public class Node {
int value;
Node left_child;
Node right_child;
public Node(int value) {
super();
this.value = value;
}
public Node(int value, Node left_child, Node right_child) {
super();
this.value = value;
this.left_child = left_child;
this.right_child= right_child;
}
public String toString() {
return "TreeNode [value=" + value + ", leftchild=" + left_child + ", right=" + right_child + "]";
}
}
相关方法:
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
public class LikedBinaryTree implements Binary {
private Node root;//定义根节点
public LikedBinaryTree() {
super();
}
public LikedBinaryTree(Node root) {
super();
this.root = root;
}
//判空
public boolean isEmpty() {
return root==null;
}
//输出节点个数
public int size() {
return this.size(root);
}
//实现
pvivate int size(Node root) {
if(root==null)
{
return 0;
}
else {
int sl = this.size(root.left_child);
int sr = this.size(root.right_child);
return sl+sr+1;
}
}
//树的高度
public int getHeight() {
System.out.print("树的高度为:");
return this.getHeight(root);
}
//实现
private int getHeight(Node root) {
if (root == null)
return 0;
else {
int nl = this.getHeight(root.left_child);
int nr = this.getHeight(root.right_child);
return nl > nr ? nl + 1 : nr + 1;
}
}
//查找节点
public Node finkKey(int value) {
return this.finkKey(value, root);
}
private Node finkKey(int value,Node root) {
if(root==null)
return null;
else if(root!=null&&root.value==value)
return root;
else {
Node Node1=this.finkKey(value, root.left_child);
Node Node2=this.finkKey(value, root.right_child);
if(Node1!=null&&Node1.value==value)
return Node1;
else if(Node2!=null&&Node2.value==value)
return Node2;
else return null;
}
}
二叉树的遍历
根据根的不同位置可分为三种遍历方式。
前序(后序):根、左、右
中序:左、根、右
后序:左、右、根
//先序遍历
public void preOrderTraverse() {
this.preOrderTraverse(root);
}
private void preOrderTraverse(Node root){
if (root != null) {
System.out.print(root.value+" ");
this.preOrderTraverse(root.left_child);
this.preOrderTraverse(root.right_child);
}
}
//中序遍历
public void inOrderTraverse() {
this.inOrderTraverse(root);
}
public void inOrderTraverse(Node root) {
if (root != null) {
this.inOrderTraverse(root.left_child);
System.out.print(root.value+" ");
this.inOrderTraverse(root.right_child);
}
}
//后序遍历
public void postOrderTraverse() {
this.postOrderTraverse(root);
}
private void postOrderTraverse(Node root) {
if (root != null) {
this.postOrderTraverse(root.left_child);
this.postOrderTraverse(root.right_child);
System.out.print(root.value+" ");
}
}
非递归中序遍历:(借助栈实现)
public void inOrderbyStack() {
Deque<Node> stack = new LinkedList<Node>();
Node current = root;
while (current!=null||!stack.isEmpty()) {
while(current!=null) {
stack.push(current);
current=current.left_child;
}
if(!stack.isEmpty()) {
current = stack.pop();
System.out.println(current.value+" ");
current =current.right_child;
}
}
}
按照层次遍历(借助队列实现):
public void leveOrderByStack() {
if(root==null)
return;
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while(queue.size()!=0) {
int len= queue.size();
for(int i =0;i<len;i++) {
Node temp = queue.poll();
System.out.println(temp.value+" ");
if(temp.left_child!=null) queue.add(temp.left_child);
if(temp.right_child!=null) queue.add(temp.right_child);
}
}
}