数据结构:二叉树的建立和遍历(C#实现)

这篇博客介绍了如何使用C#语言实现二叉树的创建和先序遍历。通过一个名为`Tree`的类来构建二叉树,并使用`TreeNode`表示树的节点。博客提供了代码示例,包括节点类和树类的定义,以及通过用户输入的字符串来创建二叉树的方法。此外,还展示了如何进行先序遍历的递归实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

遍历只做了先序,递归实现的,中序和后序都类似.代码比较简单,就不写注释,直接贴出来了

 

代码:

TreeNode:结点类

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace 二叉树的建立和遍历
  5. {
  6.     class TreeNode
  7.     {
  8.         public char data;
  9.         public  TreeNode left, right;
  10.         
  11.         public TreeNode(char c,TreeNode l,TreeNode r)
  12.         {
  13.             data = c;
  14.             left = l;
  15.             right = r;
  16.         }
  17.         public TreeNode() { left = right = null; }
  18.     }
  19. }

 

 

Tree:树类

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace 二叉树的建立和遍历
  5. {
  6.     class Tree
  7.     {
  8.         public TreeNode root;
  9.         Stack<TreeNode> stack = new Stack<TreeNode>();
  10.         
  11.         public void CreateTree(String description)
  12.         {
  13.             bool left=true
  14.             char[] descriptionarray = description.ToCharArray();
  15.             
  16.             root = new TreeNode();
  17.             root.data = descriptionarray[0];
  18.             TreeNode temp=root;
  19.            
  20.             for (int i = 1; i <=descriptionarray.Length-1; i++)
  21.             {
  22.                 
  23.                 if (descriptionarray[i] == '(')
  24.                 {
  25.                     left = true; stack.Push(temp);
  26.                 }
  27.                 else if (descriptionarray[i] == ',')
  28.                 {
  29.                     left = false;
  30.                 }
  31.                 else if (descriptionarray[i] == ')') { stack.Pop(); }
  32.                 else
  33.                 {
  34.                     temp = new TreeNode();
  35.                     temp.data = descriptionarray[i];
  36.                     if (left == true)
  37.                     {
  38.                         stack.Peek().left = temp;
  39.                     }
  40.                     else
  41.                     {
  42.                         stack.Peek().right = temp;
  43.                     }
  44.                 }
  45.                 
  46.             }
  47.         }
  48.         
  49.         public void PreOrder(TreeNode t,String sign)
  50.         {
  51.             
  52.             if (t!= null)
  53.             {
  54.                 Console.WriteLine(sign+t.data);
  55.                 sign += sign;
  56.                 if (t.left != null)
  57.                 {
  58.                     
  59.                     PreOrder(t.left,sign);
  60.                 }
  61.                 if (t.right != null)
  62.                 {
  63.                     
  64.                     PreOrder(t.right,sign); 
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }

 

 

Program:调用者

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace 二叉树的建立和遍历
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //TreeNode t=null;
  11.             Tree tree = new Tree();
  12.             Console.WriteLine("请输入用字符串表示的二叉树");
  13.             string input =Console.ReadLine();
  14.             tree.CreateTree(input);
  15.             Console.WriteLine("遍历");
  16.             tree.PreOrder(tree.root,"--");
  17.             Console.Read();
  18.         }
  19.     }
  20. }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值