void test_search_element_in_the_tree_from_different_nodes(){ Tree tree = createTree(compareInteger); Iterator result; int data[5] = {1,2,3,4,5}; ASSERT(insertTree(&tree, NULL, &data[0])); ASSERT(insertTree(&tree, &data[0], &data[1])); ASSERT(insertTree(&tree, &data[1], &data[2])); ASSERT(insertTree(&tree, &data[2], &data[3])); ASSERT(insertTree(&tree, &data[3], &data[4])); ASSERT(searchTreeNode(&tree,&data[4])); ASSERT(searchTreeNode(&tree,&data[2])); ASSERT(searchTreeNode(&tree,&data[0])); }
void test_search_element_when_not_found(){ Tree tree = createTree(compareInteger); Iterator result; int data[5] = {1,2}; ASSERT(insertTree(&tree, NULL, &data[0])); ASSERT(insertTree(&tree, &data[0], &data[1])); ASSERT(0 == searchTreeNode(&tree,&data[4])); }
int removeTreeNode(Tree *tree,void* data){ TreeNode *matchedNode = searchTreeNode(tree,data); List* list; int index; if(!matchedNode) return 0; list = matchedNode->parent->list; index = getIndex(list,data,tree->compare); remove(list,index); return 1; };
BOOL deleteTreeNode(RBTree *rbTree, code_t hashCode){ RBTreeNode *targetNode, *deleteNode, *childNode; targetNode = searchTreeNode(rbTree,hashCode); if(targetNode != NULL){ /*找到待删除的真正结点位置*/ if(targetNode->left == NULL || targetNode ->right == NULL){ deleteNode = targetNode; }else{ deleteNode = nextTreeNode(targetNode); } /*将待删除节点的子树链接到其父节点上,待删除的节点最多只有一个子树*/ if(deleteNode->left != NULL){ childNode = deleteNode->left; }else{ childNode = deleteNode->right; } if(childNode != NULL){ childNode->parent = deleteNode->parent; } if(deleteNode->parent == NULL){ *rbTree = childNode; }else{ if(deleteNode->parent->left == deleteNode){ deleteNode->parent->left = childNode; }else{ deleteNode->parent->right =childNode; } } if(targetNode != deleteNode){ targetNode->hashCode = deleteNode->hashCode; targetNode->data = deleteNode->data; } if(deleteNode->color ==BLACK){ balanceRBTreeAfterDelete(rbTree,deleteNode->parent,childNode); } free(deleteNode->data); free(deleteNode); return TRUE; }else{ return FALSE; } }
Iterator getChildren(Tree *tree,void* parent){ Iterator it; TreeNode *matchedNode; if(parent == NULL){ it.list = tree->root; it.current = NULL; it.hasNext = NULL; it.next = getRootNode; return it; } matchedNode = searchTreeNode(tree,parent); it.list = matchedNode->list; if(it.list == NULL) it.current = NULL; else it.current = ((List*)it.list)->head; it.next = getCurrentTreeNode; it.hasNext = hasCurrentTreeNode; return it; };
int insertTreeNode(Tree *tree,void *parent,void *child){ TreeNode *node = createTreeNode(child); TreeNode *matchedTreeNode; List* list; if(parent == NULL){ tree->root = node; return 1; } matchedTreeNode = searchTreeNode(tree,parent); node->parent = matchedTreeNode; if(matchedTreeNode->list == NULL){ list = create(); matchedTreeNode->list = list; } list = matchedTreeNode->list; insert(list,node,1); return 1; };
int search(void* tree,void* parent){ void *matchedNode = searchTreeNode(tree,parent); if(matchedNode) return 1; return 0; };
void test_searching_tree_node_which_is_not_present_in_tree(){ int _10 = 10,_20 = 20; ASSERT(insertTreeNode(tree, &_10,NULL)); ASSERT(0 == searchTreeNode(tree,&_20)); }
void test_search_node_in_tree(){ int _10 = 10; ASSERT(insertTreeNode(tree, &_10,NULL)); ASSERT(searchTreeNode(tree,&_10)); }