二叉树查找程序问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树查找程序问题相关的知识,希望对你有一定的参考价值。
采用中序遍历的方法查找数据元素,如查找树中是否有'R'结点和'E'结点,如有,返回该结点,如无,则提示"未找到该结点"的信息。
以下是我编的程序:
#include "stdlib.h"
#include "stdio.h"
typedef char DataType;
typedef struct Node
DataType data;
struct Node *leftChild;
struct Node *rightChild;
BiTreeNode;
void Initiate(BiTreeNode **root)
*root=(BiTreeNode *)malloc(sizeof(BiTreeNode));
(*root)->leftChild=NULL;
(*root)->rightChild=NULL;
BiTreeNode *InsertLeftNode(BiTreeNode *curr,DataType x)
BiTreeNode *s,*t;
if(curr==NULL)
return NULL;
t=curr->leftChild;
s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data=x;
s->leftChild=t;
s->rightChild=NULL;
curr->leftChild=s;
return curr->leftChild;
BiTreeNode *InsertRightNode(BiTreeNode *curr,DataType x)
BiTreeNode *s,*t;
if(curr==NULL)
return NULL;
t=curr->rightChild;
t=curr->rightChild;
s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data=x;
s->rightChild=t;
s->leftChild=NULL;
curr->rightChild=s;
return curr->rightChild;
void Destroy(BiTreeNode **root)
if((*root)!=NULL&&(*root)->leftChild!=NULL)
Destroy(&(*root)->leftChild);
if((*root)!=NULL&&(*root)->rightChild!=NULL)
Destroy(&(*root)->rightChild);
free(*root);
BiTreeNode *DeleteLeft(BiTreeNode *curr)
if(curr==NULL||curr->leftChild==NULL)
return NULL;
Destroy(&curr->leftChild);
curr->leftChild=NULL;
return curr;
BiTreeNode *DeleteRight(BiTreeNode *curr)
if(curr==NULL||curr->rightChild==NULL)
return NULL;
Destroy(&curr->rightChild);
curr->rightChild=NULL;
return curr;
void Visit(DataType item)
printf("%c",item);
void PreOrder(BiTreeNode *t,void Visit(DataType item))
if(t!=NULL)
Visit(t->data);
PreOrder(t->leftChild,Visit);
PreOrder(t->rightChild,Visit);
void InOrder(BiTreeNode *t,void Visit(DataType item))
if(t!=NULL)
InOrder(t->leftChild,Visit);
Visit(t->data);
InOrder(t->rightChild,Visit);
void PostOrder(BiTreeNode *t,void Visit(DataType item))
if(t!=NULL)
PostOrder(t->leftChild,Visit);
PostOrder(t->rightChild,Visit);
Visit(t->data);
void PrintBiTree(BiTreeNode *bt,int n)
int i;
if(bt==NULL)
return;
PrintBiTree(bt->rightChild,n+1);
for(i=0;i<n;i++)
printf(" ");
if(n>=0)
printf("- - -");
printf("%c\n",bt->data);
PrintBiTree(bt->leftChild,n+1);
main()
BiTreeNode *root,*p,*pp;
char j;
Initiate(&root);
p=InsertLeftNode(root,'A');
p=InsertLeftNode(p,'B');
p=InsertLeftNode(p,'D');
p=InsertLeftNode(p,'G');
p=InsertRightNode(root->leftChild,'C');
pp=p;
InsertLeftNode(p,'E');
InsertRightNode(pp,'F');
PrintBiTree(root->leftChild,0);
printf("前序遍历:");
PreOrder(root->leftChild,Visit);
printf("\n中序便历:");
InOrder(root->leftChild,Visit);
printf("\n后序遍历:");
PostOrder(root->leftChild,Visit);
Destroy(&root);
请在我的程序上增加函数不要另写个专门的查找程序谢谢!正确回答者请在程序旁边写上解释语句,奖励100分!
#include "stdio.h"
typedef char DataType;
typedef struct Node
DataType data;
struct Node *leftChild;
struct Node *rightChild;
BiTreeNode;
void Initiate(BiTreeNode **root)
*root=(BiTreeNode *)malloc(sizeof(BiTreeNode));
(*root)->leftChild=NULL;
(*root)->rightChild=NULL;
BiTreeNode *InsertLeftNode(BiTreeNode *curr,DataType x)
BiTreeNode *s,*t;
if(curr==NULL)
return NULL;
t=curr->leftChild;
s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data=x;
s->leftChild=t;
s->rightChild=NULL;
curr->leftChild=s;
return curr->leftChild;
BiTreeNode *InsertRightNode(BiTreeNode *curr,DataType x)
BiTreeNode *s,*t;
if(curr==NULL)
return NULL;
t=curr->rightChild;
t=curr->rightChild;
s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data=x;
s->rightChild=t;
s->leftChild=NULL;
curr->rightChild=s;
return curr->rightChild;
void Destroy(BiTreeNode **root)
if((*root)!=NULL&&(*root)->leftChild!=NULL)
Destroy(&(*root)->leftChild);
if((*root)!=NULL&&(*root)->rightChild!=NULL)
Destroy(&(*root)->rightChild);
free(*root);
BiTreeNode *DeleteLeft(BiTreeNode *curr)
if(curr==NULL||curr->leftChild==NULL)
return NULL;
Destroy(&curr->leftChild);
curr->leftChild=NULL;
return curr;
BiTreeNode *DeleteRight(BiTreeNode *curr)
if(curr==NULL||curr->rightChild==NULL)
return NULL;
Destroy(&curr->rightChild);
curr->rightChild=NULL;
return curr;
void Visit(DataType item)
printf("%c",item);
void PreOrder(BiTreeNode *t,void Visit(DataType item))
if(t!=NULL)
Visit(t->data);
PreOrder(t->leftChild,Visit);
PreOrder(t->rightChild,Visit);
void InOrder(BiTreeNode *t,void Visit(DataType item))
if(t!=NULL)
InOrder(t->leftChild,Visit);
Visit(t->data);
InOrder(t->rightChild,Visit);
void PostOrder(BiTreeNode *t,void Visit(DataType item))
if(t!=NULL)
PostOrder(t->leftChild,Visit);
PostOrder(t->rightChild,Visit);
Visit(t->data);
void PrintBiTree(BiTreeNode *bt,int n)
int i;
if(bt==NULL)
return;
PrintBiTree(bt->rightChild,n+1);
for(i=0;i<n;i++)
printf(" ");
if(n>=0)
printf("- - -");
printf("%c\n",bt->data);
PrintBiTree(bt->leftChild,n+1);
BiTreeNode *mid_search(BiTreeNode *root,DataType c)//中序查找函数,成功则返回查到结点,否则返回NULL
BiTreeNode *t;
if(root->leftChild!=NULL)if(t=(mid_search(root->leftChild,c)))return(t);//先找左子树
if(root->data==c)return(root);//再找双亲结点
if(root->rightChild!=NULL)if(t=(mid_search(root->rightChild,c)))return(t);//最后找右子树
return(NULL);//未找到则返回NULL
main()
BiTreeNode *root,*p,*pp,*t; //t为指向成功查找结点的指针,不成功则指向NULL
char j,c;
Initiate(&root);
p=InsertLeftNode(root,'A');
p=InsertLeftNode(p,'B');
p=InsertLeftNode(p,'D');
p=InsertLeftNode(p,'G');
p=InsertRightNode(root->leftChild,'C');
pp=p;
InsertLeftNode(p,'E');
InsertRightNode(pp,'F');
PrintBiTree(root->leftChild,0);
printf("前序遍历:");
PreOrder(root->leftChild,Visit);
printf("\n中序便历:");
InOrder(root->leftChild,Visit);
printf("\n后序遍历:");
PostOrder(root->leftChild,Visit);
printf("\n请输入要查找的字符\n");
scanf("%c",&c);//输入要查找的单个字符
t=NULL;//初始t为NULL
t=mid_search(root->leftChild,c);//调用函数
if(t==NULL)printf("未找到该结点。\n");//未找到
else printf("找到%c结点。\n",t->data);//找到则t为指向结点的指针
Destroy(&root);
二叉树查找
二叉树中查找算法:
JAVA代码:
/** * @author hbliu * @param data * @return 找到的结点 */ public Node searchNode(int data){ return searchNode(this.root,data); } /** * @author hbliu * @param node * @param data * @return * 功 能:二叉树中查找数据元素 */ private Node searchNode(Node node,int data){ Node searchResutlNode = null; if(node == null){ return null; } if(node.data == data){ return node; } if(node.leftNode != null){ searchResutlNode = searchNode(node.leftNode,data); if( searchResutlNode != null ) return searchResutlNode; } if(node.rightNode != null){ searchResutlNode = searchNode(node.rightNode,data); if( searchResutlNode != null ) return searchResutlNode; } return null; }
以上是关于二叉树查找程序问题的主要内容,如果未能解决你的问题,请参考以下文章