试写一非递归算法,在二叉查找树T中插入元素e。
二叉查找树的类型BSTree定义如下typedef struct fKeyType key;
// 其他数据域
TElemType;
typedef struct BSTNode fTElemType data;
struct BSTNode *lchild,*rchild; BSTNode,*BSTree;
实现下列函数
Status InsertBST I(BSTree &T,TElemType k);/*在二叉查找树T中插入元素e的非递归算法 */
#include "allinclude.h" //DO NOT edit this line
Status InsertBST_I(BSTree &T, TElemType k)
{
if (!T) { // 如果T为空,则新建一个节点
T = (BSTree)malloc(sizeof(BSTNode));
T->data = k;
T->lchild = T->rchild = NULL;
return OK;
}
BSTree p = T;
while (p) { // 在二叉查找树中查找插入位置
if (k.key == p->data.key) { // 如果已经存在相同的节点,则返回错误
return ERROR;
} else if (k.key < p->data.key) { // 如果k的值小于当前节点的值,则在左子树中查找
if (!p->lchild) { // 如果左子树为空,则插入新节点
BSTree s = (BSTree)malloc(sizeof(BSTNode));
s->data = k;
s->lchild = s->rchild = NULL;
p->lchild = s;
return OK;
}
p = p->lchild;
} else { // 如果k的值大于当前节点的值,则在右子树中查找
if (!p->rchild) { // 如果右子树为空,则插入新节点
BSTree s = (BSTree)malloc(sizeof(BSTNode));
s->data = k;
s->lchild = s->rchild = NULL;
p->rchild = s;
return OK;
}
p = p->rchild;
}
}
return ERROR;
}
试编写算法,求二叉树T中结点a和b的最近共同祖先。
二叉链表类型定义:typedef struct BiTNode fTElemType data;
struct BiTNode*lchild,*rchild;} BiTNode,*BiTree;
可用栈类型stack的相关定义:typedef struct fBiTNode *ptr; // 二叉树结点的指针类型inttag; // 0..1
// 栈的元素类型
SElemType;
status Initstack(stack &s);Status StackEmpty(stack s);int StackLength(SqStack s);Status Push(stack &S,SElemType e);status Pop(stack &S,SElemType &e);Status GetTop(stack s,SElemType &e);
要求实现下列函数:BiTree CommAncestor(BiTree T,TElemType a,TElemType b);*求二叉树T中结点a和b的最近共同祖先 */
#include "allinclude.h" //DO NOT edit this line
Status FindPath(BiTree T, TElemType x, Stack &s);
BiTree CommAncestor(BiTree T, TElemType a, TElemType b)
{ // Add your code here
if (!T) return NULL;
if (a==b) return NULL;
Stack s1, s2;
SElemType e1, e2;
InitStack(s1);
InitStack(s2);
if (!FindPath(T, a, s1)) return NULL;
if (!FindPath(T, b, s2)) return NULL;
// 使长度一致
while (StackLength(s1) > StackLength(s2)) Pop(s1, e1);
while (StackLength(s1) < StackLength(s2)) Pop(s2, e2);
while (GetTop(s1, e1) && GetTop(s2, e2) && e1.ptr->data != e2.ptr->data) {
Pop(s1, e1);
Pop(s2, e2);
}
if (!GetTop(s1, e1) || !GetTop(s2, e2)) return NULL;
return e1.ptr;
}
Status FindPath(BiTree T, TElemType x, Stack &s)
{
if (T->data==x) return OK;
SElemType e;
e.ptr = T;
Push(s, e);
if (T->lchild && FindPath(T->lchild, x, s)) return OK;
if (T->rchild && FindPath(T->rchild, x, s)) return OK;
// 没有寻找到
Pop(s, e);
return ERROR;
}
[20231823]编写一个递归算法,将二叉树输出为字符串。该字符串的格式定义如下:
(1) 如果二叉树为空,则对应的字符串为空串;
如果某个非终端结点的某棵子树为空,则使用“#”字符表示;对于叶子结点,其左、右子树无需使用任何字符表示;
(4)如果某结点存在子树,则将子树放置于括号()内,左子树与右子
树之间用逗号’,隔开;(5) 该字符串以'结尾示例:B对应的字符串为: A(B((D(#,F),G),C(E,#))二叉树定义如下:typedef structBiTNode {TElemTypedata;
E
// 数据域
// 左、右孩子指针struct BiTNode *lchild,*rchild;BiTNode,*BiTree;
请实现BiTree2string函数:char* BiTree2String(BiTree T);//返回一个表示二叉树T的字符串
void treeToString(BiTree T, string &str);
char* BiTree2String(BiTree T) {
string str = "";
treeToString(T, str);
char *res = new char[str.size() + 1];
strcpy(res, str.c_str());
return res;
}
void treeToString(BiTree T, string &str) {
if (!T) {
str += "#";
return ;
}
str += T->data;
if (T->lchild || T->rchild) {
str += '(';
treeToString(T->lchild, str);
str += ',';
treeToString(T->rchild, str);
str += ')';
}
}