是二叉树的显示一个结点的父结点、兄弟结点、子结点的值的问题。这是出现的情况
//键盘输入一个元素x,求其父节点、兄弟结点、子结点的值,不存在时给出相应提示信息。对兄弟结点和孩子结点,存在时要明确指出是左兄弟、左孩子、右兄弟或右孩子
template<class ElementType>
void BiTree<ElementType>::GetFatherBrotherChildNode(Node <ElementType>* T, ElementType x)
{
if (T)
{
if (T->lChild != NULL)
{
if (T->lChild->data == x)
{
cout << "父节点的值为:" << T->data << endl; //输出父节点的值
if (T->rChild == NULL)
{
cout << "右兄弟不存在!" << endl; //判断右兄弟是否存在,存在输出其值
}
else {
cout << "右兄第为:" << T->rChild->data << endl;
}
if (T->lChild->lChild == NULL)
{
cout << "左孩子不存在!" << endl;
}
else {
cout << "左孩子为:" << T->lChild->lChild->data << endl;
}
if (T->lChild->rChild == NULL)
{
cout << "右孩子不存在!" << endl;
}
else {
cout << "右孩子为:" << T->lChild->lChild->data << endl;
}
}
}
if (T->rChild != NULL)
{
if (T->rChild->data == x)
{
cout << "父节点的值为:" << T->data << endl; //输出父节点的值
if (T->lChild == NULL)
{
cout << "左兄弟不存在!" << endl; //判断左兄弟是否存在,存在输出其值
}
else {
cout << "左兄第为:" << T->lChild->data << endl;
}
if (T->rChild->lChild == NULL)
{
cout << "左孩子不存在!" << endl;
}
else {
cout << "左孩子为:" << T->rChild->lChild->data << endl;
}
if (T->rChild->rChild == NULL)
{
cout << "右孩子不存在!" << endl;
}
else {
cout << "右孩子为:" << T->rChild->rChild->data << endl;
}
}
}
GetFatherBrotherChildNode(T->lChild, x);
GetFatherBrotherChildNode(T->rChild, x);
}
}
#include<stdio.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
int length;
}LNode, *LinkList;
void deleteValue(int e, LinkList *L)
{
int cnt = 0;
int i;
int flag = 0;
LNode *p = (*L)->next;
LNode *q;
for(i = 0; i < (*L)->length; i++)
{
if(p->data == e)
{
if(flag == 0)
{
flag = 1;
}
else
{
LNode *p1 = p;
p = p->next;
q->next = p;
free(p1);
}
}
q = p;
p = p->next;
}
}