二叉树按中序遍历顺序转换成双向链表

8 篇文章 0 订阅
2 篇文章 0 订阅


//首先创建一个可以中序遍历的二叉树结构,并将中序遍历的结果放入List中

package com.dl.tree;



import java.util.ArrayList;
import java.util.List;


public class TreeNode {
   private int data;
   private TreeNode left;
   private TreeNode right;
   private  ArrayList<Integer> middleOr=new ArrayList<Integer>();
   public TreeNode(int data)
   {
  this.data=data;
  left=null;
  right=null;
   }
   public TreeNode()
   {
  super();

   }

//插入结点

   public void insert(TreeNode root,int data)
   {
  if(data>root.data)
  {
  if(root.right==null)
  {
  root.right=new TreeNode(data);
  }
  else{
  this.insert(root.right, data);
  }
 
  }  else
  {
  if(root.left==null)
  {
  root.left=new TreeNode(data);
  }
  else{
  this.insert(root.left, data);
  }
  }
   }
   //中序遍历并得到结果List
   public ArrayList<Integer> middleOrder(TreeNode root)
   {   
   if(root!=null)
   {
    middleOrder(root.left);
       System.out.print(root.data+" ");
       middleOr.add(root.data);
       middleOrder(root.right);
   }
   return middleOr;
   }

   

//补充层次遍历,此题不需要可以删掉

   public void leverOrder(TreeNode root)
   {
  ArrayList<TreeNode> list=new ArrayList<TreeNode>();
  int cur=0;
  int last=0;
      if(root==null)
      {
     System.out.println("树为空");
      }
  else
  {   list.add(this);  
     while
 (cur<list.size())
  {    last=list.size();
  while(cur<last)
  {   System.out.println(list.get(cur)+" "); 
               if(list.get(cur).left!=null)
  {
  list.add(list.get(cur).left);
  }
  if(list.get(cur).right!=null)
  {
  list.add(list.get(cur).right);
  }
 cur++; 
  }
  }

  }
   }
@Override
public String toString() {
return "TreeNode [data=" + data + "]";
}
   

}

//构建双链表结构

package com.dl.tree;


import java.util.ArrayList;
import java.util.List;




public class MyLinkedList {
private TwoWayNode head;
private int size;
   //初始化链表头
   public MyLinkedList()
   {  
 size=0;
 head=new TwoWayNode();
 head.pre=null;
 head.next=null;
   }


   
   public TwoWayNode createLinkedList(TreeNode root)
   {      TwoWayNode current;
           System.out.println("二叉树的遍历结果:");
          ArrayList<Integer> data=root.middleOrder(root);
          if(data.size()==0) 
          {
         System.out.println("链表为空!");
         return null;
          }
         // System.out.println("size:"+data.size());
     head.t=data.get(0);
     if(data.size()==1)
     {
     return head;
     }
     current=new TwoWayNode(data.get(1));
     head.next=current;
     current.pre=head;
     for(int i=2;i<data.size();i++)
     {
     current.next=new TwoWayNode(data.get(i));
     current.next.pre=current;
     current=current.next;
     
     }
     
     return head;
   }

   public static void main(String[] args) {
   TreeNode tree=new TreeNode(6);
tree.insert(tree, 7);
tree.insert(tree, 2);
tree.insert(tree, 5);
tree.insert(tree, 53);
tree.insert(tree, 23);
tree.insert(tree, 21);
tree.insert(tree, 9);
tree.insert(tree, 65);
tree.insert(tree, 45);
tree.insert(tree, 34);
//二叉树的中序遍历结果
//tree.middleOrder(tree);
MyLinkedList list=new MyLinkedList();
//链表的遍历结果


TwoWayNode head=list.createLinkedList(tree);
TwoWayNode current=head;
   System.out.println();
     System.out.println("链表的遍历结果:");
     while(current!=null)
     {
     System.out.print(current.t+" ");
     current=current.next;
     }
}
}

//链表结点类
class TwoWayNode {
 public int t;
 //后继
 public TwoWayNode next;
 //前驱
 public TwoWayNode pre;
 public TwoWayNode(int t){
  this.t=t;
 }
 public TwoWayNode()
 {}


 
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值