@songpfei
2016-05-20T11:15:50.000000Z
字数 6658
阅读 2242
数据结构
定义:树是一个或多个节点的有效集合,且其中:
二叉树的性质:
满二叉树(full binary tree):是深度为k的满二叉树是具有个节点的二叉树,除了叶结点外每一个结点都有左右子叶且叶子结点都处在最底层的二叉树
typedef int ElementType;
typedef struct BinaryTreeNode BinTree;
struct BinaryTreeNode
{
ElementType data;
BinaryTreeNode* left;
BinaryTreeNode* right;
};
三种遍历:
- LVR:中序遍历(inoder);
- LRV:后续遍历(postorder);
- VLR:前序遍历(preoeder);
表达式树:表达式树的树叶是操作数,常数或变量,二其他的节点为操作符(operator),表达式三种遍历顺序与表达式的中缀(infix)、后缀(postfix)和前缀(prefix)形式之间存在一种自然的对应关系。
代码实现:
#ifndef BINARYTREE_H
#define BINARYTREE_H
typedef int ElementType;
struct BinaryTreeNode
{
ElementType data;
BinaryTreeNode* left;
BinaryTreeNode* right;
};
void PreOrder(BinaryTreeNode* pRoot);//前序遍历
void PostOrder(BinaryTreeNode* pRoot);//后续遍历
void InOrder(BinaryTreeNode* pRoot);//中序遍历
void LevelOrder(BinaryTreeNode* pRoot);//层序遍历
#endif
#include"BinaryTree.h"
#include<stdio.h>
#include<stdlib.h>
#include<queue>
/*1.递归的实现二叉树的遍历*/
//前序遍历
void PreOrder(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return;
printf("%d ", pRoot->data);
PreOrder(pRoot->left);
PreOrder(pRoot->right);
}
//后续遍历
void PostOrder(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return;
PreOrder(pRoot->left);
PreOrder(pRoot->right);
printf("%d ", pRoot->data);
}
//中序遍历
void InOrder(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return;
PreOrder(pRoot->left);
printf("%d ", pRoot->data);
PreOrder(pRoot->right);
}
//层序遍历
void LevelOrder(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return;
std::queue<BinaryTreeNode*> tree_node_queue;
tree_node_queue.push(pRoot);
while (tree_node_queue.size()>0)
{
pRoot = tree_node_queue.front();
tree_node_queue.pop();
printf("%d ", pRoot->data);
if (pRoot->left != NULL)
tree_node_queue.push(pRoot->left);
if (pRoot->right != NULL)
tree_node_queue.push(pRoot->right);
}
}
#ifndef BINARYTREE_H
#define BINARYTREE_H
typedef int ElementType;
struct BinaryTreeNode
{
ElementType data;
BinaryTreeNode* left;
BinaryTreeNode* right;
};
void PreOrder(BinaryTreeNode* pRoot);//前序遍历
void PostOrder(BinaryTreeNode* pRoot);//后续遍历
void InOrder(BinaryTreeNode* pRoot);//中序遍历
void LevelOrder(BinaryTreeNode* pRoot);//层序遍历
BinaryTreeNode* BinaryTreeSearchRecursive(BinaryTreeNode* pRoot, ElementType key);//二叉搜索树查找(递归)
BinaryTreeNode* BinaryTreeSearch(BinaryTreeNode* pRoot, ElementType key);//二叉搜索树查找(迭代)
bool BinaryTreeInsert(BinaryTreeNode* &pRoot, ElementType key);//二叉树节点插入
bool BinaryTreeDelete(BinaryTreeNode* &pRoot, ElementType key);//二叉树节点删除
#endif
#include"BinaryTree.h"
#include<stdio.h>
#include<stdlib.h>
#include<queue>
/*1.递归的实现二叉树的遍历*/
//前序遍历
void PreOrder(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return;
printf("%d ", pRoot->data);
PreOrder(pRoot->left);
PreOrder(pRoot->right);
}
//后续遍历
void PostOrder(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return;
PreOrder(pRoot->left);
PreOrder(pRoot->right);
printf("%d ", pRoot->data);
}
//中序遍历
void InOrder(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return;
PreOrder(pRoot->left);
printf("%d ", pRoot->data);
PreOrder(pRoot->right);
}
//层序遍历
void LevelOrder(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return;
std::queue<BinaryTreeNode*> tree_node_queue;
tree_node_queue.push(pRoot);
while (tree_node_queue.size() > 0)
{
pRoot = tree_node_queue.front();
tree_node_queue.pop();
printf("%d ", pRoot->data);
if (pRoot->left != NULL)
tree_node_queue.push(pRoot->left);
if (pRoot->right != NULL)
tree_node_queue.push(pRoot->right);
}
}
//二叉搜索树查找(递归)
BinaryTreeNode* BinaryTreeSearchRecursive(BinaryTreeNode* pRoot, ElementType key)
{
if (pRoot == NULL || key == pRoot->data)
return pRoot;
if (key < pRoot->data)
return BinaryTreeSearchRecursive(pRoot->left, key);
else
return BinaryTreeSearchRecursive(pRoot->right, key);
}
//二叉搜索树查找(迭代)时间复杂度:o(h)
BinaryTreeNode* BinaryTreeSearch(BinaryTreeNode* pRoot, ElementType key)
{
while (pRoot != NULL && key != pRoot->data)
{
if (key < pRoot->data)
pRoot = pRoot->left;
else
pRoot = pRoot->right;
}
return pRoot;
}
//二叉树插入
bool BinaryTreeInsert(BinaryTreeNode* &pRoot, ElementType key)
{
BinaryTreeNode* pParent = NULL;
BinaryTreeNode* pNode = pRoot;
while (pNode != NULL&&pNode->data != key)
{
pParent = pNode;
if (key < pNode->data)
pNode = pNode->left;
else
pNode = pNode->right;
}
if (pNode != NULL)//key已经存在
return false;
BinaryTreeNode* pNodeInsert = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
if (pNodeInsert == NULL)//内存申请失败
return false;
pNodeInsert->data = key;
pNodeInsert->left = pNodeInsert->right = NULL;
if (pParent == NULL)//树为空,新插入的节点为根节点
pRoot = pNodeInsert;
else if (key < pParent->data)
pParent->left = pNodeInsert;
else
pParent->right = pNodeInsert;
return true;
}
//二叉树节点删除
bool BinaryTreeDelete(BinaryTreeNode* &pRoot, ElementType key)
{
BinaryTreeNode *pNext, *pFather = NULL;
BinaryTreeNode* pNode = pRoot;
int pos = 0;//待删除节点的位置,0代表根节点,-1代表左节点,1代表右节点
while (pNode != NULL&&pNode->data != key)
{
pFather = pNode;
if (key < pNode->data)
{
pos = -1;
pNode = pNode->left;
}
else
{
pos = 1;
pNode = pNode->right;
}
}
if (pNode == NULL)//节点不存在
return false;
if (pNode->left != NULL&&pNode->right != NULL)//待删除的节点有两个孩子
{
pFather = pNode;
pos = -1;
pNext = pNode->left;//用其左树中的最大元素替代删除元素
while (pNext->right != NULL)
{
pos = 1;
pFather = pNext;
pNext = pNext->right;
}
pNode->data = pNext->data;//替换
pNode = pNext;//更新需要删除的元素
}
if (pNode->left != NULL)//待删除节点只有左孩子
pNext = pNode->left;
else if (pNode->right != NULL)//只有右节点
pNext = pNode->right;
else //待删除节点没有孩子
pNext = NULL;
if (pos == 0)//待删除节点为根节点
pRoot = pNext;
else if (pos == -1)//待删除节点为左孩子
pFather->left = pNext;
else //待删除节点为右孩子
pFather->right = pNext;
delete pNode;//删除节点
pNode = NULL;
return true;
}
#include"BinaryTree.h"
#include<stdlib.h>
#include<stdio.h>
int main()
{
BinaryTreeNode* pRootNode = NULL;
BinaryTreeNode* pFind;
BinaryTreeInsert(pRootNode, 30);
BinaryTreeInsert(pRootNode, 5);
BinaryTreeInsert(pRootNode, 2);
BinaryTreeInsert(pRootNode, 40);
BinaryTreeInsert(pRootNode, 35);
BinaryTreeInsert(pRootNode, 80);
BinaryTreeInsert(pRootNode, 32);
BinaryTreeInsert(pRootNode, 38);
PreOrder(pRootNode);
printf("1.前序遍历\n");
InOrder(pRootNode);
printf("2.中序遍历\n");
PostOrder(pRootNode);
printf("3.后序遍历\n");
LevelOrder(pRootNode);
printf("4.层序遍历\n");
pFind = BinaryTreeSearch(pRootNode, 35);
if (pFind)
printf("5.找到的节点为:%d\n", pFind->data);
else
printf("5.未找到节点");
pFind = BinaryTreeSearchRecursive(pRootNode, 33);
if (pFind)
printf("6.找到的节点为:%d\n", pFind->data);
else
printf("6.未找到节点\n");
if (BinaryTreeInsert(pRootNode, 33))
{
printf("7.插入成功\n");
LevelOrder(pRootNode);
}
if (BinaryTreeDelete(pRootNode, 33))
{
printf("8.删除成功\n");
LevelOrder(pRootNode);
}
if (BinaryTreeDelete(pRootNode, 35))
{
printf("8.删除成功\n");
LevelOrder(pRootNode);
}
return 0;
}