框架:
struct treenode{
int val;
struct treenode left,right;};
void traverse(treenode root)
{
// 前序遍历root.val
traverse(root.left);
// 中序遍历root.val
traverse(root.right);
// 后序遍历root.val
}
C++实现
#include<iostream>
#include <stdlib.h>
using namespace std;
typedef struct _BNode {
int data;
struct _BNode *lchild, *rchild;
}Bnode,* Btree;
//二叉树插入结点
/*将插入结点e,与结点root结点进行比较,若小于则去到左子树,否则
去右子树进行比较,重复以上操作直到找到一个空位置放置该结点
*/
bool InsertBtree(Btree* root, Bnode* node) {
Bnode* temp = NULL;
Bnode* parent = NULL;
if (!node) { //如果插入结点为空,返回false
return false;
}else { //清空插入结点的左右子树
node->lchild = NULL;
node->rchild = NULL;
}
if (!(*root)) { //如果根节点不存在,将插入结点作为根节点
*root = node;
return true;
}else {
temp = *root;
}
while (temp != NULL) {
parent = temp;
if (temp->data>node->data) { //小于,继续向左
temp = temp->lchild;
}
else { //大于,继续向右(不可以有相同的值)
temp=temp->rchild;
}
//while循环结束,parent所处位置即为要插入结点的父结点
}
if (node->data < parent->data) {
parent->lchild = node;
}
else {
parent->rchild = node;
}
return true;
}
//二叉搜索树删除结点
//1.删除叶节点,直接删除
//2.删除结点存在左子树,不存在右子树,直接把左子节点替代删除结点
//3.删除结点存在有子节点,不存在左子节点,直接把右子节点代替删除结点
//4.删除结点存在左右子节点,则取左子树最大结点或右子树最小系欸DNA替换删除
//结点
int findLeftMax(Btree* root) {
/*采用递归方式查找
* if (root->rchild == NULL)
return root->data;
return findMax(root->rchild);
*/
//采用循环查找
Btree indexNode = *root;
while (indexNode->rchild)
indexNode = indexNode->rchild;
return indexNode->data;
}
//采用递归的方式删除结点
/*
这种递归方式,是将要修改的结点的一层一层的返回
*/
Btree deleteNode(Btree* root, int value) {
Btree compareNode = *root;
//节点为空(递归找不到value/根节点为空),直接返回
if (!compareNode)return compareNode;
//大于
if (compareNode->data > value) {
//左子树重新被赋值
compareNode->lchild = deleteNode(&(compareNode->lchild), value);
return compareNode;
}
//小于
else if (compareNode->data < value) {
//右子树重新被赋值
compareNode->rchild = deleteNode(&(compareNode->rchild), value);
return compareNode;
}
else {//等于
Btree temp = NULL;
//无左右子节点,直接返回NULL
if (compareNode->lchild == NULL && compareNode->rchild == NULL) {
delete compareNode;
}
//有左子树,返回左子树
else if (compareNode->lchild && compareNode->rchild == NULL) {
temp = compareNode->lchild;
delete compareNode;
}
//有右子树,返回右子树
else if (compareNode->rchild && compareNode->lchild == NULL) {
temp = compareNode->rchild;
delete compareNode;
}
else {
//这里采用左子树最大值替换
int leftMax = findLeftMax(&(compareNode->lchild));
//最大值替换删除结点的值
compareNode->data = leftMax;
//将最大值从树中删除
compareNode->lchild = deleteNode(&(compareNode->lchild), leftMax);
temp= compareNode;
}
return temp;
}
}
// 采用递归方式查找结点
/*
Bnode* queryByRec(Btree root, int value) {
if (root == NULL || root->data==value ){
return root;
}
else if (root->data < value) {
return queryByRec(root->lchild, value);
}
else {
return queryByRec(root->rchild, value);
}
}*/
// 使用非递归方式查找结点
Bnode* queryByLoop(Btree root, int value) {
while (root != NULL && root->data!=value) {
if (root->data>value) {
root = root->lchild;
}
else {
root = root->rchild;
}
}
return root;
}
void PreOrderRec(Btree* root) {
Btree indexNode = *root;
if (indexNode == NULL)return;
printf("—%d", indexNode->data);
PreOrderRec(&indexNode->lchild);
PreOrderRec(&indexNode->rchild);
}
int main() {
//_CrtSetBreakAlloc(86);
int test[] = { 19,7,25,5,11,15,21,61 ,4};
Btree root = NULL;
Btree node = NULL;
node = new Bnode;
node->data = test[0];
InsertBtree(&root, node);
for (int i = 1; i < sizeof(test) / sizeof(int); i++) {
node = new Bnode;
node->data = test[i];
InsertBtree(&root, node);
}
printf("前序遍历\n");
PreOrderRec(&root);
root= deleteNode(&root, 19);
printf("\n");
printf("删除19后的前序遍历\n");
PreOrderRec(&root);
printf("\n");
Btree temp;
printf("查找数据为61\n");
temp = queryByLoop(root, 61);
cout << temp->data << endl;
system("pause");
return 0;
}