typedef int DataType;
typedef struct Node{
struct Node* firstChild;//长子节点;
struct Node* pNextBrother;//横向表示兄弟;
DataType data;
}
typedef struct TreeNode{
int val;
struct TreeNode *left;//左子树的根节点
struct TreeNode*right;//右子树的根节点;
}TreeNode;
typedef struct TreeNode{
int val;
struct TreeNode *left;//左子树的根节点
struct TreeNode*right;//右子树的根节点;
struct TreeNode*parent;//指向其双亲节点;
}TreeNofe;
void preorder(TreeNode*root){
if(root==NULL){
return ;
}
printf("%d",root->val);
preorder(root->left);
preorder(root->right);
}
void LevellorderTraversal(TreeNode*root){
//队列里面存节点地址;
if(root==NULL){
return ;
}
QueuePush(queue,root);
while(QueueEmpty(queue)){
TreeNode*front = QueueFront(queue);
QueuePop(queue);
printf("%c",front->val);
if(front->left!=NULL){
QueuePush(front->left);
}
if(front->right!= NULL){
QueuePush(front->right);
}
}
}
void TreeSize(TreeNode*root,int*size){
if(root==NULL){
return ;
}
TreeSize(root->left);
(*size)++;
TreeSize(root->right);
}//中序遍历思想
int TreeSize(TreeNode*root,){
if(root==NULL){
return 0;
}
return 1+TreeSize(root->left)+TreeSize(root->right);
}//递推思想
void LeafSize(TreeNode*root,int*size){
if(root==NULL){
return;
}
if(root->left==NULL&&root->rignt==NULL){
(*size)++:
}
LeafSize(root->left);
LeafSize(root->right);
}//添加判断条件遍历即可;
int LeafSize2(TreeNode*root){
if(root==NULL){
return 0;
}
if(root->left==NULL&&root->right==NULL){
return 1;
}
return LeafSize2(root->left)+LeafSize2(root->right);
}
int LevelSize(TreeNode*root,int k){
if(root==NULL){
return 0;
}
if(k==1){
return 1;
}
return LevelSize(root->left,k-1)+LevelSize(root->right,k-1);
}
TreeNode*Find(TreeNode*root,char x){
if(root==NULL){
return NULL;
}
if(root->val==x){
return root;
}
TreeNode*get_left = Find(root->left,x);
if(get_left!=NULL){
return get_left;
}
return Find(root->right,x);
}
bool IsCompleteBinaryTree(TreeNode*root){
if(root==NULL){
return true;
}
QueuePush(queue,root);
while(QueueEmpty(queue)){
TreeNode*front = QueueFront(queue);
QueuePop(queue);
if()front==NULL){
break;
}
QueuePush(front->left);
QueuePush(front->right);
}
while(QueueEmpty(queue)){
TreeNode *front=QueueFront(queue);;
QueuePop(queue);
if(front!=NULL){
return false;
}
}
}
void PreorderTraversalNor(TreeNode*root){
//前序
TreeNode *cur = root;
TreeNode*top;
TreeNode*last = NULL
std::stack<TreeNode*> st;
while(!st.empty()||cur!=NULL){
while (cur!=NULL){
//第一次访问节点:cur;
printf("%c",cur->val)//前序
st.push(cur);
cur = cur->left;
}
top = st.top();//从栈顶取元素
if(top->right!=NULL&&top->right!=last){
cur = top->right;//第二次访问节点:top;如果其右子节点被pop则说明到了第三次;
printf("%c",cur->val)//中序,注意在右边无子树二三一起进行,所以单独打印一次
}
if(top->right==NULL){
printf("%c",cur->val)//中序
}
else{
printf("%c",cur->val)//后序
st.pop();//第三次访问节点:top;
last= top;
}
}
}
已知[parent] | 已知[child] | ||
---|---|---|---|
左子树 | 2*[parent]+1 | 双亲节点 | ([child]-1)/2 |
右子树 | 2*[parent]+2 |
void AdjustDown(int array[],int Size,int root){
//因为为完全二叉树,所以没有左孩子一定没有有孩子;
while(1){
int left = 2*root+1;
if(left>=Size){
return;
}
//一定有左孩子,但不一定有有右孩子;
int right = 2*root+2;
int min = left
if(rightt<Size&&array[right]<array[left]){
min = right;
}
if(array[min]>array[root]){
return;
}
int t =array[min];
array[min] = array[root];
array[root] = t;
root = min;//继续向下调整;
}
}
typedef struct Heap{
int arrar[100];
int size;
}Heap;
//最后一个非叶子节点=最后一个节点的双亲节点;
void CreatHeap(int arry[],int size){
for(int i = ((size-1)-1)/2;i>=0;i--){
AdjustDown(array,size,i);
}
}//从最后一个非叶子节点开始向下调整;
void HeapInit(Heap*heap,int array[],int size){
memcpy(heap->array,array,size*sizeof(int));
heap->size = size;
CreatHeap(heap->arry,size);
}
向上调整
void AdjustUp(int arry[],int size,int child){
int i = child;
while (i){
int parent = (i-1)/2;
if(array[parent]<=array[i]){
return;
}
int temp = array[i];
array[i] = array[parent];
array[parent] = temp;
i = parent;
}
}
void HeapPush(Heap*heap,int val){
heap->array[heap->size++] = val;
AdjustUp(heap->array,heap->size,heap->size-1);
}
void HeapPop(Heap*heap){
heap->[0] = heap->array[heap->Size-1];
AdjustDown(heap->array,heap->Size-1,0);
heap->Size--
}
int HeapTop(Heap*heap){
return heap->array[0];
}
void HeapSort(int arr[],int size){
CreatHeap(arr,size);
for(int i =0;i<size-1;i++){
//得进行size-1次寻找;
int t = arr[0];
arr[0] = arr[size-i-1];
arr[size-i-1] = t;//交换堆顶和堆尾
AdjustDown(arr,size-i-1,0);//向下调整,数据规模-1;
}
}//O(n*logn)