续上次查找父节点的队列版本。若要看之前的方法点击这里
代码如下:
myqueue.h
#include<stdio.h>
#include<stdlib.h>
struct TreeNode
{
TreeNode* _left;
TreeNode*_right;
int _data;
};
struct Node
{
TreeNode * data;
Node* next;
};
struct Queue
{
Node * rear;
Node * front;
};
void initQueue(Queue*q);
bool isQueueEmpty(Queue *q);
void enQueue(Queue* q,TreeNode* ch);
TreeNode* deQueue(Queue*q);
void resetQueue(Queue* q);
void destroyQueue(Queue*q);
myqueue.cpp
#include"myqueue.h"
void initQueue(Queue*q)
{
q->front = q->rear = (Node*)malloc(sizeof(Node));
q->front->next = NULL;
}
bool isQueueEmpty(Queue *q)
{
return q->front == q->rear;
}
void enQueue(Queue* q,TreeNode* ch)
{
Node* cur = (Node*)malloc(sizeof(Node));
cur->data = ch;
cur->next =NULL;
q->rear->next = cur;
q->rear = cur;
}
TreeNode* deQueue(Queue*q)
{
TreeNode* ch = q->front->next->data;
if(q->front->next == q->rear)
{
q->rear = q->front;
free(q->front->next);
q->front->next = NULL;
}
else
{
Node* cur = q->front->next;
q->front->next = cur->next;
free(cur);
cur = NULL;
}
return ch;
}
void resetQueue(Queue* q)
{
Node* head = q->front->next; //不能用isQueueEmpty和deQueue,如果用这两个,那么
q->front->next = NULL; //就是让front一直往前走,而不是复位
q->rear = q->front;
Node* ch;
while(head)
{
ch =head->next;
free(head);
head = ch;
}
}
void destroyQueue(Queue*q)
{
resetQueue(q);
free(q->front);
q->front = q->rear =NULL;
}
main函数
#include <iostream>
#include"myqueue.h"
using namespace std;
//递归,中序,左根右
void midOrderRecursive(TreeNode* r)
{
if(r)
{
midOrderRecursive(r->_left);
printf("%d ", r->_data);
midOrderRecursive(r->_right);
}
}
//BST插入, 迭代版
void insertBstIteartor(TreeNode** r,int ch)
{
TreeNode * t = *r;
if((*r) == NULL) //根
{
(*r) = (TreeNode*)malloc(sizeof(TreeNode));
(*r)->_data = ch;
(*r)->_left = (*r)->_right = NULL;
}
else
{
while(1)
{
if(ch > t->_data)
{
if(t->_right == NULL)
{
TreeNode* cur = (TreeNode*)malloc(sizeof(TreeNode));
t->_right = cur; //cur赋给t的右边,不可颠倒
cur->_data = ch;
cur->_left = cur->_right =NULL;
return;
}
else
t = t->_right;
}
else
{
if(t->_left == NULL)
{
TreeNode* cur = (TreeNode*)malloc(sizeof(TreeNode));
t->_left =cur;
cur->_data = ch;
cur->_left = cur->_right = NULL;
return;
}
else
t = t->_left;
}
}
}
}
//查找,迭代版
TreeNode* searchBstIterator(TreeNode*r,int find)
{
while(r)
{
if(r->_data == find)
return r;
else if(find > r->_data)
r=r->_right;
else
r=r->_left;
}
return NULL;
}
//查找父节点,队列版
TreeNode* getParentBst(TreeNode * r,TreeNode * child)
{
Queue q;
TreeNode* ch;
if(r)
{
initQueue(&q); //初始化
enQueue(&q,r); //入队
while(!isQueueEmpty(&q)) //如果队列不为空
{
ch = deQueue(&q);
if((ch->_left && ch->_left->_data == child->_data)
|| (ch->_right && ch->_right->_data == child->_data))
return ch;
else
{
if(ch->_left)
enQueue(&q,ch->_left);
if(ch->_right)
enQueue(&q,ch->_right);
}
}
}
return NULL;
}
int main()
{
TreeNode *root=NULL; //树的初始化
insertBstIteartor(&root,30); //利用例子建造树
insertBstIteartor(&root,8);
insertBstIteartor(&root,15);
insertBstIteartor(&root,36);
insertBstIteartor(&root,100);
insertBstIteartor(&root,32);
midOrderRecursive(root);
putchar(10);
TreeNode* search = searchBstIterator(root,36);
if(search != NULL)
{
printf("Find it , it is %d\n",search->_data);
TreeNode *parent = getParentBst(root,search);
if(parent != NULL)
printf("parent is %d\n",parent->_data);
else
printf("no parent");
}
else
printf("no find");
return 0;
}