练习题来自王道,部分题目与参考答案中的思路不同。
这些题主要是考察二叉树三种递归的遍历方法,三种非递归的遍历方法,以及层次遍历。
如有错误,欢迎指正!
/*
编写后序遍历二叉树的非递归算法
算法思想:提供一标签指针,在访问右孩子时判定,若已访问过输出节点
时间复杂度:O(n) 空间复杂度:O(n)
*/
void postOrder(BinaryTreeNode* t)
{
Stack s;
BinaryTreeNode* p = t;
BinaryTreeNode* pre;
if(p != NULL || !s.empty())
{
if(p != NULL)
{
s.push(p);
p = p->leftChild;
}else
{
p = s.top();
if(p->rightChild != NULL && p->rightChild != pre)
{
p = p->rightChild;
s.push(p);
p = p->leftChild;
}else
{
s.pop();
visit(p);
pre = p;
p = NULL;
}
}
}
}
/*
试给出二叉树的自上而下、从右到左的层次遍历算法
算法思想:层次遍历的节点出队列入栈,再依次出栈
时间复杂度:O(n) 空间复杂度:O(n)
*/
void reverseLevelOrder(BinaryTreeNode* t)
{
Queue q;
Stack s;
BinaryTreeNode* p;
q.push(t);
while(!q.empty())
{
p = q.pop();
s.push(p); // 压入栈
if(p->leftChild != NULL)
{
q.push(p->leftChild);
}
if(p->rightChild != NULL)
{
q.push(p->rightChild);
}
}
// 元素反向出栈
while(!s.empty())
{
p = s.pop();
visit(p);
}
}
/*
假设二叉树采用二叉链表存储结构,设计一个非递归算法求二叉树高度
*/
// 非递归算法求树的高度
int getHight(BinaryTreeNode<E>* t)
{
int res = -1;
int cur = 0;
bool isback = false;
Stack s;
BinaryTreeNode<E>* p = t;
BinaryTreeNode<E>* pre;
s.push(t);
while(p != NULL || !s.empty())
{
cur++;
if(p != NULL)
{
if(p->leftChild != NULL && isback == false)
{
p = p->leftChild;
s.push(p);
}else if(p->rightChild != NULL && pre != p->rightChild)
{
p = p->rightChild;
s.push(p);
isback = false;
}else
{
if(p->leftChild == NULL && p->rightChild == NULL) //到叶节点
{
if(res == -1 || cur > res)
res = cur;
}
pre = p;
if(s.empty())
{
return res;
}
p = s.pop();
if(pre == p)
p = s.pop();
cur = cur - 2;
isback = true;
}
}
}
return res;
}
/*
设一颗二叉树中各节点的值互不相同,其先序遍历序列和中序遍历序列分别存于
两个一维数组A[1...n]和B[1....n]中,试编写算法建立该二叉树的二叉链表。
算法思想:递归思想,将树循环递归为左子树和右子树
*/
BinaryTreeNode* CreateTree(ElemType A[], ElemType B[], int pre1, int pre2, int in1, int in2)
{
BinaryTreeNode* root = new BinaryTreeNode();
root->data = A[pre1]; //pre1,pre2分别是前序的头和尾元素下标,in1和in2分别是中序的头和尾元素下标,从1开始
for(int i = 1; B[i]!=root->data; i++); //找到中序遍历的相应元素下标
leftLen = i - pre1;
rightLen = pre2 - i;
// 精髓就在于如何分割
if(legtLen != 0)
{
root->leftChild = CreateTree(A, B, pre1+1, pre1+leftLen, in1, in1 + leftLen -1);
}else
{
root->leftChild = NULL;
}
if(rightLen != 0)
{
root->rightChild = CreateTree(A, B,pre2-rightLen+1, pre2, in1-rightLen+1, in2 );
}else
{
root->rightChild = NULL;
}
return root;
}
/*
二叉树按二叉树链表的形式存储,写一个判别给定二叉树是否是完全二叉树的算法
算法思想:计算树的高度和叶节点数目,完全二叉树有叶节点数目==2^(h-1)的关系
*/
bool isFullBinaryTree(BinaryTreeNode* t)
{
int h = t.getHight();
int count = 0;
int res;
Queue q;
BinaryTreeNode* p;
if(t == NULL)
return true;
q.push(t);
while(!q.empty())
{
p = q.pop();
if(p->leftChild != NULL)
{
q.push(p->leftChild);
}
if(p->rightChild != NULL)
{
q.push(p->rightChild);
}
if(p->leftChild == NULL && p->rightChild != NULL)
{
return false;
}
if(p->leftChild != NULL && p->rightChild == NULL)
{
return false;
}
if(p->leftChild == NULL && p->rightChild == NULL)
{
count++;
}
}
int res = pow(2, h-1);
if(count == res)
{
return true;
}else
{
return false;
}
}
/*
假设二叉树采用二叉链表存储结构,试设计一个算法,计算一颗给定二叉树的所有
双分支结点个数
算法思想:采用队列进行层次遍历,过程中记录双分支节点个数
*/
int binaryNodeCount(BinaryTreeNode* t)
{
binaryTreeNode* p;
Queue q;
q.push(t);
int res = 0;
while(!q.empty())
{
p = q.pop();
if(p->leftChild != NULL)
{
q.push(p->leftChild);
}
if(p->rightChild != NULL)
{
q.push(p->rightChild);
}
if(p->leftChild != NULL && p->rightChild != NULL)
{
res++;
}
}
return res;
}
/*
设数B是一颗采用链式结构存储的二叉树,编写一个把树B中的所有结点的左右子树
进行交换的函数
算法思想:采用后序遍历,每次返回祖先节点时交换左右指针
*/
void exchangeSubTree(BinaryTreeNode* t)
{
BinaryTreeNode* p;
if(t == NULL)
{
return;
}
exchangeSubTree(t->leftChild);
exchangeSubTree(t->rightChild);
p = t->leftChild;
t->leftChild = t->rightChild;
t->rightChild = p;
}
/*
假设二叉树采用二叉链存储结构,设计一个算法,求先序遍历序列中第k个
结点的值。
算法思想:啊这,非递归前序遍历+记录变量
*/
ElemType preFind(binaryNodeTree* t,int k)
{
BinaryTreeNode* p = t;
Stack s;
while(p != NULL || !s.empty())
{
if(p != NULL)
{
k--;
if(k == 0)
{
return p->data;
}
s.push(p);
p = p->leftChild;
}else
{
p = s.pop();
p = p->rightChild;
}
}
return NULL;
}
/*
已知二叉树已二叉链表存储,编写算法完成:对于树中每个元素值为x的结点,删去以它为根的子树,并释放相应的空间
算法思想:层次遍历,遍历至下一结点为空,算法结束.删除结点使用后序遍历
*/
void deleteX(BinaryTreeNode* t, ElemType x)
{
Queue q;
BinaryTreeNode* p;
q.push(t);
while(!q.empty())
{
p = q.pop();
if(p->data == x)// 使用递归删除
{
deletePostOrder(p);
}else
{
if(p->leftChild != NULL)
q.push(p->leftChild);
if(p->rightChild != NULL)
q.push(p->rightChild);
}
}
}
void deletePostOrder(BinaryTreeNode* t)
{
if(t != NULL)
{
deletePostOrder(t->leftChild);
deletePostOrder(t->leftChild);
delete t;
t = NULL;
}
}
/*
在二叉树中查找值为x的结点,试编写算法打印值为x的结点的所有祖先,
假设值为x的结点不多于1个。
算法思想:尝试使用后序遍历的递归算法打印
*/
int findDaddy(BinaryTreeNode* t, int x)
{
if(t == NULL)
return 0;
if(t->data == x)
{
cout << " --> "<< t->data ;
return 1;
}
int i = findDaddy(t->leftChild,x);
int j = findDaddy(t->rightChild,x);
if(i == 1 || j == 1)
{
cout << " --> "<< t->data ;
return 1;
}
return 0;
}
/*
假设二叉树以二叉树链表存储,root为指向该二叉树根结点的指针,p和q
为分别指向该二叉树中任意两个结点的指针,试编写算法ancestor(root,p,q,r)
,找到p和q的最近公共祖先节点r。
算法思想:接上一个的打印节点的祖先,对比祖先,不一样祖先的前一个便是共同祖先
用栈存储
*/
// 改写上面的findDaddy方法
void ancestor(BinaryTreeNode* root, BinaryTreeNode* p, BinaryTreeNode* q, BinaryTreeNode* r )
{
Stack sp;
Stack sq;
findAllDaddy(root, p, sp);
findAllDaddy(root, q, sq);
BinaryTreeNode* t1;
BinaryTreeNode* t2;
t1 = sp.pop();
sq.pop();
while(!sp.empty() && !sq.empty())
{
if(sp.top() == sq.top())
{
t1 = sp.pop();
sq.pop();
}else
{
break;
}
}
return t1;
}
int findAllDaddy(BinaryTreeNode* t, BinaryTreeNode* p,Stack &s)
{
if(t == NULL)
return 0;
if(t == p)
{
s.push(t);
return 1;
}
int i = findAllDaddy(t->leftChild, p, s);
int j = findAllDaddy(t->rightChild, p, s);
if(i == 1 || j == 1)
{
s.push(t);
return 1;
}
return 0;
}
/*
假设二叉树采用二叉链表存储结构,设计一个算法,求非空二叉树b的宽度(即具有节点
数最多的那一层的结点个数)
算法思想:层次遍历,返回结果变量,记录每层节点数目变量
记录从队列出的元素数量,根据上一层的节点数目判断出队列的元素是否到下一层
*/
int getWidth(BinaryTreeNode* t)
{
Queue* q;
BinaryTreeNode* p;
int res = 0;
int c = 1;
int layer = 1;//根节点算第一层,只有一个节点
q.push(t);
while(!q.empty())
{
layer--;
if(layer == 0)
{
layer = c;
if(res < c)
res = c;
c = 0;
}
p = q.pop();
if(p->leftChild != NULL)
{
q.push(p->leftChild);
c++;
}
if(p->leftChild != NULL)
{
q.push(p->leftChild);
c++;
}
}
return res;
}
/*
设有一颗满二叉树(所有结点值均不同),已知其先序序列为pre,设计一个算法
求其后序序列post
算法思想:递归算法,就像汉诺塔问题一样,基本操作只涉及三个元素
*/
// 假设元素从下标1开始存储
void preTopost(ElemType A[], int m, int n)
{
ElemType c;
c = A[m];
for(int i = m;i<n;i++)
{
A[i] = A[i+1];
}
A[n] = c;
if(n-m == 2)
return;
preTopost(A, m,(m+n)/2-1);
preTopost(A, (m+n)/2, n-1);
}
/*
设计一个算法将二叉树的叶节点按从左到右的顺序连成一个单链表,表头指针为head.
二叉树按二叉链表方式存储,链接时用叶结点的右指针域来存放单链表指针。
算法思想:非递归先序遍历,提供一指针保存叶节点的右指针域
*/
BinaryTreeNode* unionLeaf(BinaryTreeNode* t)
{
BinaryTreeNode* p = t;
BinaryTreeNode* head = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
BinaryTreeNode* pre = head;
Stack s;
while(p != NULL || !s.empty())
{
if(p != NULL)
{
s.push(p);
p = p->leftChild;
}else
{
p = s.pop();
if(p->leftChild == NULL && p->rightChild == NULL)
{
pre->rightChild = p;
pre = p;
}
p = p->rightChild;
}
}
return head;
}
/*
试设计判断两颗二叉树是否相似的算法。所谓二叉树T1和T2相似,指的是T1和T2都是空的二叉树或都
只有一个根节点;或T1的左子树和T2的左子树是相似的,且T1的右子树和T2的右子树是相似的。
算法思想:题目都是递归定义的,那一定是要用递归啦。
*/
bool resemble(BinaryTreeNode* t1, BinaryTreeNode* t2)
{
if(t1 == NULL && t2 == NULL)
return true;
if(t1 == t2 && resemble(t1->leftChild, t2->leftChild) && resemble(t1->rightChild, t2->rightChild))
return true;
else
return false;
}
/*
二叉树的带权路径长度(WPL)是二叉树中所有叶结点的带权路径长度之和。给定一颗
二叉树T,采用二叉树存储,结点结构为left:weight:right,weight非负。设root为
指向T的根结点指针,请设计求T的WPL的算法
算法思想:和上面的寻找共同祖先题目类似,只需要找到所有父节点并把data值相加就可
详细思想:
利用weight的非负特性,weight值最小为0.我们找到目标节点后将目标节点权值加一,
这样权值除了可以代表weight外还能代表其它分支是否找到T,没有则返回零。
我们使用递归算法,找到节点T后,依次回调函数将路径长度相加,因为没有找到的分支
weight是零,不影响最后的结果。
最后在最终结果上减去在T结点权值加上的1,便是WPL。
*/
int getWPL(BinaryTreeNode* root, BinaryTreeNode* T)
{
return findAllDaddy(root,T) - 1;
}
int findAllDaddy(BinaryTreeNode* t, BinaryTreeNode* p)
{
if(t == p)
{
return (T->weight + 1); // weight+1非负,所以判定为true
}
if(t != NULL)
{
int i = findAllDaddy(t->left);
int j = findAllDaddy(t->right);
if(i || j)
return T->weight + i + j;
}
return 0;
}
/*
请设计一个算法,将给定的表达式树转换为都能的中缀表达式,并通过括号反映操作符
的计算次序并输出。
算法思想:递归中序遍历
*/
void inOrder(BinaryTreeNode* t, int deep) // deep 输入为1
{
if(t != NULL)
{
if(deep > 1 && (t->leftChild != NULL || t->rightChild != NULL))
cout << " ( ";
inOrder(t->leftChild, deep + 1);
cout<<t->data;
inOrder(t->rightChild, deep + 1);
if(deep > 1 && t->leftChild != NULL || t->rightChild != NULL)
cout << " ) ";
}
}