算法知识整理(onenote)

折半查找

int Binsearch1(int r[], int n, int k) {
    
    
	int low = 0, hight = n - 1,mid=0;
	while (low <= hight) {
    
    
		mid = (hight + low) / 2;	
		if (r[mid]>k) hight = mid-1;      //避免死循环				
		else if(r[mid] < k)low = mid+1;	//避免死循环	
		else return mid;	
	}
	return 0;
}

二叉树

前序遍历

    public void preOrderTraversal(TreeNode root) {
    
    
      if (root == null) {
    
    
          return;
      }
      System.out.print(root.value + " ");
      preOrderTraversal(root.left);
      preOrderTraversal(root.right);
  }

中序遍历

   public void inOrderTraversal(TreeNode root) {
    
    
      if (root == null) {
    
    
          return;
      }
      inOrderTraversal(root.left);
      System.out.print(root.value + " ");
      inOrderTraversal(root.right);
  }

后序遍历

  void postOrderTraversal(TreeNode root) {
    
    
      if (root == null) {
    
    
          return;
      }
      postOrderTraversal(root.left);
      postOrderTraversal(root.right);
      System.out.print(root.value + " ");
  }

随机数生成

头文件:

#include<random>

#include<time.h>

播种语句:

srand((unsigned)time(NULL));

使用语句(rand()生成随机数):

a = rand() % b;

猜你喜欢

转载自blog.csdn.net/weixin_47656543/article/details/113778911