Quicksort and height and width of binary tree

2016.10.05

 

Quicksort is an improvement on bubble sort .

Assuming that the array to be sorted is A[0]...A[N-1], first randomly select a data (usually the first number of the array) as the key data, and then put all the numbers smaller than it into it In the front, all numbers larger than it are put behind it, this process is called a quick sort. It is worth noting that quicksort is not a stable sorting algorithm , that is, the relative positions of multiple identical values ​​may change at the end of the algorithm.

A quick sort algorithm is:
1) Set two variables i, j, when sorting starts: i=0, j=N-1;
2) Use the first array element as the key data and assign it to the key, that is, key=A[0];
3) Search forward from j, that is, search forward from the back (j--), find the first value A[j] less than the key, and exchange A[j] and A[i];
4) Search backwards from i, that is, search backwards from the front (i++), find the first A[i] greater than the key, and exchange A[i] and A[j];
5) Repeat steps 3 and 4 until i=j; (In steps 3 and 4, no qualified value is found, that is, when A[j] in 3 is not less than key, and A[i] in 4 is not greater than key Change the values ​​of j and i so that j=j-1, i=i+1, until found. Find a value that meets the conditions, and the position of the i and j pointers remains unchanged when exchanging. In addition, i==j this The process must be exactly when i+ or j- completes, at which point the loop ends).
As shown in the figure:
       
class Quick
{
 public void sort(int arr[],int low,int high)
 {
 int l=low;
 int h=high;
 int povit=arr[low];
 
 while(l<h)
 {
 while(l<h&&arr[h]>=povit)
 h--;
 if(l<h){
 int temp=arr[h];
 arr[h]=arr[l];
 arr[l]=temp;
 l++;
 }
 
 while(l<h&&arr[l]<=povit)
 l++;
 
 if(l<h){
 int temp=arr[h];
 arr[h]=arr[l];
 arr[l]=temp;
 h--;
 }
 }
 print(arr);
 System.out.print("l="+(l+1)+"h="+(h+1)+"povit="+povit+"\n");
 if(l>low)sort(arr,low,l-1);
 if(h<high)sort(arr,l+1,high);
 }
}
 Interview Question: Depth of Binary Tree
Question: Enter the root node of a binary number in Lesson 1, and find the depth of the number. A path formed by one node from the root node to the leaf node, the length of the longest path is the depth of the tree. The depth of the root node is 1.

Disintegration idea:

    1. If the root node is empty, the depth is 0, return 0, the exit of the recursion

    2. If the root node is not empty, then the depth is at least 1, and then we find the depth of their left and right subtrees,

    3. Compare the depth values ​​of the left and right subtrees and return the larger one

    4. By recursive call  

       Code:


#include<iostream>
#include<stdlib.h>
using namespace std;

struct BinaryTreeNode
{
    int m_nValue;
    BinaryTreeNode* m_pLeft;
    BinaryTreeNode* m_pRight;
};

//create binary tree node
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
    BinaryTreeNode* pNode=new BinaryTreeNode();
    pNode->m_nValue=value;
    pNode->m_pLeft=NULL;
    pNode->m_pRight=NULL;
    return pNode;
}

//连接二叉树结点
void ConnectTreeNodes(BinaryTreeNode* pParent,BinaryTreeNode* pLeft,BinaryTreeNode* pRight)
{
    if(pParent!=NULL)
    {
        pParent->m_pLeft=pLeft;
        pParent->m_pRight=pRight;
    }
}

//求二叉树深度
int TreeDepth(BinaryTreeNode* pRoot)//计算二叉树深度
{
    if(pRoot==NULL)//如果pRoot为NULL,则深度为0,这也是递归的返回条件
        return 0;
    //如果pRoot不为NULL,那么深度至少为1,所以left和right=1
    int left=1;
    int right=1;
    left+=TreeDepth(pRoot->m_pLeft);//求出左子树的深度
    right+=TreeDepth(pRoot->m_pRight);//求出右子树深度

    return left>right?left:right;//返回深度较大的那一个
}

void main()
{
//            1
//         /      \
//        2        3
//       /\         \
//      4  5         6
//           /
//        7
    //创建树结点
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);

    //连接树结点
    ConnectTreeNodes(pNode1, pNode2, pNode3);
    ConnectTreeNodes(pNode2, pNode4, pNode5);
    ConnectTreeNodes(pNode3, NULL,   pNode6);
    ConnectTreeNodes(pNode5, pNode7,  NULL );

    int depth=TreeDepth(pNode1);
    cout<<depth<<endl;

    system("pause");
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326992175&siteId=291194637