二叉树(C#数据结构四)
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Text;
5
6namespace soloDatastruct
7{
8 public class TNode<T> //树结点
9 {
10 public T data; //结点值
11 public TNode<T> left;//左孩子
12 public TNode<T> right; //右孩子
13 public TNode()
14 {
15 data =default(T);
16 left = null;
17 right = null;
18 }
19 public TNode(T datavalue)
20 {
21 data = datavalue;
22 left = null;
23 right = null;
24 }
25
26 }
27 public class Mytree<T>
28 {
29 private TNode<T> rootNode; //根结点
30 public TNode<T> RootNode
31 {
32 get { return rootNode; }
33 set { rootNode = value; }
34 }
35
36 public Mytree(T data)
37 {
38 rootNode = new TNode<T>(data);
39 }
40 public TNode<T> CreatNode(T data, TNode<T> leftNode, TNode<T> rightNode)
41 {
42 TNode<T> newNode = new TNode<T>(data);
43 newNode.left = leftNode;
44 newNode.right = rightNode;
45 return newNode;
46
47 }
48 public void InsertLchild(TNode<T> newNode, TNode<T> p)
49 {
50 //将结点添加到树 结点P的左孩子结点,
51 //如果存在,将作为左孩子结点的左孩子结点
52 if (p.left == null)
53 p.left = newNode;
54 else
55 p.left.left = newNode;
56 }
57 public void InsertRchild(TNode<T> newNode, TNode<T> p)
58 {
59 //将结点添加到树 结点P右左孩子结点,
60 //如果存在,将作为左孩子结点的右孩子结点
61 if (p.right == null)
62 p.right = newNode;
63 else
64 p.right.right = newNode;
65 }
66 public void PreOrder(TNode<T> node)
67 {
68 //递归先序算法
69 if (node == null)
70 return;
71 Console.WriteLine(node.data);
72 PreOrder(node.left);
73 PreOrder(node.right);
74 }
75 public void NRPreOrder(TNode<T> node)
76 {
77 //非递归先序算法
78 TNode<T> p=new TNode<T>();
79 p=node;
80 while (p != null)
81 {
82 Console.WriteLine(p.data);
83 p = p.left;
84 }
85 p = node;
86 while (p != null&&p.right!=null)
87 {
88 Console.WriteLine(p.right.data);
89 p = p.right;
90 }
91 }
92 public void delNode()
93 {
94 //不写了,遍历删除就行了.
95 }
96
97 }
98 class Program
99 {
100 static void Main(string[] args)
101 {
102 Mytree<int> mystree = new Mytree<int>(0);
103 TNode<int> node = new TNode<int>(1);
104 TNode<int> node2= new TNode<int>(2);
105 TNode<int> node3 = new TNode<int>(3);
106 mystree.InsertLchild(node, mystree.RootNode);
107 mystree.InsertLchild(node2, mystree.RootNode);
108 mystree.InsertRchild(node3, mystree.RootNode);
109 mystree.NRPreOrder(mystree.RootNode);
110 Console.ReadLine();
111
112 }
113 }
114}
115
116
2using System.Collections;
3using System.Collections.Generic;
4using System.Text;
5
6namespace soloDatastruct
7{
8 public class TNode<T> //树结点
9 {
10 public T data; //结点值
11 public TNode<T> left;//左孩子
12 public TNode<T> right; //右孩子
13 public TNode()
14 {
15 data =default(T);
16 left = null;
17 right = null;
18 }
19 public TNode(T datavalue)
20 {
21 data = datavalue;
22 left = null;
23 right = null;
24 }
25
26 }
27 public class Mytree<T>
28 {
29 private TNode<T> rootNode; //根结点
30 public TNode<T> RootNode
31 {
32 get { return rootNode; }
33 set { rootNode = value; }
34 }
35
36 public Mytree(T data)
37 {
38 rootNode = new TNode<T>(data);
39 }
40 public TNode<T> CreatNode(T data, TNode<T> leftNode, TNode<T> rightNode)
41 {
42 TNode<T> newNode = new TNode<T>(data);
43 newNode.left = leftNode;
44 newNode.right = rightNode;
45 return newNode;
46
47 }
48 public void InsertLchild(TNode<T> newNode, TNode<T> p)
49 {
50 //将结点添加到树 结点P的左孩子结点,
51 //如果存在,将作为左孩子结点的左孩子结点
52 if (p.left == null)
53 p.left = newNode;
54 else
55 p.left.left = newNode;
56 }
57 public void InsertRchild(TNode<T> newNode, TNode<T> p)
58 {
59 //将结点添加到树 结点P右左孩子结点,
60 //如果存在,将作为左孩子结点的右孩子结点
61 if (p.right == null)
62 p.right = newNode;
63 else
64 p.right.right = newNode;
65 }
66 public void PreOrder(TNode<T> node)
67 {
68 //递归先序算法
69 if (node == null)
70 return;
71 Console.WriteLine(node.data);
72 PreOrder(node.left);
73 PreOrder(node.right);
74 }
75 public void NRPreOrder(TNode<T> node)
76 {
77 //非递归先序算法
78 TNode<T> p=new TNode<T>();
79 p=node;
80 while (p != null)
81 {
82 Console.WriteLine(p.data);
83 p = p.left;
84 }
85 p = node;
86 while (p != null&&p.right!=null)
87 {
88 Console.WriteLine(p.right.data);
89 p = p.right;
90 }
91 }
92 public void delNode()
93 {
94 //不写了,遍历删除就行了.
95 }
96
97 }
98 class Program
99 {
100 static void Main(string[] args)
101 {
102 Mytree<int> mystree = new Mytree<int>(0);
103 TNode<int> node = new TNode<int>(1);
104 TNode<int> node2= new TNode<int>(2);
105 TNode<int> node3 = new TNode<int>(3);
106 mystree.InsertLchild(node, mystree.RootNode);
107 mystree.InsertLchild(node2, mystree.RootNode);
108 mystree.InsertRchild(node3, mystree.RootNode);
109 mystree.NRPreOrder(mystree.RootNode);
110 Console.ReadLine();
111
112 }
113 }
114}
115
116