1 | ///
|
---|
2 | /// This file is part of ILNumerics Community Edition.
|
---|
3 | ///
|
---|
4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
6 | ///
|
---|
7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
9 | /// the Free Software Foundation.
|
---|
10 | ///
|
---|
11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | /// GNU General Public License for more details.
|
---|
15 | ///
|
---|
16 | /// You should have received a copy of the GNU General Public License
|
---|
17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | ///
|
---|
20 | /// In addition this software uses the following components and/or licenses:
|
---|
21 | ///
|
---|
22 | /// =================================================================================
|
---|
23 | /// The Open Toolkit Library License
|
---|
24 | ///
|
---|
25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
26 | ///
|
---|
27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
29 | /// in the Software without restriction, including without limitation the rights to
|
---|
30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
32 | /// so, subject to the following conditions:
|
---|
33 | ///
|
---|
34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
35 | /// copies or substantial portions of the Software.
|
---|
36 | ///
|
---|
37 | /// =================================================================================
|
---|
38 | ///
|
---|
39 |
|
---|
40 | #region LGPL License
|
---|
41 | /*
|
---|
42 | This file is part of ILNumerics.Net Core Module.
|
---|
43 |
|
---|
44 | ILNumerics.Net Core Module is free software: you can redistribute it
|
---|
45 | and/or modify it under the terms of the GNU Lesser General Public
|
---|
46 | License as published by the Free Software Foundation, either version 3
|
---|
47 | of the License, or (at your option) any later version.
|
---|
48 |
|
---|
49 | ILNumerics.Net Core Module is distributed in the hope that it will be useful,
|
---|
50 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
51 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
52 | GNU Lesser General Public License for more details.
|
---|
53 |
|
---|
54 | You should have received a copy of the GNU Lesser General Public License
|
---|
55 | along with ILNumerics.Net Core Module.
|
---|
56 | If not, see <http://www.gnu.org/licenses/>.
|
---|
57 | */
|
---|
58 | #endregion
|
---|
59 |
|
---|
60 | using System;
|
---|
61 | using System.Collections.Generic;
|
---|
62 | using System.Text;
|
---|
63 | using System.Windows.Forms;
|
---|
64 |
|
---|
65 |
|
---|
66 | namespace ILNumerics.Data {
|
---|
67 | public class ILBinTree<T> {
|
---|
68 |
|
---|
69 | protected int m_count;
|
---|
70 | protected ILBinTreeNode<T> m_root;
|
---|
71 | public virtual bool Contains (T data) {
|
---|
72 | return Contains(m_root,data);
|
---|
73 | }
|
---|
74 | private bool Contains(ILBinTreeNode<T> _root, T data) {
|
---|
75 | if (object.Equals(_root.Data,data)) return true;
|
---|
76 | if (_root.LeftSon != null && Contains(_root.LeftSon,data)) return true;
|
---|
77 | if (_root.RightSon != null && Contains(_root.RightSon,data)) return true;
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 | //public ILBinTreeNode<T> Find(T data) {
|
---|
81 |
|
---|
82 | //}
|
---|
83 | //public ILBinTreeNode<T> AddLeftSon (ILBinTreeNode<T> node, T) {
|
---|
84 | // T ret = null;
|
---|
85 | // if (node.LeftSon == null) {
|
---|
86 |
|
---|
87 | // } else {
|
---|
88 |
|
---|
89 | // return
|
---|
90 | // }
|
---|
91 | //}
|
---|
92 |
|
---|
93 | }
|
---|
94 | public class ILBinTreeNode<T> {
|
---|
95 | internal T m_data;
|
---|
96 | internal ILBinTreeNode<T> m_father;
|
---|
97 | internal ILBinTreeNode<T> m_leftSon;
|
---|
98 | internal ILBinTreeNode<T> m_rightSon;
|
---|
99 |
|
---|
100 | public T Data {
|
---|
101 | get {
|
---|
102 | return m_data;
|
---|
103 | }
|
---|
104 | set {
|
---|
105 | m_data = value;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | public ILBinTreeNode<T> Father {
|
---|
109 | get {
|
---|
110 | return m_father;
|
---|
111 | }
|
---|
112 | set {
|
---|
113 | m_father = value;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | public ILBinTreeNode<T> LeftSon {
|
---|
117 | get {
|
---|
118 | return m_leftSon;
|
---|
119 | }
|
---|
120 | set {
|
---|
121 | m_leftSon = value;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | public ILBinTreeNode<T> RightSon {
|
---|
125 | get {
|
---|
126 | return m_rightSon;
|
---|
127 | }
|
---|
128 | set {
|
---|
129 | m_rightSon = value;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | public ILBinTreeNode() {}
|
---|
133 | public ILBinTreeNode(T data) {
|
---|
134 | m_data = data;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public bool IsLeaf() {
|
---|
138 | return m_leftSon == null && m_rightSon == null;
|
---|
139 | }
|
---|
140 | public int Height () {
|
---|
141 | if (IsLeaf()) return 0;
|
---|
142 | int lh = (m_leftSon != null) ? m_leftSon.Height() : 0;
|
---|
143 | int rh = (m_rightSon != null) ? m_rightSon.Height() : 0;
|
---|
144 | if (lh > rh) return lh + 1; else return rh + 1;
|
---|
145 | }
|
---|
146 | public override string ToString() {
|
---|
147 | return ToString(0, '-', BinTreeWalkingMode.PreOrder);
|
---|
148 | }
|
---|
149 | public string ToString(int indent, char indentChar, BinTreeWalkingMode walkingMode) {
|
---|
150 | StringBuilder sb = new StringBuilder();
|
---|
151 | switch (walkingMode) {
|
---|
152 | case BinTreeWalkingMode.PreOrder:
|
---|
153 | #region PreOrder
|
---|
154 | string s;
|
---|
155 | if (m_data != null) {
|
---|
156 | s = m_data.ToString();
|
---|
157 | } else {
|
---|
158 | s = "(null)";
|
---|
159 | }
|
---|
160 | sb.Append(s); indent += s.Length;
|
---|
161 | sb.Append(indentChar);
|
---|
162 | if (m_leftSon != null) {
|
---|
163 | sb.Append(m_leftSon.ToString(indent + 1, indentChar,walkingMode));
|
---|
164 | } else {
|
---|
165 | sb.Append(Environment.NewLine);
|
---|
166 | }
|
---|
167 | sb.Append(new String(' ',indent) + indentChar);
|
---|
168 | if (m_rightSon != null) {
|
---|
169 | sb.Append(m_rightSon.ToString(indent + 1, indentChar,walkingMode));
|
---|
170 | } else {
|
---|
171 | sb.Append(Environment.NewLine);
|
---|
172 | }
|
---|
173 | break;
|
---|
174 | #endregion
|
---|
175 | default:
|
---|
176 | throw new InvalidOperationException();
|
---|
177 | }
|
---|
178 | return sb.ToString();
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | }
|
---|
183 | public enum BinTreeWalkingMode {
|
---|
184 | PreOrder,
|
---|
185 | PostOrder,
|
---|
186 | InOrder
|
---|
187 | }
|
---|
188 |
|
---|
189 | }
|
---|