Eighty-eight. Binary tree traversal, heap sort (search and sort (3))-JAVA

Search and Sort (1)
Search and Sort (2)
Search and Sort (4)
Traversal of Binary Tree
Insert picture description here
Insert picture description here
Insert picture description here

import java.util.Scanner;

public class LianXi {
    
    
	
	//前序遍历
	public static void preOrder(int []arr, int i){
    
    
		if(i >= arr.length)
			return;
		System.out.print(arr[i] + " ");   //输出根节点
		preOrder(arr, 2 * i + 1);    //递归输出左子树
		preOrder(arr, 2 * i + 2);    //递归输出右子树
	}
	
    //中序遍历
	public static void inOrder(int []arr, int i){
    
    
		if(i >= arr.length)
			return;
		inOrder(arr, 2 * i + 1);     //递归输出左子树
		System.out.print(arr[i] + " ");  //输出根节点
		inOrder(arr, 2 * i + 2);     //递归输出右子树
	}
	
	//后序遍历
	public static void postOrder(int []arr, int i){
    
    
		if(i >= arr.length)
			return;
		postOrder(arr, 2 * i + 1);   //递归输出左子树
		postOrder(arr, 2 * i + 2);   //递归输出右子树
		System.out.print(arr[i] + " ");  //输出根节点
	}
	
	public static void main(String[] args){
    
    
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int []arr = new int[N];
		for(int i = 0 ; i<N; i++){
    
    
			 arr[i] = in.nextInt();
		}
		System.out.println("初始数组为:");
		for(int i = 0 ; i<N; i++){
    
    
			System.out.print(arr[i] + " ");
		}
		System.out.println("\n");
		System.out.println("前序遍历:");
		preOrder(arr, 0);
		System.out.println("\n");
		System.out.println("中序遍历:");
		inOrder(arr, 0);
		System.out.println("\n");
		System.out.println("后序遍历:");
		postOrder(arr, 0);
	}
}

Insert picture description here
heap

Heap concept

  • A binary heap is a complete binary tree or a nearly complete binary tree.
  • Binary heap satisfies two characteristics:
    1. The key value of the parent node is always greater than or equal to (less than or equal to) the key value of any child node.
    2. The left and right subtrees of each node are a binary heap (both are the largest heap or the smallest heap)
  • The value of any node is greater than the value of its child nodes-big top heap
  • The value of any node is less than the value of its child nodes-small top heap

Insert picture description here
大顶堆:arr[i] >= arr[2i+1] && arr[i] >= arr[2i+2]
小顶堆:arr[i] <= arr[2i+1] && arr[i] <= arr[2i+2]

Heap Sorting
The basic idea of ​​heap sorting is: construct the sequence to be sorted into a large top heap, at this time, the maximum value of the entire sequence is the root node of the top of the heap. Exchange it with the last element, and the end is the maximum value at this time. Then reconstruct the remaining n-1 elements into a pile, so that the next smallest value of n elements will be obtained. Repeated execution like this, you can get an ordered sequence

Step
1. Construct the initial heap. Construct a given disordered sequence into a large top pile (generally, a large top pile is used for ascending order, and a small top pile is used for descending order).
2. Exchange the top element and the end element to make the end element the largest. Then continue to adjust the heap, and then swap the top element with the end element to get the second largest element. Exchange, rebuild, and exchange are repeated in this way.

Minimum heap sort

import java.util.Scanner;

public class LianXi {
    
    
   /*	 
    * MinHeap(A){
    *    n = A.length;
    *    for i from n/2-1 down to 0{
    *        MinHeapFixDown(A,i,n);
    *    }
    * }
    * MinHeapFixDown(A,i,n){
    *  // 找到左右孩子
    *  left = 2 * i + 1;
    *  right = 2 * i + 2;
    *  //左孩子已经越界,i就是叶子节点
    *  if(left>=n){
    *     return; 
    *  }
    *  
    *  min = left;
    *  if(right >= n){
    *     min = left;
    *  }
    *  else{
    *     if(A[right] < A[left])
    *       min = right;
    *  
    * }
    * //min指向了左右孩子中较小的那个
    * //如果A[i]比两个孩子都要小,不用调整
    * if(A[i] <= A[min]){
    *    return ;
    * }
    * // 否则,找到两个孩子中较小的,和i交换
    * swap(A,i,min)
    * //小孩子那个位置的值发生了变化,i变更为小孩子那个位置,递归调整
    * MinHeapFixDown(A,min,n);
    * 
    * sort(A):
    * //先对A进行堆化
    * MinHeap(A);
    * for(int x = n-1; x>=0; x--)
    *    //把堆顶,0号元素和最后一个元素对调
    *    swap(A,0,x);
    *    //缩小堆的范围,对堆顶元素进行向下调整
    *    MinHeapFixDown(A,0,x-1)
   */
	
	public static void MinHeap(int []A){
    
    
		int n = A.length;
		for(int i = n/2-1; i>=0; i--){
    
    
			MinHeapFixDown(A, i, n);
		}
	}  
	
	public static void MinHeapFixDown(int []A, int i, int n){
    
    
		int left = 2 * i + 1;
		int right = 2 * i + 2;
		if(left >= n){
    
    
			return ;
		}
		int min = left;
		if(right >= n){
    
    
			min = left;
		}
		else{
    
    
			if(A[right] < A[left]){
    
    
				min = right;
			}
		}
		if(A[i] <= A[min]){
    
    
			return ;
		}
		swap(A,i,min);
		MinHeapFixDown(A, min, n);
	}
	
	public static void sort(int []A){
    
    
		MinHeap(A);
		for(int x = A.length-1; x>=0; x--){
    
    
			swap(A,0,x);
			MinHeapFixDown(A, 0, x-1);
		}
	}
	public static void swap(int[]A, int i, int min){
    
    
		int temp = A[i];
		A[i] = A[min];
		A[min] = temp;
	}
	
	public static void main(String[] args){
    
    
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int []A = new int[N];
		for(int i = 0; i<N; i++){
    
    
			A[i] = in.nextInt();
		}
		System.out.println("初始数组为:");
		for(int i = 0; i<N; i++){
    
    
			System.out.print(A[i]+ " ");
		}
		System.out.println("\n");
		sort(A);
		System.out.println("最小堆排序后数组为:");
		for(int i = 0; i<N; i++){
    
    
			System.out.print(A[i] + " ");
		}
	}
}

Insert picture description here

Guess you like

Origin blog.csdn.net/JiangYu200015/article/details/113770464