ZE-Standard-Libraries/tree/tree.c

498 lines
13 KiB
C
Raw Normal View History

2018-06-12 09:03:31 +00:00
#include"tree.h"
2018-06-12 11:07:12 +00:00
2018-07-23 04:59:16 +00:00
static int if_safeModeForTree = 0;
2018-06-13 08:04:51 +00:00
2018-06-12 11:07:12 +00:00
int safeModeForTree(int ifon) {
if (ifon == 1) {
if (tnode_list == NULL && tree_list == NULL) {
2018-08-03 10:58:49 +00:00
tnode_list = (List *)malloc(sizeof(List));
if(tnode_list == NULL){
showError(pushError(TREE_NODE, HIGH, initInfo("safeModeForTree()", "Error in get the memory of tnode_list.")));
return -1;
}
2018-06-12 11:07:12 +00:00
tree_list = (List *)malloc(sizeof(List));
2018-08-03 10:58:49 +00:00
if(tree_list == NULL){
showError(pushError(TREE_NODE, HIGH, initInfo("safeModeForTree()", "Error in get the memory of tree_list.")));
return -1;
}
2018-06-12 11:07:12 +00:00
tree_list->head = NULL;
tree_list->length = 0;
tree_list->tail = NULL;
tnode_list->head = NULL;
tnode_list->length = 0;
tnode_list->tail = NULL;
if_safeModeForTree = 1;
}
else {
return -1;
}
}
return ifon;
}
TNode *initTNode(void) {
Node *s_node;
2018-07-30 10:07:24 +00:00
TNode *p_tnode = (TNode *)malloc(sizeof(TNode));
2018-08-03 10:58:49 +00:00
if(p_tnode == NULL){
2018-08-07 04:10:55 +00:00
showError(pushError(TREE_NODE, STANDARD, initInfo("initTNode()", "Error in getting the memory of tnode.")));
2018-08-03 10:58:49 +00:00
return NULL;
}
2018-08-03 10:32:20 +00:00
p_tnode->s_id = getS_id(TREE_NODE, 2);
p_tnode->if_sid = 1;
2018-06-12 11:07:12 +00:00
p_tnode->child_num = 0;
p_tnode->father = NULL;
p_tnode->if_malloc = 0;
p_tnode->value = NULL;
2018-07-23 04:59:16 +00:00
p_tnode->type = VOID;
2018-06-12 11:07:12 +00:00
p_tnode->home = initList();
p_tnode->room = NULL;
if (if_safeModeForTree) {
if (if_safeModeForNode) {
if_safeModeForNode = 0;
s_node = initNode();
2018-07-23 04:59:16 +00:00
initMallocValueForNode(s_node, POINTER, (void *)p_tnode);
2018-06-12 11:07:12 +00:00
insertInTail(tnode_list, s_node);
if_safeModeForNode = 1;
}
else
{
s_node = initNode();
2018-07-23 04:59:16 +00:00
initMallocValueForNode(s_node, POINTER, (void *)p_tnode);
2018-06-12 11:07:12 +00:00
insertInTail(tnode_list, s_node);
}
}
return p_tnode;
}
Tree *initTree(void) {
Node *s_node;
2018-07-30 10:07:24 +00:00
Tree *p_tree = (Tree *)malloc(sizeof(Tree));
2018-08-03 10:58:49 +00:00
if(p_tree == NULL){
2018-08-07 04:10:55 +00:00
showError(pushError(TREE, STANDARD, initInfo("initTree()", "Error in getting the memory of tree.")));
2018-08-03 10:58:49 +00:00
return NULL;
}
2018-08-03 10:32:20 +00:00
p_tree->s_id = getS_id(TREE, 1);
p_tree->if_sid = 1;
2018-06-12 11:07:12 +00:00
p_tree->root = NULL;
if (if_safeModeForTree) {
if (if_safeModeForNode) {
if_safeModeForNode = 0;
s_node = initNode();
2018-07-23 04:59:16 +00:00
initMallocValueForNode(s_node, POINTER, (void *)p_tree);
2018-06-12 11:07:12 +00:00
if_safeModeForNode = 1;
insertInTail(tree_list, s_node);
}
else
{
s_node = initNode();
2018-07-23 04:59:16 +00:00
initMallocValueForNode(s_node, POINTER, (void *)p_tree);
2018-06-12 11:07:12 +00:00
insertInTail(tree_list, s_node);
}
}
return p_tree;
}
2018-07-30 09:45:33 +00:00
int *initMallocValueForTNode(TNode *p_tnode, unsigned int type, void *value) {
2018-06-12 11:07:12 +00:00
p_tnode->type = type;
p_tnode->value = value;
p_tnode->if_malloc = 1;
2018-06-12 09:03:31 +00:00
return 0;
2018-06-12 11:07:12 +00:00
}
int addChildInLeft(TNode *f_tnode, TNode *c_tnode) {
Node *p_node = initNode();
2018-07-23 04:59:16 +00:00
initMallocValueForNode(p_node, POINTER, c_tnode);
2018-06-12 11:07:12 +00:00
insertInHead(f_tnode->home, p_node);
c_tnode->father = f_tnode;
2018-06-13 08:04:51 +00:00
c_tnode->room = p_node;
2018-06-12 11:07:12 +00:00
f_tnode->child_num++;
return 0;
}
int addChildInRight(TNode *f_tnode, TNode *c_tnode) {
Node *p_node = initNode();
2018-07-23 04:59:16 +00:00
initMallocValueForNode(p_node, POINTER, c_tnode);
2018-06-12 11:07:12 +00:00
insertInTail(f_tnode->home, p_node);
c_tnode->father = f_tnode;
2018-06-13 08:04:51 +00:00
c_tnode->room = p_node;
2018-06-12 11:07:12 +00:00
f_tnode->child_num++;
return 0;
}
TNode *getBrotherInLeft(TNode *p_tnode) {
List *p_home = p_tnode->father->home;
2018-06-13 08:32:39 +00:00
Node *p_node = p_tnode->room;
2018-06-12 11:07:12 +00:00
unsigned long long index = getIndexByNode(p_home, p_node);
if (index > 0) return (TNode *)(findByIndexForNode(p_home, index - 1)->value);
return NULL;
}
TNode *getBrotherInRight(TNode *p_tnode) {
List *p_home = p_tnode->father->home;
Node *p_node = p_home->head;
unsigned long long index = getIndexByNode(p_home, p_node);
if (index < p_home->length - 1) return (TNode *)(findByIndexForNode(p_home, index + 1)->value);
return NULL;
}
int removeChildInLeft(TNode *p_tnode) {
TNode *c_tnode = (TNode *)p_tnode->home->head->value;
c_tnode->father = NULL;
releaseOnlyNode(c_tnode->room);
c_tnode->room = NULL;
p_tnode->child_num--;
popFromHead(p_tnode->home);
return 0;
}
int removeChildInRight(TNode *p_tnode) {
TNode *c_tnode = (TNode *)p_tnode->home->tail->value;
c_tnode->father = NULL;
releaseOnlyNode(c_tnode->room);
c_tnode->room = NULL;
p_tnode->child_num--;
popFromTail(p_tnode->home);
return 0;
}
2018-07-30 09:45:33 +00:00
TNode *getChildById(TNode *p_tnode, const SID *s_id) {
2018-06-12 11:07:12 +00:00
List *p_home = p_tnode->home;
2018-08-07 04:10:55 +00:00
target_value_sid = NULL;
List *er_list = initList();
insertInTail(er_list, nodeWithPointer(s_id));
List *rtnc_list = listThrough(p_home, _dogetChildById, er_list);
free(er_list);
Node *p_node= findByIndexForNode(rtnc_list, 1);
if (p_node != NULL) {
TNode *p_tnode = getByPointerForNode(p_node);
free(rtnc_list);
return p_tnode;
2018-06-12 11:07:12 +00:00
}
return NULL;
}
2018-08-07 04:10:55 +00:00
List *_dogetChildById(unsigned int type, void *value, List *er_list) {
SID *target_sid = getByPointerForNode(findByIndexForNode(er_list, 0));
List *rtn_list = initList();
2018-07-23 04:59:16 +00:00
if (type == POINTER) {
2018-06-12 11:07:12 +00:00
TNode *p_tode = (TNode *)value;
2018-07-30 09:45:33 +00:00
if (simFitS_id(p_tode->s_id, target_sid)) {
2018-08-07 04:10:55 +00:00
target_value_sid = p_tode;
insertInTail(rtn_list, nodeWithInt(-1));
insertInTail(rtn_list, nodeWithPointer(target_value_sid));
return rtn_list;
2018-06-12 11:07:12 +00:00
}
2018-08-07 04:10:55 +00:00
}
insertInTail(rtn_list, nodeWithInt(0));
return rtn_list;
2018-06-12 11:07:12 +00:00
}
2018-07-30 09:45:33 +00:00
TNode *getChildByValue(TNode *p_tnode, unsigned int type, void *value) {
2018-06-12 11:07:12 +00:00
List *p_home = p_tnode->home;
target_value = value;
target_type = type;
2018-08-07 04:10:55 +00:00
target_value_value = NULL;
List *er_list = initList();
insertInTail(er_list, nodeWithUInt(type));
insertInTail(er_list, nodeWithPointer(value));
List *rtnc_list = listThrough(p_home, _dogetChildByValue,er_list);
free(er_list);
Node *p_node = NULL;
if ((p_node = findByIndexForNode(rtnc_list, 1)) != NULL) {
TNode *p_tnode = getByPointerForNode(p_node);
free(rtnc_list);
return p_tnode;
2018-06-12 11:07:12 +00:00
}
return NULL;
}
2018-08-07 04:10:55 +00:00
List *_dogetChildByValue(unsigned int type, void *value, List *er_list) {
List *rtn_list = initList();
unsigned int target_type = getByUIntForNode(findByIndexForNode(rtn_list, 0));
void *target_value = getByPointerForNode(findByIndexForNode(rtn_list, 1));
2018-07-23 04:59:16 +00:00
if (type == target_type) {
2018-06-12 11:07:12 +00:00
TNode *p_tode = (TNode *)value;
2018-07-23 04:59:16 +00:00
if (target_type == INT) {
2018-06-12 11:07:12 +00:00
if (*(int *)p_tode->value == *(int *)target_value)
2018-08-07 04:10:55 +00:00
{
insertInTail(rtn_list, nodeWithInt(-1));
insertInTail(rtn_list, nodeWithPointer(p_tode));
return rtn_list;
2018-06-12 11:07:12 +00:00
}
}
2018-07-23 04:59:16 +00:00
else if (target_type == DOUBLE)
2018-06-12 11:07:12 +00:00
{
if (*(double *)p_tode->value == *(double *)target_value)
2018-08-07 04:10:55 +00:00
{
insertInTail(rtn_list, nodeWithInt(-1));
insertInTail(rtn_list, nodeWithPointer(p_tode));
return rtn_list;
2018-06-12 11:07:12 +00:00
}
}
2018-07-23 04:59:16 +00:00
else if (target_type == STRING)
2018-06-12 11:07:12 +00:00
{
if (!strcmp((char *)p_tode->value, (char *)target_value))
2018-08-07 04:10:55 +00:00
{
insertInTail(rtn_list, nodeWithInt(-1));
insertInTail(rtn_list, nodeWithPointer(p_tode));
return rtn_list;
2018-06-12 11:07:12 +00:00
}
}
2018-07-23 04:59:16 +00:00
else if (target_type == POINTER)
2018-06-12 11:07:12 +00:00
{
if (p_tode->value == target_value)
2018-08-07 04:10:55 +00:00
{
insertInTail(rtn_list, nodeWithInt(-1));
insertInTail(rtn_list, nodeWithPointer(p_tode));
return rtn_list;
2018-06-12 11:07:12 +00:00
}
}
2018-08-07 04:10:55 +00:00
}
insertInTail(rtn_list, nodeWithInt(0));
return rtn_list;
2018-06-12 11:07:12 +00:00
}
2018-07-30 09:45:33 +00:00
int removeChildById(TNode *p_tnode, const SID *s_id) {
TNode *t_tnode = getChildById(p_tnode, s_id);
2018-06-12 11:07:12 +00:00
if (t_tnode != NULL) {
TNode *p_fnode = t_tnode->father;
p_fnode->child_num--;
2018-07-30 09:45:33 +00:00
removeById(p_fnode->home, t_tnode->room->s_id);
2018-06-12 11:07:12 +00:00
releaseOnlyNode(t_tnode->room);
t_tnode->room = NULL;
return 0;
}
return -1;
}
2018-07-30 09:45:33 +00:00
int removeChildByValue(TNode *p_tnode, unsigned int type, void *value) {
2018-06-12 11:07:12 +00:00
TNode *t_tnode = getChildByValue(p_tnode, type, value);
if (t_tnode != NULL) {
TNode *p_fnode = t_tnode->father;
p_fnode->child_num--;
2018-07-30 09:45:33 +00:00
removeById(p_fnode->home, t_tnode->room->s_id);
2018-06-12 11:07:12 +00:00
releaseOnlyNode(t_tnode->room);
t_tnode->room = NULL;
return 0;
}
return -1;
}
TNode *getChildByIndex(TNode *p_tnode, unsigned long long index) {
List *p_home = p_tnode->home;
Node *p_node = p_home->head;
int m_index = 0;
2018-06-13 08:15:25 +00:00
if (index < p_tnode->child_num)
2018-06-12 11:07:12 +00:00
{
2018-06-13 08:04:51 +00:00
while (p_node != NULL && m_index < index) {
2018-06-12 11:07:12 +00:00
m_index++;
p_node = p_node->next;
}
return (TNode *)p_node->value;
}
return NULL;
}
unsigned long long getIndexByChild(TNode *f_tnode, TNode *c_tnode) {
List *p_home = f_tnode->home;
Node *p_node = p_home->head;
int m_index = 0;
while (p_node != NULL) {
TNode *p_tnode = (TNode *)p_node->value;
2018-07-30 10:07:24 +00:00
if (p_tnode->s_id == c_tnode->s_id) {
2018-06-12 11:07:12 +00:00
return m_index;
}
m_index++;
p_node = p_node->next;
}
return -1;
}
int removeChildByIndex(TNode *p_tnode, unsigned long long index) {
TNode *t_tnode = getChildByIndex(p_tnode, index);
if (t_tnode != NULL) {
2018-06-13 08:04:51 +00:00
TNode *p_fnode = t_tnode->father;
2018-06-12 11:07:12 +00:00
p_fnode->child_num--;
2018-07-30 09:45:33 +00:00
removeById(p_fnode->home, t_tnode->room->s_id);
2018-06-12 11:07:12 +00:00
releaseOnlyNode(t_tnode->room);
t_tnode->room = NULL;
return 0;
}
return -1;
}
int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) {
int i;
2018-06-12 11:07:12 +00:00
TNode *p_tnode = p_tree->root;
if (p_tnode != NULL) {
if (p_tnode->child_num > 0) {
for (i = 0; i < p_tnode->child_num; i++) {
2018-06-12 11:07:12 +00:00
if (_doTreeThroughUp(getChildByIndex(p_tnode, i), 1, func) == -1) {
break;
}
}
}
2018-06-13 08:32:39 +00:00
func(p_tnode, 0);
2018-06-12 11:07:12 +00:00
}
return 0;
}
int _doTreeThroughUp(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)) {
int i, func_back;
2018-06-12 11:07:12 +00:00
if (p_tnode->child_num > 0) {
for (i = 0; i < p_tnode->child_num; i++) {
2018-06-12 11:07:12 +00:00
if (_doTreeThroughUp(getChildByIndex(p_tnode, i), height + 1, func)) return -1;
}
}
func_back = func(p_tnode, height);
2018-06-12 11:07:12 +00:00
if (func_back == -1)return -1;
return 0;
}
int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) {
int i;
2018-06-12 11:07:12 +00:00
TNode *p_tnode = p_tree->root;
if (p_tree->root != NULL) {
2018-06-13 08:32:39 +00:00
func(p_tnode, 0);
2018-06-12 11:07:12 +00:00
if (p_tree->root->child_num > 0) {
for (i = 0; i < p_tnode->child_num; i++) {
2018-06-12 11:07:12 +00:00
if (_doTreeThroughDown(getChildByIndex(p_tnode, i), 1, func) == -1) {
break;
}
}
}
}
return 0;
}
int _doTreeThroughDown(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)) {
int i;
2018-06-12 11:07:12 +00:00
int func_back = func(p_tnode, height);
if (p_tnode->child_num > 0) {
for (i = 0; i < p_tnode->child_num; i++) {
2018-06-12 11:07:12 +00:00
if (_doTreeThroughDown(getChildByIndex(p_tnode, i), height + 1, func)) return -1;
}
}
if (func_back == -1)return -1;
return 0;
}
int TreeTravel(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) {
TNode *p_tnode = p_tree->root;
unsigned long long height = 0;
if (p_tnode != NULL) {
int func_back = func(p_tnode, height);
while (func_back > -2) {
if (func_back > -1) {
p_tnode = getChildByIndex(p_tnode, func_back);
func(p_tnode, height + 1);
}
else
{
p_tnode = p_tnode->father;
func(p_tnode, height - 1);
}
}
}
return 0;
}
int releaseTNode(TNode *p_tnode) {
if (p_tnode->child_num == 0) {
releaseList(p_tnode->home);
if (p_tnode->father != NULL) {
2018-07-30 09:45:33 +00:00
removeChildById(p_tnode->father, p_tnode->s_id);
2018-06-12 11:07:12 +00:00
}
2018-07-23 04:59:16 +00:00
if (p_tnode->type != POINTER) {
if (p_tnode->type == LIST) {
2018-06-12 11:07:12 +00:00
releaseList((List *)p_tnode->value);
}
else {
free(p_tnode->value);
}
}
p_tnode->value = NULL;
2018-07-23 04:59:16 +00:00
p_tnode->type = VOID;
2018-07-30 10:07:24 +00:00
freeS_id(p_tnode->s_id);
2018-06-12 11:07:12 +00:00
p_tnode->if_malloc = 0;
free(p_tnode);
}
return 0;
}
int releaseTree(Tree *p_tree) {
TreeThroughUp(p_tree, _doreleaseTree);
p_tree->root = NULL;
2018-07-30 10:07:24 +00:00
freeS_id(p_tree->s_id);
2018-06-12 11:07:12 +00:00
free(p_tree);
return 0;
}
int _doreleaseTree(TNode *p_tnode, unsigned long long height) {
releaseTNode(p_tnode);
return 0;
}
int releaseOnlyTree(Tree *p_tree) {
2018-07-30 10:07:24 +00:00
freeS_id(p_tree->s_id);
2018-06-12 11:07:12 +00:00
p_tree->root = NULL;
free(p_tree);
return 0;
}
int releaseOnlyTNode(TNode *p_tnode) {
releaseList(p_tnode->home);
2018-06-13 08:04:51 +00:00
if (p_tnode->if_malloc) {
2018-07-23 04:59:16 +00:00
if (p_tnode->type != STRING) {
if (p_tnode->type == LIST) {
2018-06-13 08:04:51 +00:00
releaseList((List *)p_tnode->value);
}
else {
free(p_tnode->value);
}
2018-06-12 11:07:12 +00:00
}
}
p_tnode->value = NULL;
2018-07-23 04:59:16 +00:00
p_tnode->type = VOID;
2018-07-30 10:07:24 +00:00
freeS_id(p_tnode->s_id);
2018-06-12 11:07:12 +00:00
p_tnode->if_malloc = 0;
free(p_tnode);
return 0;
}
int releaseAllForTree(void) {
Node *p_node;
Tree *p_tree;
2018-06-12 11:07:12 +00:00
if (if_safeModeForTree) {
if_safeModeForTree = 0;
p_node = tnode_list->head;
2018-06-12 11:07:12 +00:00
while (p_node != NULL) {
TNode *p_tnode = (TNode *)p_node->value;
releaseOnlyTNode(p_tnode);
2018-06-13 08:04:51 +00:00
p_node = p_node->next;
2018-06-12 11:07:12 +00:00
}
p_node = tree_list->head;
while (p_node != NULL) {
p_tree = (Tree *)p_node->value;
2018-06-12 11:07:12 +00:00
releaseOnlyTree(p_tree);
2018-06-13 08:04:51 +00:00
p_node = p_node->next;
2018-06-12 11:07:12 +00:00
}
releaseList(tnode_list);
releaseList(tree_list);
}
return 0;
}
2018-06-13 08:04:51 +00:00
int setRoot(Tree *p_tree, TNode *p_tnode) {
p_tree->root = p_tnode;
return 0;
}