searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

C++ 自定义二叉树并输出二叉树图形

2023-02-20 17:22:22
5
0

使用C++构建一个二叉树并输出。

输入

输入根节点为10,依次输入6、4、8、14、12、16

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include<iostream>
#include <stack>
#include<cstdlib>
​
#include <string>
using namespace std;
​
​
struct TreeLinkNode         // 定义二叉树
{
   int val;                       // 当前节点值用val表示
   struct TreeLinkNode *left;     // 指向左子树的指针用left表示
   struct TreeLinkNode *right;    // 指向右子树的指针用right表示
   struct TreeLinkNode *parent;  //指向父节点的指针用parent表示
   TreeLinkNode(int x) :val(x), left(NULL), right(NULL), parent(NULL) { } // 初始化当前结点值为x,左右子树、父节点为空
};
​
//创建树
TreeLinkNode* insert(TreeLinkNode* tree, int value)
{
   TreeLinkNode* node = (TreeLinkNode*)malloc(sizeof(TreeLinkNode)); // 创建一个节点
   node->val = value;      // 初始化节点
   node->left = NULL;
   node->right = NULL;
   node->parent = NULL;
​
   TreeLinkNode* temp = tree;      // 从树根开始
   while (temp != NULL)
  {
       if (value < temp->val)  // 小于根节点就进左子树
      {
           if (temp->left == NULL)
          {
               temp->left = node;  // 新插入的数为temp的左子树
               node->parent = temp; // temp为新插入的数的父节点
               return tree;
          }
           else           // 下一轮判断
               temp = temp->left;
      }
       else           // 否则进右子树
      {    
​
           if (temp->right == NULL)
          {
               temp->right = node;  // 新插入的数为temp的右子树
               node->parent = temp; // temp为新插入的数的父节点
               return tree;
          }
           else           // 下一轮判断
               temp = temp->right;
      }
  }
   return tree;
}

​
// ************* 输出图形二叉树 *************
void output_impl(TreeLinkNode* n, bool left, string const& indent)
{
   if (n->right)
  {
       output_impl(n->right, false, indent + (left ? "|     " : "     "));
  }
   cout << indent;
   cout << (left ? '\\' : '/');
   cout << "-----";
   cout << n->val << endl;
   if (n->left)
  {
       output_impl(n->left, true, indent + (left ? "     " : "|     "));
  }
}
void output(TreeLinkNode* root)
{
   if (root->right)
  {
       output_impl(root->right, false, "");
  }
   cout << root->val << endl;
   if (root->left)
  {
       output_impl(root->left, true, "");
  }
   system("pause");
}
// ***************************************
​
​
​
// ====================测试代码====================
int main()
{
​
   TreeLinkNode tree = TreeLinkNode(10);       // 树的根节点
   TreeLinkNode* treeresult;
​
   treeresult = insert(&tree, 6);         // 输入n个数并创建这个树
   treeresult = insert(&tree, 4);
   treeresult = insert(&tree, 8);
   treeresult = insert(&tree, 14);
   treeresult = insert(&tree, 12);
   treeresult = insert(&tree, 16);
​
   output(treeresult);         // 输出图形二叉树
​
}

 

输出

 

 

 

0条评论
0 / 1000
代码的路
100文章数
1粉丝数
代码的路
100 文章 | 1 粉丝
原创

C++ 自定义二叉树并输出二叉树图形

2023-02-20 17:22:22
5
0

使用C++构建一个二叉树并输出。

输入

输入根节点为10,依次输入6、4、8、14、12、16

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include<iostream>
#include <stack>
#include<cstdlib>
​
#include <string>
using namespace std;
​
​
struct TreeLinkNode         // 定义二叉树
{
   int val;                       // 当前节点值用val表示
   struct TreeLinkNode *left;     // 指向左子树的指针用left表示
   struct TreeLinkNode *right;    // 指向右子树的指针用right表示
   struct TreeLinkNode *parent;  //指向父节点的指针用parent表示
   TreeLinkNode(int x) :val(x), left(NULL), right(NULL), parent(NULL) { } // 初始化当前结点值为x,左右子树、父节点为空
};
​
//创建树
TreeLinkNode* insert(TreeLinkNode* tree, int value)
{
   TreeLinkNode* node = (TreeLinkNode*)malloc(sizeof(TreeLinkNode)); // 创建一个节点
   node->val = value;      // 初始化节点
   node->left = NULL;
   node->right = NULL;
   node->parent = NULL;
​
   TreeLinkNode* temp = tree;      // 从树根开始
   while (temp != NULL)
  {
       if (value < temp->val)  // 小于根节点就进左子树
      {
           if (temp->left == NULL)
          {
               temp->left = node;  // 新插入的数为temp的左子树
               node->parent = temp; // temp为新插入的数的父节点
               return tree;
          }
           else           // 下一轮判断
               temp = temp->left;
      }
       else           // 否则进右子树
      {    
​
           if (temp->right == NULL)
          {
               temp->right = node;  // 新插入的数为temp的右子树
               node->parent = temp; // temp为新插入的数的父节点
               return tree;
          }
           else           // 下一轮判断
               temp = temp->right;
      }
  }
   return tree;
}

​
// ************* 输出图形二叉树 *************
void output_impl(TreeLinkNode* n, bool left, string const& indent)
{
   if (n->right)
  {
       output_impl(n->right, false, indent + (left ? "|     " : "     "));
  }
   cout << indent;
   cout << (left ? '\\' : '/');
   cout << "-----";
   cout << n->val << endl;
   if (n->left)
  {
       output_impl(n->left, true, indent + (left ? "     " : "|     "));
  }
}
void output(TreeLinkNode* root)
{
   if (root->right)
  {
       output_impl(root->right, false, "");
  }
   cout << root->val << endl;
   if (root->left)
  {
       output_impl(root->left, true, "");
  }
   system("pause");
}
// ***************************************
​
​
​
// ====================测试代码====================
int main()
{
​
   TreeLinkNode tree = TreeLinkNode(10);       // 树的根节点
   TreeLinkNode* treeresult;
​
   treeresult = insert(&tree, 6);         // 输入n个数并创建这个树
   treeresult = insert(&tree, 4);
   treeresult = insert(&tree, 8);
   treeresult = insert(&tree, 14);
   treeresult = insert(&tree, 12);
   treeresult = insert(&tree, 16);
​
   output(treeresult);         // 输出图形二叉树
​
}

 

输出

 

 

 

文章来自个人专栏
文章 | 订阅
0条评论
0 / 1000
请输入你的评论
2
2