当前位置: 首页>>代码示例>>C++>>正文


C++ TreeNode::add方法代码示例

本文整理汇总了C++中TreeNode::add方法的典型用法代码示例。如果您正苦于以下问题:C++ TreeNode::add方法的具体用法?C++ TreeNode::add怎么用?C++ TreeNode::add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TreeNode的用法示例。


在下文中一共展示了TreeNode::add方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main(int argc, const char * argv[]) {

	//Create the root node of the tree:

	KeyValuePair root_pair = KeyValuePair(8, "Node H");
	TreeNode root = TreeNode(root_pair);

	//add some nodes to our tree

	TreeNode* node_a = new TreeNode(1, "Node A");

	TreeNode* node_i = new TreeNode(9, "Node I");

	TreeNode* node_b = new TreeNode(2, "Node B");

	TreeNode* node_j = new TreeNode(10, "Node J");

	root.setLeftChild(node_a);
	root.setRightChild(node_i);

	node_a->setRightChild(node_b);
	node_i->setRightChild(node_j);

	//------------------------------------------

	// 1) Test the tree traversals:
	std::cout << "-------------------" << std::endl;
	std::cout << "Inorder Traversal:" << std::endl;
	root.printInOrder();
	std::cout << std::endl;
	std::cout << "-------------------" << std::endl;
	std::cout << std::endl;

	std::cout << "-------------------" << std::endl;
	std::cout << "Preorder Traversal:" << std::endl;
	root.printPreOrder();
	std::cout << std::endl;
	std::cout << "-------------------" << std::endl;
	std::cout << std::endl;

	std::cout << "-------------------" << std::endl;
	std::cout << "Postorder Traversal:" << std::endl;
	root.printPostOrder();
	std::cout << std::endl;
	std::cout << "-------------------" << std::endl;
	std::cout << std::endl;

	//------------------------------------------

	// 2) Test the searchkey function by searching for keys
	std::cout << "Searching: " << std::endl;
	std::cout << "-------------------" << std::endl;
	TreeNode* my_node_pntr = root.searchKey(10);
	std::cout << "Search for key 10 found:  " << my_node_pntr->getPair()->getValue() << std::endl;          //output should be Node J

	my_node_pntr = root.searchKey(3);
	std::cout << "Search for key 3 found: " << my_node_pntr->getPair()->getValue() << std::endl;          // output should be Node B (because 3 isnt in the tree)

	//------------------------------------------

	// 3) Test the add method by creating new keyvaluepairs and adding them to the tree
	// and after that, print again the inorder traversal, to see if the new nodes were added

	std::cout << std::endl;
	KeyValuePair* my_pair_pointer = new KeyValuePair(12, "Node L");
	KeyValuePair* my_scnd_pair_pntr = new KeyValuePair(0, "Node 0");

	root.add(my_pair_pointer);
	root.add(my_scnd_pair_pntr);

	std::cout << "updated Tree in inorder traversal:" << std::endl;
	std::cout << "-------------------" << std::endl;
	root.printInOrder();

	//------------------------------------------

	return 0;
}
开发者ID:rkoch,项目名称:uzh-inf02b-a3,代码行数:78,代码来源:main.cpp


注:本文中的TreeNode::add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。