01 如何实现二叉树
首先定义树的结点
public class Node {
public int data;
public Node left;
public Node right;
public Node (int data){
this.data=data;
this.left=null;
this.right=null;
}
public int leftMaxDistance;//左子树距根结点的最大距离
public int rightMaxDistance;//右子数距根结点的最大距离
}
二叉树的实现
public class BinaryTree {
private Node root;
public BinaryTree(){
root=null;
}
public Node getRoot(){
return root;
}
//将data插入到排序二叉树中
public void insert(int data){
Node newNode = new Node(data);
if (root==null){
root=newNode;
}else {
Node current=root;
Node parent;
while (true){//寻找插入的位置
parent =current;
if (data<current.data){
current=current.left;
if (current==null){
parent.left=newNode;
return;
}
}else{
current=current.right;
if (current==null){
parent.right=newNode;
return;
}
}
}
}
}
//将数值输入构建二叉树
public void buildTree(int[]data){
for (int i=0;i<data.length;i++){
insert(data[i]);
}
}
//中序遍历方法递归实现
public void inOrder(Node localRoot){
if (localRoot!=null){
inOrder(localRoot.left);
System.out.print(localRoot.data+" ");
inOrder(localRoot.right);
}
}
public void inOrder(){
this.inOrder(this.root);
}
//先序遍历方法递归实现
public void preOrder(Node localRoot){
if (localRoot!=null){
System.out.print(localRoot.data+" ");
preOrder(localRoot.left);
preOrder(localRoot.right);
}
}
public void preOrder(){
this.preOrder(this.root);
}
//后续遍历方法递归实现
public void postOrder(Node localRoot){
if (localRoot!=null){
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.data+" ");
}
}
public void postOrder(){
this.postOrder(this.root);
}
public static void main(String[] args) {
BinaryTree biTree=new BinaryTree();
int[] data={2,8,7,4,9,3,1,6,7,5};
biTree.buildTree(data);
System.out.println("二叉树的中序遍历");
biTree.inOrder();
System.out.println();
System.out.println("二叉树的先序遍历");
biTree.preOrder();
System.out.println();
System.out.println("二叉树的后序遍历");
biTree.postOrder();
}
}
02 如何层序遍历二叉树
可以用队列实现二叉树的层序遍历。主要思路如下:先将根结点放入队列中,然后每次都从队列中取出一个结点打印该结点的值,若这个结点有子结点,则将它的子结点放入队列,直到队列为空.
public static void layerTranverse(Node root){
if(root==null){
return;
}
Queue<Node> q = new LinkedList<>();
((LinkedList<Node>) q).add(root);
while (!q.isEmpty()){
Node n=q.poll();
System.out.print(n.data+" ");
if (n.left!=null)
((LinkedList<Node>) q).add(n.left);
if (n.right!=null)
((LinkedList<Node>) q).add(n.right);
}
}
public static void main(String[] args) {
BinaryTree biTree=new BinaryTree();
int[] data={2,8,7,4,9,3,1,6,7,5};
biTree.buildTree(data);
layerTranverse(biTree.getRoot());
}
03 已知先序遍历和中序遍历,如何求后序遍历
public class BinaryTree1 {
private Node root;
public BinaryTree1(){
root=null;
}
//后序遍历方法,递归实现
public void postOrder(Node localRoot){
if (localRoot!=null){
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.data+" ");
}
}
public void postOrder(){
this.postOrder(this.root);
}
public void initTree(int[]preOrder,int[]inOrder){
this.root=this.initTree(preOrder,0,preOrder.length-1,inOrder,0,inOrder.length-1);
}
private Node initTree(int[] preOrder, int start1, int end1, int[] inOrder, int start2, int end2) {
if (start1>end1||start2>end2){
return null;
}
int rootData=preOrder[start1];
Node head = new Node(rootData);
//找到根结点所在的位置
int rootIndex=findIndexInArray(inOrder,rootData,start2,end2);
int offSet=rootIndex-start2-1;
//构建左子树
Node left=initTree(preOrder,start1+1,start1+1+offSet,inOrder,start2,start2+offSet);
//构建右子数
Node right=initTree(preOrder,start1+offSet+2,end1,inOrder,rootIndex+1,end2);
head.left=left;
head.right=right;
return head;
}
private int findIndexInArray(int[] a, int x, int begin, int end) {
for (int i=begin;i<=end;i++){
if (a[i]==x){
return i;
}
}
return -1;
}
public static void main(String[] args) {
BinaryTree1 biTree=new BinaryTree1();
int[] preOrder={1,2,4,8,9,5,10,3,6,7};
int[] inOrder={8,4,9,2,10,5,1,6,3,7};
biTree.initTree(preOrder,inOrder);
System.out.println("二叉树的后序遍历:");
biTree.postOrder();
}
}
04 如何求二叉树中结点的最大距离
public class BinaryTree2 {
private int maxLen=0;
private int max(int a,int b){
return a>b?a:b;
}
public void FindMaxDistance(Node root){
if (root==null)
return;
if (root.left==null)
root.leftMaxDistance=0;
if (root.right==null)
root.rightMaxDistance=0;
if (root.left!=null)
FindMaxDistance(root.left);
if (root.right!=null)
FindMaxDistance(root.right);
//计算左子树中距离更节点的最大距离
root.leftMaxDistance=max(root.left.leftMaxDistance,root.left.rightMaxDistance)+1;
//计算右子数中距离根结点的最大距离
root.rightMaxDistance=max(root.right.leftMaxDistance,root.right.rightMaxDistance)+1;
//获取二叉树所有节点的最大距离
if (root.leftMaxDistance+root.rightMaxDistance>maxLen)
maxLen=root.leftMaxDistance+root.rightMaxDistance;
}
}