Java与算法(9)
1.给定二叉树头节点,每个节点的值都不同,找到含有节点最多的搜索二叉子树。
public class FindBST {
public static class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
left = null;
right = null;
}
}
public static ArrayList<Node> trees = new ArrayList<>();
public void buildTree(int[] array) {
for(int i=0;i<array.length;i++) {
trees.add(new Node(array[i]));
}
for(int index = 0;index<array.length/2-1;index++) {
trees.get(index).left = trees.get(index*2+1);
trees.get(index).right = trees.get(index*2+2);
}
int lastindex = array.length/2-1;
trees.get(lastindex).left = trees.get(lastindex*2+1);
if (array.length%2==1) {
trees.get(lastindex).right = trees.get(lastindex*2+2);
}
}
public Node bigBST(Node root) {
int[] record = new int[3];
return find(root, record);
}
public Node find(Node root,int[] r) {
if (root==null) {
r[0] = 0;
r[1] = Integer.MAX_VALUE;
r[2] = Integer.MAX_VALUE;
return null;
}
int data = root.data;
Node right = root.right;
Node left = root.left;
Node lBST = find(left,r);
int lSIZE = r[0];
int lMIN = r[1];
int lMAX = r[2];
Node rBST = find(right, r);
int rSIZE = r[0];
int rMIN = r[1];
int rMAX = r[2];
r[1] = Math.min(lMIN, data);
r[2] = Math.max(rMAX, data);
if (left==lBST && right==rBST && lMAX<data && data<rMIN) {
r[0] = rSIZE+lSIZE +1;
return root;
}
r[0] = Math.max(rSIZE, lSIZE);
return lSIZE>rSIZE?lBST:rBST;
}
}
2.实现二叉树三种遍历,要求:
时间复杂度O(N),空间复杂度O(1)
public class MorrisTree {
public static class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
left = null;
right = null;
}
}
public static ArrayList<Node> trees = new ArrayList<>();
public void buildTree(int[] array) {
for(int i=0;i<array.length;i++) {
trees.add(new Node(array[i]));
}
for(int index = 0;index<array.length/2-1;index++) {
trees.get(index).left = trees.get(index*2+1);
trees.get(index).right = trees.get(index*2+2);
}
int lastindex = array.length/2-1;
trees.get(lastindex).left = trees.get(lastindex*2+1);
if (array.length%2==1) {
trees.get(lastindex).right = trees.get(lastindex*2+2);
}
}
public static void morrisIn(Node head) {
if (head == null) {
return;
}
Node cur1 = head;
Node cur2 = null;
while (cur1 != null) {
cur2 = cur1.left;
if (cur2 != null) {
while (cur2.right != null && cur2.right != cur1) {
cur2 = cur2.right;
}
if (cur2.right == null) {
cur2.right = cur1;
cur1 = cur1.left;
continue;
} else {
cur2.right = null;
}
}
System.out.print(cur1.data + " ");
cur1 = cur1.right;
}
System.out.println();
}
public static void morrisPre(Node head) {
if (head == null) {
return;
}
Node cur1 = head;
Node cur2 = null;
while (cur1 != null) {
cur2 = cur1.left;
if (cur2 != null) {
while (cur2.right != null && cur2.right != cur1) {
cur2 = cur2.right;
}
if (cur2.right == null) {
cur2.right = cur1;
System.out.print(cur1.data + " ");
cur1 = cur1.left;
continue;
} else {
cur2.right = null;
}
} else {
System.out.print(cur1.data + " ");
}
cur1 = cur1.right;
}
System.out.println();
}
public static void morrisPos(Node head) {
if (head == null) {
return;
}
Node cur1 = head;
Node cur2 = null;
while (cur1 != null) {
cur2 = cur1.left;
if (cur2 != null) {
while (cur2.right != null && cur2.right != cur1) {
cur2 = cur2.right;
}
if (cur2.right == null) {
cur2.right = cur1;
cur1 = cur1.left;
continue;
} else {
cur2.right = null;
printEdge(cur1.left);
}
}
cur1 = cur1.right;
}
printEdge(head);
System.out.println();
}
public static void printEdge(Node head) {
Node tail = reverseEdge(head);
Node cur = tail;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.right;
}
reverseEdge(tail);
}
public static Node reverseEdge(Node from) {
Node pre = null;
Node next = null;
while (from != null) {
next = from.right;
from.right = pre;
pre = from;
from = next;
}
return pre;
}
public static void main(String[] args) {
int[] array = {3,6,5,9,10,7,29};
MorrisTree tree = new MorrisTree();
tree.buildTree(array);
tree.morrisIn(trees.get(0));
System.out.println();
tree.morrisPre(trees.get(0));
System.out.println();
tree.morrisPos(trees.get(0));
}
}
3.实现二叉树三种遍历(递归和非递归)
public class BinartTree {
public static class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
left = null;
right = null;
}
}
public static ArrayList<Node> trees = new ArrayList<>();
public void buildTree(int[] array) {
for(int i=0;i<array.length;i++) {
trees.add(new Node(array[i]));
}
for(int index = 0;index<array.length/2-1;index++) {
trees.get(index).left = trees.get(index*2+1);
trees.get(index).right = trees.get(index*2+2);
}
int lastindex = array.length/2-1;
trees.get(lastindex).left = trees.get(lastindex*2+1);
if (array.length%2==1) {
trees.get(lastindex).right = trees.get(lastindex*2+2);
}
}
public void preOrder(Node root) {
if (root==null) {
return;
}
System.out.print(root.data+" ");
preOrder(root.left);
preOrder(root.right);
}
public void preOrderNotrecur(Node root) {
Stack<Node> stack = new Stack<>();
if (root!=null) {
stack.push(root);
while (!stack.isEmpty()) {
root = stack.pop();
System.out.print(root.data+" ");
if (root.right!=null) {
stack.push(root.right);
}
if (root.left!=null) {
stack.push(root.left);
}
}
}
System.out.println("");
}
public void inOrder(Node root) {
if (root == null) {
return;
}
inOrder(root.left);
System.out.print(root.data+" ");
inOrder(root.right);
}
public void inOrderNotrecur(Node root) {
Stack<Node> stack = new Stack<>();
if (root!=null) {
while (!stack.isEmpty() || root!=null) {
if (root!=null) {
stack.push(root);
root=root.left;
} else {
root = stack.pop();
System.out.print(root.data+" ");
root = root.right;
}
}
}
System.out.println();
}
public void posOrder(Node root) {
if (root == null) {
return ;
}
posOrder(root.left);
posOrder(root.right);
System.out.print(root.data+" ");
}
public void posOrderNotrecur(Node root) {
Stack<Node> stack = new Stack<>();
if (root!=null) {
stack.push(root);
Node node = null;
while (!stack.isEmpty()) {
node=stack.peek();
if (node.left!=null && root!=node.left && root!=node.right) {
stack.push(node.left);
} else if (node.right!=null && root!=node.right) {
stack.push(node.right);
} else {
System.out.print(stack.pop().data+" ");
root = node;
}
}
}
System.out.println();
}
public static void main(String[] args) {
int[] array = {3,6,5,9,10,7,29};
BinartTree tree = new BinartTree();
tree.buildTree(array);
System.out.println("先序遍历递归实现");
tree.preOrder(trees.get(0));
System.out.println("");
System.out.println("先序遍历非递归实现");
tree.preOrderNotrecur(trees.get(0));
System.out.println();
System.out.println("中序遍历递归实现");
tree.inOrder(trees.get(0));
System.out.println();
System.out.println("中序遍历递归实现");
tree.inOrderNotrecur(trees.get(0));
System.out.println();
System.out.println("后序遍历递归实现");
tree.posOrder(trees.get(0));
System.out.println("");
System.out.println("后序遍历递归实现");
tree.posOrderNotrecur(trees.get(0));
}
}