二元查找树转化为排序双链表

问题:给定一个二叉搜索树,要求不创建新的节点,只调整二叉搜索树中的指针指向,

           将该二叉搜索树转化为有序的双链表。

解题思路:图1展示了一个二叉搜索树和对应的双链表,其中的二叉搜索树也可以叫二叉排序树,它的

           中序遍历可以得到一个有序序列。使用中序遍历将二叉搜索树转化为双向链表可以保证链表有序。

           转化的过程如下:中序遍历二叉树,将上次访问的节点记为Last,当前访问的节点记为current;

           对于遍历过程中的每个当前节点,让该节点的左指针指向last(current->left = last),让last的右指针

           指向当前节点(last->right = current),然后将last更新为current。当中序遍历结束时,二叉搜索树也

           被转化为双链表了。

                  例如:对于图1,当遍历到节点12时(current:12),终须遍历上次得到的节点为11(Last=11),

           此时,将节点12的左指针指向节点11,将节点11的右指针节点12。        

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

public class BSTreeToLinkedList {

	/*
	 * 把二元查找树转变成排序的双向链表 题目: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
	 * 要求不能创建任何新的结点,只调整指针的指向。 10 / \ 6 14 / \ / \ 4 8 12 16 转换成双向链表
	 * 4=6=8=10=12=14=1
	 */
	public static void main(String[] args) {
		BSTreeToLinkedList bn = new BSTreeToLinkedList();
		int[] a = { 10, 6, 14, 4, 8, 12, 16 };// 这些数据是按二叉查找树的层次遍历存放
		Node head = bn.creatTree(a);
		bn.toTwoWayLinkedList(head);
		bn.printTwoLinkedList(head);

	}

	/*
	 * 思路:中序遍历二叉树,将上次访问的节点记为previous,当前访问的节点记为current;
	 * 
	 * 对于遍历过程中的每个当前节点,让该节点的左指针指向previous(current->left = previous),让previous的右指针
	 * 
	 * 指向当前节点(previous->right = current),然后将previous更新为current。当中序遍历结束时,二叉搜索树也
	 * 
	 * 被转化为双链表了。
	 * 
	 * 
	 * 
	 * 问题:这个previous只能作为类成员才能得到正确的结果,作为局部变量的话,我得不到正解。 我尝试过这样写:
	 * toTwoWayLinkedList(Node node,Node previous),在main函数里面调用时候,用
	 * toTwoWayLinkedList(head,null),得不到正确答案
	 */
	private Node previous;//上次访问的节点

	public void toTwoWayLinkedList(Node node) {
		if (node != null) {
			toTwoWayLinkedList(node.getLeft());
			if (previous != null) {
				previous.setRight(node);
				node.setLeft(previous);
			}
			previous = node;
			toTwoWayLinkedList(node.getRight());
		}
	}

	public void printTwoLinkedList(Node node) {
		if (node != null) {
			while (node.getLeft() != null) {
				node = node.getLeft();// find the true Head.
			}
			while (node != null) {
				System.out.print(node.getData() + " ");
				node = node.getRight();
			}
		}
	}

	public Node creatTree(int[] data) {
		List<Node> nodeList = new ArrayList<Node>();
		for (int each : data) {
			Node node = new Node(each);
			nodeList.add(node);
		}
		int lastRootIndex = data.length / 2 - 1;
		for (int i = lastRootIndex; i >= 0; i--) {
			int leftIndex = i * 2 + 1;
			Node root = nodeList.get(i);
			Node left = nodeList.get(leftIndex);
			root.setLeft(left);
			if (leftIndex + 1 < data.length) {
				Node right = nodeList.get(leftIndex + 1);
				root.setRight(right);
			}
		}
		Node head = nodeList.get(0);
		return head;

	}

}

class Node {
	private int data;
	private Node left;
	private Node right;

	public Node(int i) {
		data = i;
	}

	public int getData() {
		return data;
	}

	public void setData(int data) {
		this.data = data;
	}

	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;
	}
}

转http://bylijinnan.iteye.com/blog/1343176

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值