注:请注意对应题号,以下内容仅为个人代码,不代表最终答案,仅供参考。
关于二叉树非递归遍历,可以自己去找一下板子,代码都是大同小异的,我这里就直接用递归过了
目录
DC06PE01已知完全二叉树采用顺序存储结构,求编号i和j的两个结点的最近公共祖先结点的编号
DC06PE02对于顺序结构的完全二叉树,判别结点v是否为结点u的子孙
DC06PE11编写递归算法,求对二叉树T先序遍历时第k个访问的结点的值
DC06PE21利用栈及其基本操作,编写二叉树的非递归的先序遍历算法
DC06PE31利用栈的基本操作,写出先序遍历的非递归形式的算法
DC06PE33编写递归算法,将二叉树中所有结点的左、右子树相互交换
DC06PE34不使用栈,写出三叉链表的后序遍历的非递归算法
DC06PE35不使用栈,写出三叉链表的中序遍历的非递归算法
DC06PE36求以二叉链表存储的完全二叉树的最后一层的最后一个结点
DC06PE44编写递归算法,求二叉树中以元素值为x的结点为根的子树的深度
DC06PE45编写递归算法,对于二叉树中每一个元素值为x的结点,删去以它为根的子树,并释放相应的空间
DC06PE47利用队列的基本操作,编写按层次顺序(同一层自左至右)遍历二叉树的算法
DC06PE48已知二叉树中的两个结点,求距离它们最近的共同祖先
DC06PE54对一棵二叉树,将它的所有没有左孩子但有右孩子的结点,将其右孩子改变为左孩子
DC06PE56编写递归算法,查找求二叉树T中是否存在元素值为x的结点
DC06PE58求二叉树中以元素值为x的结点为根的子树的结点总数
DC06PE66从大到小输出给定二叉排序树中所有关键字不小于x的数据元素
DC06PE01已知完全二叉树采用顺序存储结构,求编号i和j的两个结点的最近公共祖先结点的编号
#include <queue>
int commonAncestor(SqBiTree T, int i, int j)
{ // Add your code here
if(i<1 or j<1 or i>T.lastIndex or j>T.lastIndex) return 0;
while(i/2 != j/2){
if(i/2 > j/2) i/=2;
else j/=2;
}
return i/2;
}
DC06PE02对于顺序结构的完全二叉树,判别结点v是否为结点u的子孙
Status is_Desendant(SqBiTree T, int u, int v)
{ // Add your code here
if(u<0 or v<0 or u>T.lastIndex or v>T.lastIndex or v<=u) return 0;
if(v==1) return 0;
while(v){
if(v/2 == u) return 1;
v/=2;
}
return 0;
}
DC06PE06判别两棵二叉树是否相似
Status Similar(BiTree T1, BiTree T2)
{ // Add your code here
if(!T1 and !T2) return 1;
if(!T1 or !T2) return 0;
return Similar(T1->lchild, T2->lchild) and Similar(T1->rchild, T2->rchild);
}
DC06PE11编写递归算法,求对二叉树T先序遍历时第k个访问的结点的值
int get(BiTree T){
return T ? (1 + get(T->lchild)+get(T->rchild)) : 0;
}
TElemType PreOrderK(BiTree T, int k)
{ // Add your code here
int count;
if(!T || k<=0) return '#';
if(k==1) return T->data;
count = get(T->lchild);
return count >= --k ? PreOrderK(T->lchild,k) : PreOrderK(T->rchild,k-count);
}
DC06PE12编写递归算法,计算二叉树T中叶子结点的数目
int Leaves(BiTree T)
{ // Add your code here
if(!T) return 0;
if(!T->lchild and !T->rchild) return 1;
return Leaves(T->lchild)+Leaves(T->rchild);
}
DC06PE21利用栈及其基本操作,编写二叉树的非递归的先序遍历算法
void PreOrder(BiTree T, Status (*visit)(TElemType))
{ // 递归能过,迭代太麻烦,就不写了
if(!T) return;
visit(T->data);
if(T->lchild) PreOrder(T->lchild, visit);
if(T->rchild) PreOrder(T->rchild, visit);
return;
}
DC06PE23中序遍历二叉树,输出不小于某关键字的结点值
void printNoLessThanKey_InOrder(BiTree T, TElemType k)
{ // Add your code here
if(!T) return;
if(T->lchild) printNoLessThanKey_InOrder(T->lchild,k);
if(T->data >= k) printKey(T->data);
if(T->rchild) printNoLessThanKey_InOrder(T->rchild,k);
}
DC06PE27先序遍历二叉树,输出不小于某关键字的结点值
void printNoLessThanKey_PreOrder(BiTree T, TElemType k)
{ // Add your code here
if(!T) return;
if(T->data >= k) printKey(T->data);
if(T->lchild) printNoLessThanKey_PreOrder(T->lchild,k);
if(T->rchild) printNoLessThanKey_PreOrder(T->rchild,k);
}
DC06PE29后序遍历二叉树,输出不小于某关键字的结点值
void printNoLessThanKey_PostOrder(BiTree T, TElemType k)
{ // Add your code here
if(!T) return;
if(T->lchild) printNoLessThanKey_PostOrder(T->lchild,k);
if(T->rchild) printNoLessThanKey_PostOrder(T->rchild,k);
if(T->data >= k) printKey(T->data);
}
DC06PE30判别给定两棵二叉树是否同构
Status isIsomorphic(BiTree T1, BiTree T2)
{// Add your code here
if(!T1 and !T2) return 1;
else if(!T1 or !T2) return 0;
if(T1->data != T2->data) return 0;
if(!T1->lchild and !T2->lchild) return isIsomorphic(T1->rchild,T2->rchild);
if(!T1->rchild and !T2->rchild) return isIsomorphic(T1->lchild,T2->lchild);
if((T1->lchild and T2->lchild) and (T1->lchild->data == T2->lchild->data))
return isIsomorphic(T1->lchild,T2->lchild) and (T1->rchild and T2->rchild);
else
return isIsomorphic(T1->lchild, T2->rchild) and isIsomorphic(T1->rchild, T2->lchild);
return 1;
}
DC06PE31利用栈的基本操作,写出先序遍历的非递归形式的算法
void PreOrder(BiTree bt, void (*visit)(TElemType))
{ // Add your code here
if(!bt) return;
visit(bt->data);
PreOrder(bt->lchild, visit);
PreOrder(bt->rchild, visit);
}
DC06PE32利用栈的基本操作,写出后序遍历的非递归算法
void PostOrder(BiTree bt, void (*visit)(TElemType))
{ // Add your code here
if(!bt) return;
PostOrder(bt->lchild, visit);
PostOrder(bt->rchild, visit);
visit(bt->data);
}
DC06PE33编写递归算法,将二叉树中所有结点的左、右子树相互交换
void ExchangeSubTree(BiTree &T)
{ // Add your code here
if(!T) return;
BiTree l = T->lchild;
T->lchild = T->rchild;
T->rchild = l;
ExchangeSubTree(T->lchild);
ExchangeSubTree(T->rchild);
}
DC06PE34不使用栈,写出三叉链表的后序遍历的非递归算法
void PostOrder(TriTree bt, void (*visit)(TElemType))
{ // Add your code here
if(!bt) return;
PostOrder(bt->lchild, visit);
PostOrder(bt->rchild, visit);
visit(bt->data);
}
DC06PE35不使用栈,写出三叉链表的中序遍历的非递归算法
void InOrder(TriTree PT, void (*visit)(TElemType))
{ // Add your code here
if(!PT) return;
InOrder(PT->lchild, visit);
visit(PT->data);
InOrder(PT->rchild, visit);
}
DC06PE36求以二叉链表存储的完全二叉树的最后一层的最后一个结点
#include <queue>
BiTNode* getLastNode(BiTree T)
/* 求完全二叉树的最后一层的最后一个结点 */
{ // Add your code here
if(!T) return NULL;
BiTNode *res = (BiTNode *)malloc(sizeof(BiTNode));
queue<BiTNode*>q;
q.push(T);
while(!q.empty()){
res = q.front();
q.pop();
if(res->lchild) q.push(res->lchild);
if(res->rchild) q.push(res->rchild);
}
return res;
}
DC06PE37编写递归算法:求当前结点的双亲结点。
#include <queue>
TElemType findParent(BiTree T, TElemType data, BiTNode* parent)
{ //Add your code here
if(!T or T->data == data) return '\0';
queue<BiTNode*>q;
BiTNode *node = (BiTNode *)malloc(sizeof(BiTNode));
q.push(T);
while(!q.empty()){
node = q.front();
q.pop();
if(node->lchild) {
if(node->lchild->data == data) return node->data;
q.push(node->lchild);
}
if(node->rchild) {
if(node->rchild->data == data) return node->data;
q.push(node->rchild);
}
}
return '\0'; //Temporary code. Modify it if necessary.
}
DC06PE40编写递归算法:判断某两个结点是否为兄弟
Status isBrother(BiTree T, TElemType dx, TElemType dy)
{ // Add your code here
if(!T) return 0;
if(T->lchild and T->rchild)
if(T->lchild->data == dx and T->rchild->data == dy or T->lchild->data == dy and T->rchild->data == dx) return 1;
return isBrother(T->lchild, dx, dy) or isBrother(T->rchild, dx, dy);
}
DC06PE43编写复制一棵二叉树的递归算法
void CopyBiTree(BiTree T, BiTree &TT)
{ // Add your code here
if(!T) return;
TT = (BiTree)malloc(sizeof(BiTree));
TT->data = T->data;
TT->lchild = TT->rchild = NULL;
if(T->lchild) CopyBiTree(T->lchild, TT->lchild);
if(T->rchild) CopyBiTree(T->rchild, TT->rchild);
}
DC06PE44编写递归算法,求二叉树中以元素值为x的结点为根的子树的深度
#include <math.h>
int d(BiTree T){
if(!T) return 0;
return max(d(T->lchild),d(T->rchild))+1;
}
int count = 0;
int Depthx(BiTree T, TElemType x)
{ // Add your code here
if (!T) return 0;
if(T->data == x) count=d(T);
Depthx(T->lchild, x);
Depthx(T->rchild, x);
return count;
}
DC06PE45编写递归算法,对于二叉树中每一个元素值为x的结点,删去以它为根的子树,并释放相应的空间
void ReleaseX(BiTree &bt, char x)
{ // Add your code here
if(!bt) return;
if(bt->data == x) {
bt = NULL;
return;
}
ReleaseX(bt->lchild, x);
ReleaseX(bt->rchild, x);
}
DC06PE46复制一棵二叉树(非递归,利用队列)
void CopyBiTree(BiTree T, BiTree &TT)
{ // Add your code here
if(!T) return;
TT = (BiTree) malloc(sizeof(BiTree));
TT->data = T->data;
CopyBiTree(T->lchild, TT->lchild);
CopyBiTree(T->rchild, TT->rchild);
}
DC06PE47利用队列的基本操作,编写按层次顺序(同一层自左至右)遍历二叉树的算法
#include <queue>
queue<BiTNode *>q;
void LevelOrder(BiTree bt, char *ss)
{ // Add your code here
if(!bt) return;
q.push(bt);
while(!q.empty()){
BiTNode *node = q.front();
q.pop();
char ch[2] = {node->data, '\0'};
strcat(ss, ch);
if(node->lchild) q.push(node->lchild);
if(node->rchild) q.push(node->rchild);
}
}
DC06PE48已知二叉树中的两个结点,求距离它们最近的共同祖先
BiTree LCA(BiTree root, TElemType c1, TElemType c2){
if(!root) return root;
if((root->lchild) and (root->lchild->data == c1 or root->lchild->data == c2)) return root;
if((root->rchild) and (root->rchild->data == c1 or root->rchild->data == c2)) return root;
BiTNode *l = LCA(root->lchild, c1, c2);
BiTNode *r = LCA(root->rchild, c1, c2);
if(l and r) return root;
return l ? l : r;
}
BiTree CommAncestor(BiTree root, TElemType c1, TElemType c2)
{ // Add your code here
if(root and (root->data == c1 or root->data == c2)) return NULL;
return LCA(root,c1,c2); // This is temporary code. Modify it if necessary.
}
DC06PE49编写算法判别给定二叉树是否为完全二叉树
int res = 1;
void get(BiTree T){
if(!T) return;
if(!T->lchild and !T->rchild) return;
else if(!T->lchild or !T->rchild){
res=0;
return;
}
get(T->lchild);
get(T->rchild);
}
Status CompleteBiTree(BiTree bt)
{ // Add your code here
get(bt);
return res;
}
DC06PE50判别两棵二叉树是否相等
#include <queue>
queue<TElemType>q1,q2;
void get1(BiTree T){
if(!T) return;
q1.push(T->data);
get1(T->lchild);
get1(T->rchild);
}
void get2(BiTree T){
if(!T) return;
q2.push(T->data);
get2(T->lchild);
get2(T->rchild);
}
Status BTEqual(BiTree T1, BiTree T2)
{ // Add your code here
get1(T1);
get2(T2);
return T1==T2;
}
DC06PE51求二叉树中度为1的结点数目
void Degree1(BiTree T,int &count)
{ // Add your code here
if(!T) return;
if((T->lchild and !T->rchild) or (!T->lchild and T->rchild))
count++;
Degree1(T->lchild, count);
Degree1(T->rchild, count);
}
DC06PE52求二叉树的分支结点总数
int sum=0;
int BranchNodes(BiTree T)
{ // Add your code here
if(!T) return 0;
return T->lchild or T->rchild ? 1+BranchNodes(T->lchild)+BranchNodes(T->rchild) : 0;
}
DC06PE53按层次遍历方式计算二叉树的结点个数
int get(BiTree T){
return T ? 1+get(T->lchild)+get(T->rchild) : 0;
}
int LevelSum(BiTree T)
{ // Add your code here
if(!T) return 0;
int count = get(T);
return count;
}
DC06PE54对一棵二叉树,将它的所有没有左孩子但有右孩子的结点,将其右孩子改变为左孩子
void ChangeTree(BiTree &T)
{ // Add your code here
if(!T) return;
if(T->rchild and !T->lchild) {
T->lchild = T->rchild;
T->rchild = NULL;
}
ChangeTree(T->lchild);
ChangeTree(T->rchild);
}
DC06PE55求二叉树的宽度
#include <queue>
int Width(BiTree T)
{ // Add your code here
if(!T) return 0;
queue<BiTNode*>q;
q.push(T);
int width = 0, res = 0;
while(!q.empty()){
width = q.size();
if(width > res) res = width;
for(int i=0; i<width; i++){
BiTNode *node = q.front();
q.pop();
if(node->lchild) q.push(node->lchild);
if(node->rchild) q.push(node->rchild);
}
}
return res;
}
DC06PE56编写递归算法,查找求二叉树T中是否存在元素值为x的结点
bool found = 0;
void find(BiTree T,TElemType x){
if(!T) return;
if(T->data == x){
found = 1;
return;
}
find(T->lchild,x);
find(T->rchild,x);
}
Status SearchX(BiTree T, TElemType x)
{ // Add your code here
if(!T) return 0;
find(T,x);
return found;
}
DC06PE57计算二叉树中值为x的结点所在的层次
#include <queue>
int NodeLevel(BiTree t, TElemType x)
{ // Add your code here
if(!t) return -1;
queue<BiTNode*>q;
q.push(t);
int width = 0, count=0;
while(!q.empty()){
width = q.size();
count++;
for(int i=0; i<width; i++){
BiTNode *node = q.front();
if(node->data == x) return count;
q.pop();
if(node->lchild) q.push(node->lchild);
if(node->rchild) q.push(node->rchild);
}
}
return -1;
}
DC06PE58求二叉树中以元素值为x的结点为根的子树的结点总数
int count(BiTree T) {
return T ? 1 + count(T->lchild) + count(T->rchild) : 0;
}
int find(BiTree T, TElemType x) {
if(!T) return 0;
if(T->data == x) return count(T);
int l = find(T->lchild, x);
int r = find(T->rchild, x);
if(l) return l;
return r;
}
int xSum(BiTree T, TElemType x)
{ // Add your code here
if(!T) return 0;
return find(T,x);
}
DC06PE59求指定结点在二叉树中的层次
#include <queue>
void xLevel(BiTree T,TElemType x, bool &found, int &xlev)
{ // Add your code here
if(!T) return;
queue<BiTNode*>q;
q.push(T);
int width = 0, count=0;
while(!q.empty()){
width = q.size();
count++;
for(int i=0; i<width; i++){
BiTNode *node = q.front();
if(node->data == x) {
found = 1;
xlev = count;
return;
}
q.pop();
if(node->lchild) q.push(node->lchild);
if(node->rchild) q.push(node->rchild);
}
}
found = 0;
xlev = 0;
}
DC06PE60判别一棵二叉树是否为正则二叉树
Status RegularBiTree(BiTree T)
{ // Add your code here
if(!T)return 1;
if(!T->lchild and !T->rchild)return 1;
if(T->lchild and T->rchild){
if(RegularBiTree(T->lchild) and RegularBiTree(T->rchild))return 1;
else return 0;
}
return 0;
}
DC06PE61判别一棵二叉树是否为小根二叉树
#include <queue>
Status SmallBiTree(BiTree T)
{ // Add your code here
if(!T) return 1;
queue<BiTNode *>q;
q.push(T);
while(!q.empty()) {
BiTNode *node = q.front();
q.pop();
if(node->lchild) {
q.push(node->lchild);
if(node->lchild->data <= node->data) return 0;
}
if(node->rchild) {
q.push(node->rchild);
if(node->rchild->data <= node->data) return 0;
}
}
return 1;
}
DC06PE62利用队列,非递归求解二叉树的宽度
#include <queue>
int Width(BiTree T)
{ // Add your code here
if(!T) return 0;
queue<BiTNode*>q;
q.push(T);
int width = 0;
while(!q.empty()){
if(q.size() > width) width = q.size();
int n = q.size();
for(int i=0;i<n;i++){
BiTNode *node = q.front();
q.pop();
if(node->lchild) q.push(node->lchild);
if(node->rchild) q.push(node->rchild);
}
}
return width;
}
DC06PE65试编写一个二叉排序树的判定算法
bool test(BSTree T, KeyType l, KeyType r){
if(!T) return 1;
if(T->data.key <= l or T->data.key >= r) return 0;
return test(T->lchild, l, T->data.key) and test(T->rchild, T->data.key, r);
}
Status IsBSTree(BSTree T)
{ // Add your code here
if(!T) return 1;
return test(T, 'A' - 1, 'Z' + 1);
}
DC06PE66从大到小输出给定二叉排序树中所有关键字不小于x的数据元素
void OrderOut(BSTree T, KeyType k, void(*visit)(TElemType))
{ // Add your code here
if(!T) return;
OrderOut(T->rchild,k,visit);
if(T->data.key >= k) visit(T->data);
OrderOut(T->lchild,k,visit);
}
DC06PE67在二叉查找树中插入一个元素
Status InsertBST_I(BSTree &T, TElemType k)
{ // Add your code here
if(!T) {
BSTNode *node = (BSTNode *)malloc(sizeof(BSTNode));
node -> data = k;
node -> lchild = NULL;
node -> rchild = NULL;
T = node;
return 1;
}
if(T->data.key == k.key) return 0;
if(k.key < T->data.key) {
if(!T->lchild) {
T->lchild = (BSTNode *)malloc(sizeof(BSTNode));
T->lchild->data = k;
T->lchild->lchild = NULL;
T->lchild->rchild = NULL;
return 1;
}
else return InsertBST_I(T->lchild, k);
}
else {
if(!T->rchild) {
T->rchild = (BSTNode *)malloc(sizeof(BSTNode));
T->rchild->data = k;
T->rchild->lchild = NULL;
T->rchild->rchild = NULL;
return 1;
}
else return InsertBST_I(T->rchild, k);
}
return 1;
}
DC06PE68求二叉树T中任意两个结点的最近共同祖先
#include <queue>
bool find(BiTree T, TElemType t) {
if(!T) return 0;
queue<BiTNode*>q;
q.push(T);
while(!q.empty()){
BiTNode *node = q.front();
q.pop();
if(node->data == t) return 1;
if(node->lchild) q.push(node->lchild);
if(node->rchild) q.push(node->rchild);
}
return 0;
}
BiTNode* LCA(BiTree T, TElemType a, TElemType b) {
if(!T) return NULL;
BiTNode *l, *r;
if(T->lchild and (T->lchild->data == a or T->lchild->data == b)) return T;
if(T->rchild and (T->rchild->data == a or T->rchild->data == b)) return T;
l = LCA(T->lchild, a, b);
r = LCA(T->rchild, a, b);
if(l and r) return T;
return l ? l : r;
}
BiTree CommAncestor(BiTree T, TElemType a, TElemType b)
{ // Add your code here
if(!T or !find(T,a) or !find(T,b) or T->data == a or T->data == b) return NULL;
return LCA(T,a,b); // This is temporary code. Change it if necessary.
}
DC06PE69将二叉树以字符串形式输出
#include <queue>
queue<char>s;
void get(BiTree T) {
if(!T) {
s.push('#');
return;
}
s.push(T->data);
if(!T->lchild and !T->rchild) return;
s.push('(');
if(T->lchild) get(T->lchild);
else s.push('#');
s.push(',');
if(T->rchild) get(T->rchild);
else s.push('#');
s.push(')');
}
char* BiTree2String(BiTree T)
{ // Add your code here
get(T);
int n = s.size();
char *ch = new char[n+1];
ch[n] = '\0';
for(int i=0; i<n; i++){
ch[i] = s.front();
s.pop();
}
return ch; //Temporary code. Modify it if necessary.
}
DC06PE75求二叉排序树中第k小的结点的位置
BSTNode *Ranking(BSTree T, int k)
{
if(NULL == T) return NULL;
if(T->lsize == k) return T;
else if(k < T->lsize) return Ranking(T->lchild, k);
else return Ranking(T->rchild, k-T->lsize);
}
DC06PE77求平衡树T的深度,并j记录每个结点平衡因子
int depth(BBSTree T){
if(!T) return NULL;
return 1 + (depth(T->lchild) > depth(T->rchild) ? depth(T->lchild) : depth(T->rchild));
}
int Depth_BF(BBSTree T)
{ // Add your code here
if(!T) return NULL;
int l = Depth_BF(T->lchild);
int r = Depth_BF(T->rchild);
T->bf = l-r;
return depth(T);
}
DC06PE82平衡二叉排序树的右平衡处理
void RightBalance(BBSTree &T)
{ // Add your code here
BBSTree r = T->rchild;
if (r->bf == -1) {
T->bf = r->bf = 0;
L_Rotate(T);
return;
}
if (r->bf) {
BBSTree l = r->lchild;
if (l->bf == 1) {
T->bf = 0;
r->bf = -1;
} else if (l->bf == 0) {
T->bf = r->bf = 0;
} else if (l->bf == -1) {
T->bf = 1;
r->bf = 0;
}
l->bf = 0;
R_Rotate(T->rchild);
L_Rotate(T);
}
}