树
1.树的遍历
public class TreePrint{
public class TreeNode{
int value;
Node left;
Node right;
public TreeNode(int data){
this.value=data;
}
}
public static void printPreorder1(TreeNode head){
if(head==null){
return;
}
System.out.println("head.value+" ");
prinPreorder1(head.left);
printPreorder(head.right);
}
public static void printInorder1(TreeNode head){
if(head==null){
return;
}
printInorder1(head.left);
System.out.println(head.value+" ");
printInorder1(head.right);
}
public static void printPostorder1(TreeNode head){
if(head==null){
return;
}
printPostorder1(head.left);
printPostorder1(head.right);
System.out.println(head.value+" ");
}
public static void preOrderUnRecur(TreeNode head){
if(head!=null){
Stack<TreeNode> stack=new Stack<>();
stack.add(head);
while(!stack.isEmpty()){
head=stack.pop();
System.out.println(head.value+" ");
if(head.right!=null){
stack.push(head.right);
}
if(head.left!=null){
stack.push(head.left);
}
}
}
}
public static void inOrderUnRecur(TreeNode head){
if(head!=null){
Stack<TreeNode> stack=new Stack<>();
stack.add(head);
while(!stack.isEmpty()||head!=null){
if(head!=null){
stack.push(head);
head=head.left;
}
else{
head=stack.pop();
System.out.println(head.value+" ");
head=head.right;
}
}
}
}
public static void posOrderUnRecur(Node head){
if(head!=null){
Stack<TreeNode> stack1=new Stack<>();
Stack<TreeNode> stack2=new Stack<>();
stack1.add(head);
while(!stack1.isEmpty()){
head=stack1.pop();
stack2.push(head);
if(head.left!=null){
stack1.push(head.left);
}
if(head.right!=null){
stack1.push(head.right);
}
}
while(!stack2.isEmpty()){
System.out.println(stack2.pop().value+"");
}
}
}
}
2.树的宽度优先遍历
public class LevelTravel{
public static void level(Node head){
if(head==null){
return;
}
Queue<Node> queue=new LinkedList<>();
queue.add(head);
while(!queue.isEmpty()){
Node cur=queue.poll();
System.out.println(cur.value);
if(cur.left!=null){
queue.add(cur.left);
}
if(cur.right!=null){
queue.add(cur.right);
}
}
}
}
3.树的最大宽度
public class MaxWidthUseMap{
public static int maxWidthUseMap(Node head){
if(head==null){
return 0;
}
Queue<Node> queue=new LinkedList<>();
queue.add(head);
HashMap<Node,Integer> levelMap=new HashMap<>();
levelMap.put(head,1);
int curlevel=1;
int curlevelNodes=0;
int max=0;
while(!queue.isEmpty()){
Node cur=queue.poll();
int curNodeLevel=levelMap.get(cur);
if(cur.left!=null){
levelMap.put(cur.left,curNodeLevel+1);
queue.add(cur.left);
}
if(cur.right!=null){
levelMap.put(cur.right,curNodeLevel+1);
queue.add(cur.right);
}
if(curNodeLevel==curlevel){
curlevelNodes++;
}
else{
max=Math.max(max,curleveNodes);
curlevel++;
curlevelNodes=1;
}
}
max=Math.max(max,curlevelNodes);
return max;
}
}
4.二叉树的后继结点
public class SuccessorNode{
public static Node getSuccessorNode(Node node){
if(node==null){
return node;
}
if(node.right!=null){
return getLeftMost(node.right);
}
else{
Node parent=node.parent;
while(parent!=null&&paren.left!=node){
node=parent;
parent=node.parent;
}
return parent;
}
}
}
5.二叉树的序列化及反序列化
public class SerialByPre{
public static String serialByPre(Node head){
if(head==null){
return "#!";
}
String res=head.value+"!";
res+=serialByPre(head.left);
res+=serialByPre(head.right);
return res;
}
public static Node reconByPreString(String preStr){
String[] values=preStr.spilt("!");
Queue<String> queue=new LinkedList<String>();
for(int i=0;i!=values.length;i++){
queue.add(values[i]);
}
return reconPreOrder(queue);
}
public static Node reconPreOrder(Queue<String> queue){
String value=queue.poll();
if(value.equals("#")){
return null;
}
Node head=new Node(Integer.valueOf(value));
head.left=reconPreOrder(queue);
head.right=reconPreOrder(queue);
return head;
}
}
6.前缀表达式
public class PreFixnum{
public static class TrieNode{
int path;
int end;
TrieNode[] nexts;
public TrieNode(){
path=0;
end=0;
nexts=new TrieNode[26];
}
}
public static class Trie{
TrieNode root;
public Trie(){
root=new TrieNode();
}
public void insert(String word){
if(word==null){
return;
}
char[] chs=word=.toCharArray();
TrieNode node=root;
int index=0;
for(int i=0;i<chs.length;i++){
index=chs[i]-'a';
if(node.nexts[index]==null){
node.nexts[index]=new TrieNode();
}
node=node.nexts[index];
node.path++;
}
node.end++;
}
public void delete(String word){
if(search(word)!=0){
char[] chs=word.toCharArray();
TrieNode node=root;
int index=0;
for(int i=0;i<chs.length;i++){
index=chs[i]-'a';
if(--node.nexts[index].path==0){
node.nexts[index]=null;
return;
}
node=node.nexts[index];
}
node.end--;
}
}
public int search(String word){
if(word==null){
return 0;
}
char[] chs=word.toCharArray();
TrieNode node=root;
int index=0;
for(int i=0;i<chs.length;i++){
index=chs[i]-'a';
if(node.nexts[index]==null){
return 0;
}
node=node.nexts[index];
}
return node.end;
}
public int prefixnum(String word){
if(word==null){
return 0;
}
char[] chs=word.toCharArray();
TrieNode node=root;
int index=0;
for(int i=0;i<chs.length;i++){
index=chs[i]-'a';
if(node.nexts[index]==null){
return 0;
}
node=node.nexts[index];
}
return node.path;
}
}
}
7.最大搜索二叉树
public class BiggestSubBSTInTree{
public static Node getManBST(Node head){
return process(head).maxBSTHead;
}
public static class ReturnType{
Node maxBSTHead;
int maxBSTSize;
int min;
int max;
public ReturnType(Node maxBSTHead,int maxBSTSize,int max,int min){
this.maxBSTHead=maxBSTHead;
this.maxBSTSize=maxBSTSize;
this.min=min;
this.max=max;
}
}
public static ReturnType process(Node X){
if(X==null){
return new ReturnType(null,0,Integer.MAX_VALUE,Integer.MIN_VALUE);
}
ReturnType lData=process(X.left);
ReturnType rData=process(X.right);
int min=Math.min(X.value,Math.min(lData.min,rData.min));
int max=Math.max(X.value,Math.max(lData.max,rData.max));
int maxBSTSize=Math.max(lData.maxBSTSize,rData.maxBSTSize);
Node maxBSTHead=lData.maxBSTSize>=rData.maxBSTSize?lData.maxBSTSize:rData.maxBSTSize;
if(lData.maxBSTHead==X.left&&rData.maxBSTHead==X.right&&X.value>lData.max&&X.value<rData.min){
maxBSTSize=lData.maxBSTSize+rData.maxBSTSize+1;
maxBSTHead=X;
}
return new ReturnType(maxBSTHead,maxBSTSize,max,min);
}
}
8.平衡树
public static boolean isBalanced(Node head){
return process(head).isBalanced;
}
public static class ReturnType{
boolean isBalanced;
int height;
ReturnType(boolean isBalanced,int height){
this.isBalanced=isBalanced;
this.height=height;
}
}
public static ReturnType process(Node head){
if(head=null){
return new ReturnType(true,0);
}
ReturnType leftData=process(head.left);
ReturnType rightData=process(head.right);
int height=Math.max(leftData.height,rightData.height)+1;
boolean isBalanced=leftData.isBalanced&&rightData.isBalanced&&Math.abs(leftData.height-rightData.height)<2;
return new ReturnType(isBalanced,height);
}