您的位置:首页 > Web前端

【剑指offer】第八题(二叉树的下一个结点) 和 第九题(用两个栈实现队列)

2018-03-16 16:13 489 查看
第八题:二叉树的下一个结点

        题目:

给定一颗二叉树和其中的一个结点,如何找出中序遍历的下一个结点?树中的结点除了有两个分别指向左、右子节点的指针,还有一个指向父节点的指针。

/*************************************************************************
> File Name: NextNodeInBinaryTree.cpp
> Author: arrayli
> Mail: 1374367549@qq.com
> Created Time: 2018年03月14日 星期三 20时56分02秒
************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stack>
using namespace std;

typedef struct _BinaryTreeNode
{
int m_cValue;
struct _BinaryTreeNode *m_pLeft;
struct _BinaryTreeNode *m_pRight;
struct _BinaryTreeNode *m_pParent;
}BinaryTreeNode;

BinaryTreeNode * GetNext(BinaryTreeNode *pNode)
{
if(pNode == NULL)
return NULL;

BinaryTreeNode *pNext = NULL;

if( pNode->m_pRight != NULL)
{
BinaryTreeNode *pRight = pNode->m_pRight;
while( pRight->m_pLeft != NULL )
pRight = pRight->m_pLeft;

pNext = pRight;
}
else if( pNode->m_pParent != NULL )
{
BinaryTreeNode *pCurrent = pNode;
BinaryTreeNode *pParent = pNode->m_pParent;

while( pParent != NULL && pCurrent == pParent->m_pRight )
{
pCurrent = pParent;
pParent = pParent->m_pParent;
}
pNext = pParent;
}

return pNext;
}

// 销毁二叉树

void DestoryBinaryTree(BinaryTreeNode *root)
{
if( root != NULL )
{
BinaryTreeNode * pLeft = root->m_pLeft;
BinaryTreeNode * pRight = root->m_pRight;

free(root);
root = NULL;

DestoryBinaryTree(pLeft);
DestoryBinaryTree(pRight);
}
}

// 递归中序遍历二叉树

void Recursion_InOrder( BinaryTreeNode *root )
{
if( root == NULL )
return;

Recursion_InOrder(root->m_pLeft);
printf("%d\t",root->m_cValue);
Recursion_InOrder(root->m_pRight);

}

// 非递归中序遍历二叉树

void InOrder( BinaryTreeNode *root )
{
stack<BinaryTreeNode*> s;

BinaryTreeNode *pNode = root;

while( pNode != NULL || !s.empty() )
{
if( pNode != NULL )
{
s.push(pNode);
pNode = pNode->m_pLeft;
}

if( !s.empty() )
{
pNode = s.top();
s.pop();
printf("%d\t",pNode->m_cValue);
pNode = pNode->m_pRight;
}

}
}

// 二叉树插入节点

void InsertBinaryNode(BinaryTreeNode *root,int ch)
{
if(root->m_cValue > ch)
{
if(root->m_pLeft == NULL)
{
BinaryTreeNode *pNode = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
if( NULL == pNode )
{
printf("malloc root failed!\n");
return ;
}
pNode->m_pLeft = pNode->m_pRight = NULL;
pNode->m_cValue = ch;
root->m_pLeft = pNode;
}
else
{
InsertBinaryNode(root->m_pLeft,ch);
}

}
else
{
if(root->m_pRight == NULL)
{
BinaryTreeNode *pNode = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
if( NULL == pNode )
{
printf("malloc root failed!\n");
return ;
}
pNode->m_pLeft = pNode->m_pRight = NULL;
pNode->m_cValue = ch;
root->m_pRight = pNode;
}
else
{
InsertBinaryNode(root->m_pRight,ch);
}
}
}

// 创建二叉树

BinaryTreeNode *CreatBinaryTree( void )
{

int ch;
BinaryTreeNode *root = NULL;
BinaryTreeNode *pNode = NULL;

scanf("%d",&ch);
while( ch != -1 )
{
if( root == NULL )
{
pNode = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
if( pNode == NULL )
{
printf("malloc root failed!\n");
return NULL;
}
pNode->m_pLeft = pNode->m_pRight = NULL;
pNode->m_cValue = ch;
root = pNode;
}
else
{
InsertBinaryNode(pNode,ch);
}
scanf("%d",&ch);

}

return root;
}

int main(void)
{
BinaryTreeNode *root = NULL;

root = CreatBinaryTree();

Recursion_InOrder(root);
printf("\n");

BinaryTreeNode *pNew = NULL;
pNew = GetNext(root);
printf("%d\n",pNew->m_cValue);

DestoryBinaryTree(root);
return 0;
}


第九题:用两个栈实现队列

题目:

用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能。



解题程序如下:

/*************************************************************************
> File Name: QueueWithTwoStacks.cpp
> Author: arrayli
> Mail: 1374367549@qq.com
> Created Time: 2018年03月14日 星期三 23时19分03秒
************************************************************************/

#include<iostream>
#include<stdio.h>
#include<stack>
#include<exception>

using namespace std;

template <typename T> class CQueue
{
public:
CQueue(void);
~CQueue(void);

void appendTail(const T & node);
T deleteHead();
private:
stack<T> stack1;
stack<T> stack2;

};

template<typename T> CQueue<T>::CQueue(void)
{

}

template<typename T> CQueue<T>::~CQueue(void)
{

}

template<typename T> void CQueue<T>::appendTail(const T &node)
{
stack1.push(node);
}
template<typename T> T CQueue<T>::deleteHead(void)
{

if(stack2.size()<=0)
{
while(stack1.size()>0)
{
T & data = stack1.top();
stack1.pop();
stack2.push(data);
}
}

if(stack2.size() == 0)
{
printf("queue is empty!\n");
T temp = (int) 1;
return temp;
}

T head = stack2.top();
stack2.pop();
return head;
}

void test()
{
CQueue<int> q;

q.appendTail(1);
q.appendTail(2);
q.appendTail(3);
q.appendTail(4);

int temp = q.deleteHead();

printf("%d\t",temp);

temp = q.deleteHead();
printf("%d\t",temp);

temp = q.deleteHead();
printf("%d\t",temp);

temp = q.deleteHead();
printf("%d\t",temp);

temp = q.deleteHead();
printf("%d\t",temp);
}

int main(void)
{
test();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: