-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSTree.cpp
153 lines (136 loc) · 3.24 KB
/
CSTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "CSTree.h"
#include<iostream>
CSTree::CSTree()
{
root = NULL;
}
CSTree::~CSTree()
{
Traverse(root, ROOT_LAST_REVERSE_ORDER, &DeleteNode);
}
void CSTree::CreateFromBiTree(BiTree *bi_tree)
{
root = CreateFromBiTreeRecursion(bi_tree->GetRoot());
}
CSTreePointer CSTree::CreateFromBiTreeRecursion(BiTreeNodePointer bi_tree_root)
{
if (bi_tree_root == NULL)
return NULL;
CSTreePointer new_node = new CSTreeNode;
new_node->data = bi_tree_root->data;
new_node->first_child = CreateFromBiTreeRecursion(bi_tree_root->lchild);
new_node->next_subling = CreateFromBiTreeRecursion(bi_tree_root->rchild);
return new_node;
}
int CSTree::ShowNodeData(CSTreePointer Node)
{
if (Node != NULL)
std::cout << Node->data << " ";
return 0;
}
void CSTree::RootFisrtOrderTraverse(CSTreePointer tree, int(*visit)(CSTreePointer))
{
if (tree != NULL)
{
visit(tree);
RootFisrtOrderTraverse(tree->first_child,visit);
RootFisrtOrderTraverse(tree->next_subling,visit);
}
}
void CSTree::RootLastOrderTraverse(CSTreePointer tree, int(*visit)(CSTreePointer))
{
if (tree != NULL)
{
RootLastOrderTraverse(tree->first_child, visit);
visit(tree);
RootLastOrderTraverse(tree->next_subling, visit);
}
}
void CSTree::Traverse(CSTreePointer tree, int order, int(*visit)(CSTreePointer))
{
if (tree == NULL)
std::cout << "¿ÕÊ÷" << std::endl;
else
{
switch (order)
{
case ROOT_FISRT_ORDER:
RootFisrtOrderTraverse(tree, visit); break;
case ROOT_LAST_ORDER:
RootLastOrderTraverse(tree, visit); break;
case ROOT_LAST_REVERSE_ORDER:
RootLastReverseOrderTraverse(tree, visit); break;
default:
std::cout << "²ÎÊý´íÎó" << std::endl; break;
}
}
}
void CSTree::ShowAllData(int order)
{
int(*visit)(CSTreePointer) = &(CSTree::ShowNodeData);
Traverse(root, order, visit);
std::cout << std::endl;
}
int CSTree::DeleteNode(CSTreePointer Node)
{
if (Node != NULL)
delete Node;
return 0;
}
void CSTree::RootLastReverseOrderTraverse(CSTreePointer tree, int(*visit)(CSTreePointer))
{
if (tree != NULL)
{
RootLastReverseOrderTraverse(tree->first_child, visit);
RootLastReverseOrderTraverse(tree->next_subling, visit);
visit(tree);
}
}
int CSTree::LeafCount()
{
return LeafCountRecursion(root);
}
int CSTree::LeafCountRecursion(CSTreePointer tree)
{
int result = 0;
if (tree != NULL)
{
if (tree->first_child == NULL)
result = 1;
return result + LeafCountRecursion(tree->first_child) + LeafCountRecursion(tree->next_subling);
}
else
return 0;
}
void CSTree::CreateFromLevelDegree(int *level, int *degree, int length)
{
int i, father=0, child_visited=0;
CSTreePointer nodes[MAX_NODE];
nodes[0] = NULL;
for (i = 0; i < length; i++)
{
nodes[i] = new CSTreeNode;
nodes[i]->data = level[i];
nodes[i]->first_child = NULL;
nodes[i]->next_subling = NULL;
if (i == 0)
continue;
if (child_visited == 0)
nodes[father]->first_child = nodes[i];
else
nodes[i - 1]->next_subling = nodes[i];
child_visited++;
if (degree[father] == child_visited && i != length - 1)
{
father++;
while (degree[father] == 0)
father++;
child_visited = 0;
}
}
root = nodes[0];
}
CSTreePointer CSTree::GetRoot()
{
return root;
}