int main(int argc, char *argv[]) { BiTree T,H; printf("请创建一棵二叉树(如:'ABDH##I##EJ###CF##G##')\n"); CreateBinaryTree(&T); printf("\n二叉树的深度为:%d,结点数目为:%d\n",BinaryDepth(T),NodeCount(T)); printf("\n先序遍历的结果是:\n"); PreOrderTraverse(T); printf("\n中序遍历的结果是:\n"); InOrderTraverse(T); printf("\n后序遍历的结果是:\n"); PostOrderTraverse(T); printf("\n对二叉树进行中序线索化\n"); InOrderThreading(&H,T); printf("\n中序遍历线索二叉树的结果是:\n"); InOrderTraverseThreadTree(H); printf("\n摧毁一棵二叉树\n"); DestoryBinaryTree(&T); printf("\n二叉树的深度为:%d,结点数目为:%d\n",BinaryDepth(T),NodeCount(T)); return 0; }
/*计算结点数量*/ int NodeCount(BiTree T) { if(!T) return 0; else return (NodeCount(T->lchild)+NodeCount(T->rchild)+1); }
int BinarySearchTree::NodeCount( Node *node ) { if ( !node ) { return 0; } return NodeCount( node->left ) + NodeCount( node->right ) + 1; }
Node* Graph::GetNode(int index) { if (NodeCount() <= index) return 0; std::list<Node*>::iterator iter = nodes.begin(); std::advance(iter, index); return *iter; }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Print(CallGraph* callGraph, Unit* unit) { // Print the external and unknown nodes. NewNode(ExternalCallNode::GetExternalNode(), "EXTERNAL", COLOR_LIGHT_PINK, SHAPE_RECT); NewNode(UnknownCallNode::GetUnknownNode(), "UNKNOWN", COLOR_LIGHT_GREEN, SHAPE_RECT); // Print the nodes for each function, then create the links. for(auto funct = unit->Functions().First(); funct; funct = funct->Next) { auto functionRef = GetFunctionReference(funct->Value, unit); auto node = callGraph->GetNode(functionRef); NewNode(node, *funct->Value->Name(), COLOR_LIGHT_BLUE, SHAPE_TRAPEZIUM); node->ForEachCallSite([this](CallSite* site) -> bool { string name = string::Format(L"%d", site->GetCallId()); NewNode(site, name, COLOR_LIGHT_GREY, SHAPE_ELLIPSE); return true; }); } auto& nodeGroups = callGraph->GetCallNodeGroups(); for(int i = 0; i < nodeGroups.Count(); i++) { auto nodeGroup = nodeGroups[i]; string name = string::Format(L"Group: %d", nodeGroup->NodeCount()); NewNode(nodeGroup, name, COLOR_YELLOW, SHAPE_DIAMOND); nodeGroup->ForEachNode([this, nodeGroup](CallNodeBase* node) -> bool { Link(nodeGroup, node); return true; }); } for(auto funct = unit->Functions().First(); funct; funct = funct->Next) { auto functionRef = GetFunctionReference(funct->Value, unit); auto node = callGraph->GetNode(functionRef); CreateLinks(node); } CreateLinks(ExternalCallNode::GetExternalNode()); CreateLinks(UnknownCallNode::GetUnknownNode()); Close(); }
int BinarySearchTree::NodeCount() { return NodeCount( m_Root ); }