資料結構與演算法——普通樹的定義與C++實現
阿新 • • 發佈:2018-12-25
用樹的第一個兒子和下一個兄弟表示法來表示一個樹。
樹的節點結構為:
struct TreeNode{
TYPE element;//該節點的元素
TreeNode *firstChild;//指向該節點的第一個孩子
TreeNode *nextSibling;//指向該節點的兄弟節點
};
上圖的第一個兒子和下一個兄弟表示法如下圖所示:
樹的遍歷:採用前序遍歷(先根節點,再左孩子,最後右孩子);
該表示法實現樹的中序遍歷和後序遍歷不太方便。。
原始碼:
/*************************************************************************
> File Name: tree_print.cpp
> Author:
> Mail:
> Created Time: 2016年04月05日 星期二 09時10分02秒
************************************************************************/
#include <iostream>
using namespace std;
#define TYPE char
//樹的節點
struct TreeNode{
TYPE element;//該節點的元素
TreeNode *firstChild;//指向該節點的第一個孩子
TreeNode *nextSibling;//指向該節點的兄弟節點
};
class Tree{
public:
Tree(TreeNode * r = NULL):root(r){}
Tree(int node_num);
~Tree();
void addNode(int i, int j);
void preOrder();//前序遍歷
void print();//列印
private:
void print(TreeNode* node, int num);
void addBrotherNode(TreeNode* bro, TreeNode* node);
void preOrder(TreeNode* parent);//前序遍歷
private:
TreeNode * root;//該樹的根
};
//列印樹的形狀
void Tree::print()
{
print(root, 0 );
}
void printSpace(int num)
{
int i = 0;
for(i = 0; i < num-3; i++)
cout << " ";
for(; i < num-2; ++i)
cout << "|";
for(; i < num; ++i)
cout << "_";
}
void Tree::print(TreeNode* node, int num)
{
if(node != NULL){
printSpace(num);
cout << node->element << endl;
print(node->firstChild, num+4);
print(node->nextSibling, num);
}
}
//前序遍歷
void Tree::preOrder()
{
cout << "前序遍歷: ";
preOrder(root);
cout << endl;
}
void Tree::preOrder(TreeNode* parent)
{
if(parent != NULL){
cout << parent->element << " ";
preOrder(parent->firstChild);
preOrder(parent->nextSibling);
}
}
//分配並初始化所有的樹結點
Tree::Tree(int node_num)
{
root = new TreeNode[node_num];
char ch = 'A';
for(int i = 0; i < node_num; ++i){
root[i].element = ch + i;
root[i].firstChild = NULL;
root[i].nextSibling = NULL;
}
}
//釋放所有節點的記憶體空間
Tree::~Tree()
{
if(root != NULL)
delete [] root;
}
//addNode將父子結點組對
//如果父節點的firstChild==NULL, 則firstChild = node;
//如果父節點的firstChild != NULL, 則
void Tree::addNode(int i, int j)
{
TreeNode* parent = &root[i];
TreeNode* node = &root[j];
if(parent->firstChild == NULL)
parent->firstChild = node;
else
addBrotherNode(parent->firstChild, node);
}
//將節點插入到兄弟節點
void Tree::addBrotherNode(TreeNode* bro, TreeNode* node)
{
if(bro->nextSibling == NULL)
bro->nextSibling = node;
else
addBrotherNode(bro->nextSibling, node);
}
//主函式
int main()
{
Tree tree(16);//分配十六個節點
tree.addNode(0, 1);
tree.addNode(0, 2);
tree.addNode(0, 3);
tree.addNode(0, 4);
tree.addNode(0, 5);
tree.addNode(0, 6);
tree.addNode(3, 7);
tree.addNode(4, 8);
tree.addNode(4, 9);
tree.addNode(5, 10);
tree.addNode(5, 11);
tree.addNode(5, 12);
tree.addNode(6, 13);
tree.addNode(9, 14);
tree.addNode(9, 15);
cout << "Tree1: " << endl;
tree.preOrder();
tree.print();
Tree tree2(9);
tree2.addNode(0, 1);
tree2.addNode(0, 2);
tree2.addNode(1, 3);
tree2.addNode(1, 4);
tree2.addNode(2, 5);
tree2.addNode(3, 6);
tree2.addNode(5, 7);
tree2.addNode(5, 8);
cout << "Tree2: " << endl;
tree2.preOrder();
tree2.print();
return 0;
}
程式中tree1的形狀為下圖所示:
程式中tree2的形狀為下圖所示:
程式的執行結果為: