int traverseBBSTMidOrder(BBST tree, DQueue lines) { BBSTNode p = tree->Root; DQueueData stack; initializeDQueue(&stack, tree->Context); while( stack.NodeCount > 0 || p != NULL ) { if ( p != NULL ) { insertDQueueHeadNode(&stack, p); p = p->Left; } else { p = (BBSTNode)removeDQueueHeadNode(&stack); insertDQueueTailNode(lines, p); p = p->Right; } } Assert(stack.NodeCount == 0); removeAllDQueueNodes(&stack); cleanDQueue(&stack); return FUNC_RETURN_OK; }
/* This function generates a sequence of nodes by a depth-first traverse in * pre-order, this is for verifying if the BST is well balanced. */ int traverseBBSTPreOrder(BBST tree, DQueue lines, int maxcount) { BBSTNode p = tree->Root; DQueueData stack; initializeDQueue(&stack, tree->Context); while( stack.NodeCount > 0 || p != NULL ) { if ( p != NULL ) { insertDQueueTailNode(lines, p); insertDQueueHeadNode(&stack, p); p = p->Left; if ( maxcount > 0 && getDQueueLength(lines) == maxcount ) { break; } } else { p = (BBSTNode)removeDQueueHeadNode(&stack); p = p->Right; } } removeAllDQueueNodes(&stack); cleanDQueue(&stack); return FUNC_RETURN_OK; }
int getLocalHostAllIPAddressesAsStrings(DQueue addresslist) { int res = FUNC_RETURN_OK; struct ifaddrs *ifaddr, *ifa; int family, s, n; char host[NI_MAXHOST]; if ( getifaddrs(&ifaddr) == -1 ) { elog(ERROR, "Failed to get interface addresses when calling getifaddrs(), " "errno %d", errno); } for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { if ( ifa->ifa_addr == NULL ) { continue; } family = ifa->ifa_addr->sa_family; if ( family == AF_INET ) { s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if ( s != 0 ) { elog(WARNING, "Fail to call getnameinfo(), err %s", gai_strerror(s )); continue; } HostAddress newaddr = NULL; newaddr = createHostAddressAsStringFromIPV4AddressStr(addresslist->Context, host); insertDQueueTailNode(addresslist, newaddr); elog(LOG, "Resource manager discovered local host IPv4 address %s", ((AddressString)(newaddr->Address))->Address); } } freeifaddrs(ifaddr); return res; }