如图 15
/ \
12 23
/ \
9 25求给出代码!!!!
3条回答 默认 最新
- Xefvan 2016-12-24 15:39关注
如果是单纯的遍历,只需要输出数字即可,代码如下
#include <iostream> using namespace std; template <typename T> struct binaryTreeNode { T element; binaryTreeNode<T> *leftChild, *rightChild; binaryTreeNode() { leftChild = rightChild = NULL; } binaryTreeNode(const T &theElement) : element(theElement) { leftChild = rightChild = NULL; } binaryTreeNode(const T &theElement, binaryTreeNode *theLeftChild, binaryTreeNode *theRightChild) : element(theElement) { leftChild = theLeftChild; rightChild = theRightChild; } }; template <typename T> void printvalue(binaryTreeNode<T> *t) { cout << t->element << ' '; } template <typename T> void preOrder(binaryTreeNode<T> *t) { if(t != NULL) { printvalue(t); // 输出元素 preOrder(t->leftChild); // 前序遍历左子树 preOrder(t->rightChild); // 后序遍历右子树 } } template <typename T> void inOrder(binaryTreeNode<T> *t) { if(t != NULL) { inOrder(t->leftChild); // 中序遍历左子树 printvalue(t); // 输出元素 inOrder(t->rightChild); // 中序遍历右子树 } } int main() { binaryTreeNode<int> *first, *second, *third, *four, *five, *six, *seven; seven = new binaryTreeNode<int>(7); six = new binaryTreeNode<int>(6); five = new binaryTreeNode<int>(25); four = new binaryTreeNode<int>(9); third = new binaryTreeNode<int>(23, six, seven); second = new binaryTreeNode<int>(12, four, five); first = new binaryTreeNode<int>(15, second, third); cout << "先序遍历:"; preOrder(first); cout << endl; cout << "中序遍历:"; inOrder(first); // PrintNodeByLevel(first); return 0; }
如果要输出有树形的二叉树,代码如下
#include <iostream> #include <queue> using namespace std; template <typename T> struct binaryTreeNode { T element; binaryTreeNode<T> *leftChild, *rightChild; binaryTreeNode() { leftChild = rightChild = NULL; } binaryTreeNode(const T &theElement) : element(theElement) { leftChild = rightChild = NULL; } binaryTreeNode(const T &theElement, binaryTreeNode *theLeftChild, binaryTreeNode *theRightChild) : element(theElement) { leftChild = theLeftChild; rightChild = theRightChild; } }; template <typename T> void printvalue(binaryTreeNode<T> *t) { cout << t->element << ' '; } template <typename T> void PrintNodeByLevel(binaryTreeNode<T>* root) { queue<binaryTreeNode<T>*> Q; queue<string> C; Q.push(root); Q.push(0); do { binaryTreeNode<T>* node = Q.front(); Q.pop(); if (node) { cout << node->element << " "; if (node->leftChild) { Q.push(node->leftChild); C.push("/"); } if (node->rightChild) { Q.push(node->rightChild); C.push("\\"); } } else if (!Q.empty()) { cout << endl; while(C.size() != 0) { cout << C.front() << " "; C.pop(); } Q.push(0); cout << endl; } } while (!Q.empty()); } int main() { binaryTreeNode<int> *first, *second, *third, *four, *five, *six, *seven; seven = new binaryTreeNode<int>(7); six = new binaryTreeNode<int>(6); five = new binaryTreeNode<int>(25); four = new binaryTreeNode<int>(9); third = new binaryTreeNode<int>(23, six, seven); second = new binaryTreeNode<int>(12, four, five); first = new binaryTreeNode<int>(15, second, third); PrintNodeByLevel(first); return 0; }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 解决一个加好友限制问题 或者有好的方案
- ¥15 关于#java#的问题,请各位专家解答!
- ¥15 急matlab编程仿真二阶震荡系统
- ¥20 TEC-9的数据通路实验
- ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
- ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
- ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
- ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
- ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
- ¥30 求解达问题(有红包)