33,311
社区成员
发帖
与我相关
我的任务
分享
struct BinaryTreeNode
{
char data; //方便一点;
BinaryTreeNode* leftChild;
BinaryTreeNode* rightChild;
BinaryTreeNode():leftChild(NULL), rightChild(NULL), data(5)
{}
};
void levelOrderConstruct(BinaryTreeNode* &root, const std::string& str)
{
std::queue<BinaryTreeNode*> queue;
BinaryTreeNode *temp;
int i=0;
queue.push(root);
while (!queue.empty()) {
temp = queue.front();
queue.pop();
if (temp != 0)
{
temp->data = str[i++];
if( i==str.size() )
break;
if(temp->leftChild == 0)
{
temp->leftChild= new BinaryTreeNode;
queue.push(temp->leftChild);
}
if(temp->rightChild == NULL)
{
temp->rightChild= new BinaryTreeNode;
queue.push(temp->rightChild);
}
}
}
}
void inOrderTraverse(BinaryTreeNode *root) {
if (0 == root) {
return;
}
if (root->leftChild != 0) {
inOrderTraverse(root->leftChild);
}
std::cout<<root->data<<" ";
if (root->rightChild != 0) {
inOrderTraverse(root->rightChild);
}
}
void destroyTree(BinaryTreeNode* root)
{
std::queue<BinaryTreeNode*> queue;
BinaryTreeNode *node = 0;
queue.push(root);
while (!queue.empty())
{
node = queue.front();
queue.pop();
if (node != 0) {
if (node->leftChild != 0) {
queue.push(node->leftChild);
}
if (node->rightChild != 0) {
queue.push(node->rightChild);
}
}
delete node;
}
}
int main()
{
std::string str="123456";
BinaryTreeNode* root= new BinaryTreeNode;
levelOrderConstruct(root, str);
inOrderTraverse(root);
destroyTree(root);
return 0;
}
//输出的非数字字符表示多余节点的字符;
简单的讲,将节点按层次从1-n编号:
1
/ \
2 3
/ \ /
4 5 6
缺少的节点只能是大号的,
即:如果n号节点存在,则1到n-1号节点必定存在,
同样,若n号节点不存在,则n+1号及更大号的节点也必定不存在
这就唯一的确定了一颗完全二叉树了,
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define FALSE 0
#define TURE 1
#define OVERFLOW -2
typedef int Status;
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
Status CreatBiTree(BiTNode **p)
{
char ch;
scanf("%c",&ch);/* input ab**e**或者abc**d**ef**g** */
if('*'==ch)
{
*p=NULL;
}
else
{
*p=(BiTNode *)malloc(sizeof(BiTNode));
if(!(*p))
exit(OVERFLOW);
(*p)->data=ch;
CreatBiTree(&((*p)->lchild));
CreatBiTree(&((*p)->rchild));
}
return OK;
}
Status visit(char data)
{
printf("%c",data);
return OK;
}
Status PreOrderTraverse(BiTNode *Bp)/*先序遍历*/
{
if(Bp)
{
if(visit(Bp->data))
{
if(PreOrderTraverse(Bp->lchild))
{
if(PreOrderTraverse(Bp->rchild))
{
return OK;
}
}
}
return ERROR;
}
else
{
return OK;
}
}
Status InOrderTraverse(BiTNode *Bp)/*中序遍历*/
{
if(Bp)
{
if(InOrderTraverse(Bp->lchild))
{
if(visit(Bp->data))
{
if(InOrderTraverse(Bp->rchild))
{
return OK;
}
}
return ERROR;
}
return ERROR;
}
else
{
return OK;
}
}
Status PostOrderTraverse(BiTNode *Bp)/*后序遍历*/
{
if(Bp)
{
if(InOrderTraverse(Bp->lchild))
{
if(InOrderTraverse(Bp->rchild))
{
if(visit(Bp->data))
{
return OK;
}
return ERROR;
}
}
return ERROR;
}
else
{
return OK;
}
}
int main()
{
BiTNode *Bp=NULL;
CreatBiTree(&Bp);
printf("\nPreOrderTraverse:");
PreOrderTraverse(Bp);
printf("\n");
printf("\nInOrderTraverse:");
InOrderTraverse(Bp);
printf("\n");
printf("\nPostOrderTraverse:");
PostOrderTraverse(Bp);
printf("\n");
getchar();
getchar();
return OK;
}
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
class BinaryTreeNode {
public:
BinaryTreeNode(int data = 0) {
leftChild = 0;
rightChild = 0;
parent = 0;
this->data = data;
}
~BinaryTreeNode() {
}
void visit() {
std::cout << data << " ";
}
BinaryTreeNode *leftChild;
BinaryTreeNode *rightChild;
BinaryTreeNode *parent;
int data;
};
class BinaryTree {
public:
BinaryTree();
~BinaryTree();
void add(int value);
bool isEmpty();
void createTree(int *array, int length);
void preOrderTraverse();
void inOrderTraverse();
void postOrderTraverse();
void levelOrderTraverse();
private:
void preOrderTraverse(BinaryTreeNode *root);
void inOrderTraverse(BinaryTreeNode *root);
void postOrderTraverse(BinaryTreeNode *root);
void createTree(BinaryTreeNode *&node, int *array, int first, int last);
private:
BinaryTreeNode *root;
};
BinaryTree::BinaryTree() {
root = 0;
}
BinaryTree::~BinaryTree() {
std::queue<BinaryTreeNode*> queue;
BinaryTreeNode *node = 0;
queue.push(root);
while (!queue.empty()) {
node = queue.front();
queue.pop();
if (node != 0) {
if (node->leftChild != 0) {
queue.push(node->leftChild);
}
if (node->rightChild != 0) {
queue.push(node->rightChild);
}
}
delete node;
#ifdef DEBUG
std::cout << "Destructor" << std::endl;
#endif
}
}
void BinaryTree::add(int value) {
if (isEmpty()) {
root = new BinaryTreeNode();
root->data = value;
return;
}
BinaryTreeNode *node = root;
while (true) {
if (value < node->data) {
if (0 == node->leftChild) {
node->leftChild = new BinaryTreeNode();
node->leftChild->data = value;
break;
} else {
node = node->leftChild;
}
} else {
if (0 == node->rightChild) {
node->rightChild = new BinaryTreeNode();
node->rightChild->data = value;
break;
} else {
node = node->rightChild;
}
}
}
}
bool BinaryTree::isEmpty() {
return (0 == root) ? true : false;
}
void BinaryTree::createTree(int *array, int length) {
createTree(root, array, 0, length - 1);
}
void BinaryTree::createTree(BinaryTreeNode *&node, int *array, int first, int last) {
if (first > last) {
return;
}
int mid = (first + last + 1) / 2;
node = new BinaryTreeNode(*(array + mid));
createTree(node->leftChild, array, first, mid - 1);
createTree(node->rightChild, array, mid + 1, last);
}
void BinaryTree::preOrderTraverse() {
//preOrderTraverse(root);
std::stack<BinaryTreeNode*> stack;
BinaryTreeNode *temp = 0;
stack.push(root);
while (!stack.empty()) {
// Go to the most left end.
while ((temp = stack.top()) != 0) {
temp->visit();
stack.push(temp->leftChild);
}
stack.pop();
if (!stack.empty()) {
temp = stack.top();
stack.pop();
stack.push(temp->rightChild);
}
}
}
void BinaryTree::preOrderTraverse(BinaryTreeNode *node) {
// DLR
if (0 == node) {
return;
}
node->visit();
if (0 != node->leftChild) {
preOrderTraverse(node->leftChild);
}
if (0 != node->rightChild) {
preOrderTraverse(node->rightChild);
}
}
void BinaryTree::inOrderTraverse() {
//inOrderTraverse(root);
std::stack<BinaryTreeNode*> stack;
BinaryTreeNode *temp = 0;
stack.push(root);
while (!stack.empty()) {
while ((temp = stack.top()) != 0) {
stack.push(temp->leftChild);
}
stack.pop();
if (!stack.empty()) {
temp = stack.top();
stack.pop();
temp->visit();
stack.push(temp->rightChild);
}
}
}
void BinaryTree::inOrderTraverse(BinaryTreeNode *root) {
if (0 == root) {
return;
}
if (root->leftChild != 0) {
inOrderTraverse(root->leftChild);
}
root->visit();
if (root->rightChild != 0) {
inOrderTraverse(root->rightChild);
}
}
void BinaryTree::postOrderTraverse() {
postOrderTraverse(root);
}
void BinaryTree::postOrderTraverse(BinaryTreeNode *root) {
if (0 == root) {
return;
}
if (root->leftChild != 0) {
inOrderTraverse(root->leftChild);
}
if (root->rightChild != 0) {
inOrderTraverse(root->rightChild);
}
root->visit();
}
void BinaryTree::levelOrderTraverse() {
std::queue<BinaryTreeNode*> queue;
BinaryTreeNode *temp;
queue.push(root);
while (!queue.empty()) {
temp = queue.front();
queue.pop();
if (temp != 0) {
temp->visit();
if (temp->leftChild != 0) {
queue.push(temp->leftChild);
}
if (temp->rightChild != 0) {
queue.push(temp->rightChild);
}
}
}
}
int main(int argc, char *argv[]) {
BinaryTree *bTree = new BinaryTree();
const int length = 7;
int array[length] = {1, 2, 3, 4, 5, 6, 7};
bTree->createTree(array, length);
bTree->levelOrderTraverse();
std::cout << std::endl;
bTree->inOrderTraverse();
std::cout << std::endl;
delete bTree;
return EXIT_SUCCESS;
}