小的放左边,大的放右边(这里为关键字大小)
错误写法:(运行不出结果,插入部分错误)
初始化插入节点时,只是把值传入,左右孩子没有置空
#include<stdio.h> #include<stdlib.h> #include<string.h> //二叉搜索树,主要是插入 typedef struct data { int first;//关键字 char a[20];//待插入内容 }DATA; //树节点 typedef struct treeNode { DATA element; struct treeNode* Lchild; struct treeNode* Rchild; }NODE,*LPNODE; //树 typedef struct tree { LPNODE root; int size; }*LPTREE,TREE; //生成树节点 LPNODE createNode(DATA data) { LPNODE newNode=(LPNODE)malloc(sizeof(NODE)); newNode->element=data; newNode->Lchild=NULL; newNode->Rchild=NULL; return newNode; } //生成新树 LPTREE createTree() { LPTREE newTree=(LPTREE)malloc(sizeof(TREE)); newTree->root=NULL;//这一步关键 newTree->size=0; return newTree; } //插入节点 void insertNode(LPTREE tree,DATA data) { LPNODE inNode=(LPNODE)malloc(sizeof(NODE)); inNode->element=data; LPNODE cruNode=tree->root; LPNODE preNode=NULL; if(tree->size=0) { tree->root=inNode; tree->size++; return; } else { while(cruNode!=NULL) { if(cruNode->element.first>inNode->element.first) { cruNode=cruNode->Lchild; preNode=cruNode; } else if(cruNode->element.first<inNode->element.first) { cruNode=cruNode->Rchild; preNode=cruNode; } else { strcpy(cruNode->element.a,inNode->element.a); tree->size++; return;//如果关键字相等,新的覆盖旧的 } } } //将值放入 if(preNode->element.first>inNode->element.first) { preNode->Lchild=inNode; tree->size++; } else if(preNode->element.first<inNode->element.first) { preNode->Rchild=inNode; tree->size++; } } //遍历,这里与普通的二叉树不同,因为传入的是tree->root,根节点才有这个,其他节点没有,只有个左右孩子 //所以要封装树节点的打印 //先封装 void printTree(LPNODE root)//注意这里是LPNODE按照节点的方式传入 { if(root!=NULL) { printTree(root->Lchild); printf("%d %s ",root->element.first,root->element.a); printTree(root->Rchild); } } //再传入根节点 void midOrderBinaryTree(LPTREE tree) { printTree(tree->root); } int main() { DATA arrayData[3]; //={100,"丁丁",50,"平平",150,"琳琳"}; arrayData[0].first=100; strcpy(arrayData[0].a,"丁丁"); arrayData[1].first=50; strcpy(arrayData[1].a,"平平"); arrayData[2].first=150; strcpy(arrayData[2].a,"琳琳"); LPTREE tree=createTree(); insertNode(tree,arrayData[0]); printf("%d",arrayData[0].first); midOrderBinaryTree(tree); return 0; }
将错误的地方进行更改:
更加正确完整的写法(逻辑更清晰):
#include<stdio.h> #include<stdlib.h> #include<string.h> //二叉搜索树,主要是插入 typedef struct data { int first;//关键字 char a[20];//待插入内容 }DATA; //树节点 typedef struct treeNode { DATA element; struct treeNode* Lchild; struct treeNode* Rchild; }NODE,*LPNODE; //树 typedef struct tree { LPNODE root; int size; }*LPTREE,TREE; //生成树节点 LPNODE createNode(DATA data) { LPNODE newNode=(LPNODE)malloc(sizeof(NODE)); newNode->element=data; newNode->Lchild=NULL; newNode->Rchild=NULL; return newNode; } //生成新树 LPTREE createTree() { LPTREE newTree=(LPTREE)malloc(sizeof(TREE)); newTree->root=NULL;//这一步关键 newTree->size=0; return newTree; } //插入节点 void insertNode(LPTREE tree,DATA element) { LPNODE moveNode=tree->root; LPNODE moveParentNode=NULL; while(moveNode!=NULL) { moveParentNode=moveNode; if(element.first<moveNode->element.first) { moveNode=moveNode->Lchild; } else if(element.first>moveNode->element.first) { moveNode=moveNode->Rchild; } else { strcpy(moveNode->element.a,element.a); return; } } //找到位置,插入节点 LPNODE newNode=createNode(element); if(tree->root==NULL) { tree->root=newNode; } else//判断插在移动节点父节点的哪一边 { if(moveParentNode->element.first>element.first) { moveParentNode->Lchild=newNode; } else { moveParentNode->Rchild=newNode; } } } //遍历,这里与普通的二叉树不同,因为传入的是tree->root,根节点才有这个,其他节点没有,只有个左右孩子 //所以要封装树节点的打印 //先封装 void printTree(LPNODE root)//注意这里是LPNODE按照节点的方式传入 { if(root!=NULL) { printTree(root->Lchild); printf("%d %s ",root->element.first,root->element.a); printTree(root->Rchild); } } //再传入根节点 void midOrderBinaryTree(LPTREE tree) { printTree(tree->root); } int main() { DATA arrayData[3]; //={100,"丁丁",50,"平平",150,"琳琳"}; arrayData[0].first=100; strcpy(arrayData[0].a,"丁丁"); arrayData[1].first=50; strcpy(arrayData[1].a,"平平"); arrayData[2].first=150; strcpy(arrayData[2].a,"琳琳"); LPTREE tree=createTree(); insertNode(tree,arrayData[0]); insertNode(tree,arrayData[1]); insertNode(tree,arrayData[2]); midOrderBinaryTree(tree); return 0; }
结果显示
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct Node{ struct Node* lchild; struct Node* rchild; char c; int flag; }*LPNODE,NODE; typedef struct tree{ LPNODE root; int size; }TREE,*LPTREE; LPNODE createNode(char c,int flag){ LPNODE newNode=(LPNODE)malloc(sizeof(NODE)); newNode->lchild=NULL; newNode->rchild=NULL; newNode->c=c; newNode->flag=flag; return newNode; } LPTREE createTree(){ LPTREE newTree=(LPTREE)malloc(sizeof(TREE)); newTree->root=NULL; newTree->size=0; return newTree; } void insertNode(LPTREE tree,char c,int flag){ LPNODE inNode=createNode(c,flag); if(tree->size==0){ tree->root=inNode; tree->size++; return; } LPNODE cruNode=tree->root; LPNODE preNode=NULL; while(cruNode!=NULL){ if(inNode->flag>cruNode->flag){ preNode=cruNode;//这一句一定要在下一句的前面才能真正记录之前的cruNode; cruNode=cruNode->rchild; } else if(inNode->flag<cruNode->flag){ preNode=cruNode; cruNode=cruNode->lchild; } else if(inNode->flag==cruNode->flag){ cruNode->c=inNode->c; return; } } //printf(" %p",preNode); if(inNode->flag>preNode->flag){ preNode->rchild=inNode; tree->size++; } else { preNode->lchild=inNode; tree->size++; } } void lastOrder(LPNODE root){ if(root){ lastOrder(root->lchild); lastOrder(root->rchild); printf("%c ",root->c); } } void main(){ LPTREE tree=createTree(); insertNode(tree,'A',100); insertNode(tree,'B',110); insertNode(tree,'C',50); lastOrder(tree->root); //printf(" %p",tree->root); }
上述代码结果为:C B A