package AlgorithmTest; /** * Created by dell on 2015/10/7. */ public class BinarySortToDequeTest { public static void main(String[] args) { BinarySearchTree binarySearchTree = new BinarySearchTree(); binarySearchTree.insertNode(10); binarySearchTree.insertNode(6); binarySearchTree.insertNode(4); binarySearchTree.insertNode(8); binarySearchTree.insertNode(14); MyLinkedList myDeque = MyLinkedList.ConvertFromBinarySortTree(binarySearchTree); myDeque.print(); } } class MyLinkedList { private Node firstNode; public void setFirstNode(Node firstNode){ this.firstNode = firstNode; } public void print(){ if (firstNode != null){ for (Node iter = firstNode; iter != null; iter = iter.nodeRight){ System.out.println(iter.val); } } } public static MyLinkedList ConvertFromBinarySortTree(BinarySearchTree tree){ MyLinkedList myDeque = new MyLinkedList(); if (null != tree && tree.getRootNode() != null){ ConvertFromBinarySortTree(tree.getRootNode()); Node minNode = tree.getRootNode(); while (minNode.nodeLeft != null){ minNode = minNode.nodeLeft; } myDeque.setFirstNode(minNode); tree.setRootNode(null); } return myDeque; } private static void ConvertFromBinarySortTree(final Node node){ if (node != null){ if (node.nodeLeft != null){ ConvertFromBinarySortTree(node.nodeLeft); Node leftMaxNode = node.nodeLeft; while (leftMaxNode.nodeRight != null){ leftMaxNode = leftMaxNode.nodeRight; } Node1ConnectNode2(leftMaxNode, node); } if (node.nodeRight != null){ ConvertFromBinarySortTree(node.nodeRight); Node rightMinNode = node.nodeRight; while (rightMinNode.nodeLeft != null){ rightMinNode = rightMinNode.nodeLeft; } Node1ConnectNode2(node, rightMinNode); } } } private static void Node1ConnectNode2(Node node1, Node node2){ if (node1 != null){ node1.nodeRight = node2; if (node2 != null){ node2.nodeLeft = node1; } } } } class Node{ int val; Node nodeLeft; Node nodeRight; public Node(int val, Node nodeLeft, Node nodeRight){ this.val = val; this.nodeLeft = nodeLeft; this.nodeRight = nodeRight; } public int getVal() { return val; } public void setVal(int val) { this.val = val; } public Node getNodeLeft() { return nodeLeft; } public void setNodeLeft(Node nodeLeft) { this.nodeLeft = nodeLeft; } public Node getNodeRight() { return nodeRight; } public void setNodeRight(Node nodeRight) { this.nodeRight = nodeRight; } } class BinarySearchTree { private Node rootNode; public BinarySearchTree(){ rootNode = null; } public Node getRootNode() { return rootNode; } public void setRootNode(Node rootNode) { this.rootNode = rootNode; } public boolean insertNode(int val){ if (null == rootNode){ rootNode = new Node(val, null, null); return true; } return insertNode(rootNode, val); } public void printVals(){ printVals(rootNode); } public boolean findVal(int val){ return findVal(rootNode, val); } private boolean insertNode(final Node node, final int val){ if (val == node.val){ return false; } if (val > node.val){ if (null == node.getNodeRight()){ node.setNodeRight(new Node(val, null, null)); return true; }else { return insertNode(node.getNodeRight(), val); } } if (val < node.val){ if (null == node.getNodeLeft()){ node.setNodeLeft(new Node(val, null, null)); return true; }else { return insertNode(node.getNodeLeft(), val); } } return true; } private void printVals(Node node){ if (null == node){ return; } printVals(node.getNodeLeft()); System.out.println(node.val); printVals(node.getNodeRight()); } public boolean findVal(final Node node, final int val){ if (null == node){ return false; } if (val == node.val){ return true; } if (val > node.val){ return findVal(node.getNodeRight(), val); } if (val < node.val){ return findVal(node.getNodeLeft(), val); } return true; } }
二叉排序树转化为顺序双链表 JAVA实现
最新推荐文章于 2024-01-12 23:24:41 发布