BinaryTree homemade OOP code written in C#
$begingroup$
I have been working on a homemade binary-tree and want to optimize my coding style. Is there anything I could improve? Working in .NET Core 3.0
.
using System;
namespace BinaryTree
{
[Serializable]
public class BinaryTree<T> where T:IComparable, new()
{
private Node<T> _parentNode;
public BinaryTree()
{
_parentNode = new Node<T>();
}
public void Add(T item)
{
_parentNode.Add(item);
}
public void Remove(T item)
{
_parentNode.Remove(item);
}
public int Contains(T item)
{
return _parentNode.Contains(item);
}
public void Clear()
{
_parentNode = new Node<T>();
}
[Serializable]
private class Node<T> where T:IComparable, new()
{
private (T data, int count) _item;
private Node(T item)
{
_item = (item, 1);
}
public Node(){}
private void Set(T data)
{
if (_item.data.Equals(default(T)))
{
_item = (data, 1);
return;
}
if (data.Equals(_item.data))
{
_item.count++;
}
else
{
Add(data);
}
}
public void Remove(T data)
{
if (data.Equals(_item.data))
{
if (_item.count > 1)
{
_item.count--;
}
else
{
if (_parent == null)
{
// Unallowed to remove root element.
return;
}
var replacement = new Node<T>(data)
{
_smaller = _smaller,
_bigger = _bigger,
_parent = _parent
};
if (_parent._item.data.CompareTo(_item.data) == 1)
{
_parent._smaller = replacement;
}
else
{
_parent._bigger = replacement;
}
}
}
else
{
if (data.CompareTo(_item.data) == 1)
{
_bigger?.Remove(data);
if (_bigger != null) _bigger._parent = this;
}
else
{
_smaller?.Remove(data);
if (_smaller != null) _smaller._parent = this;
}
}
}
public void Add(T item)
{
if (_item.data.Equals(default(T)))
{
Set(item);
return;
}
if (item.CompareTo(_item.data)==1)
{
(_bigger=CreateOrReturnNode(_bigger)).Set(item);
}
else
{
(_smaller=CreateOrReturnNode(_smaller)).Set(item);
}
}
public int Contains(T value)
{
if (_item.data.Equals(value))
{
return _item.count;
}
if (value.CompareTo(_item.data).Equals(1))
{
return _bigger?.Contains(value) ?? 0;
}
return _smaller?.Contains(value) ?? 0;
}
private static Node<T> CreateOrReturnNode (Node<T> node = null)
{
return node ?? new Node<T>();
}
private Node<T> _parent;
private Node<T> _bigger;
private Node<T> _smaller;
}
}
}
c# .net .net-core
New contributor
$endgroup$
add a comment |
$begingroup$
I have been working on a homemade binary-tree and want to optimize my coding style. Is there anything I could improve? Working in .NET Core 3.0
.
using System;
namespace BinaryTree
{
[Serializable]
public class BinaryTree<T> where T:IComparable, new()
{
private Node<T> _parentNode;
public BinaryTree()
{
_parentNode = new Node<T>();
}
public void Add(T item)
{
_parentNode.Add(item);
}
public void Remove(T item)
{
_parentNode.Remove(item);
}
public int Contains(T item)
{
return _parentNode.Contains(item);
}
public void Clear()
{
_parentNode = new Node<T>();
}
[Serializable]
private class Node<T> where T:IComparable, new()
{
private (T data, int count) _item;
private Node(T item)
{
_item = (item, 1);
}
public Node(){}
private void Set(T data)
{
if (_item.data.Equals(default(T)))
{
_item = (data, 1);
return;
}
if (data.Equals(_item.data))
{
_item.count++;
}
else
{
Add(data);
}
}
public void Remove(T data)
{
if (data.Equals(_item.data))
{
if (_item.count > 1)
{
_item.count--;
}
else
{
if (_parent == null)
{
// Unallowed to remove root element.
return;
}
var replacement = new Node<T>(data)
{
_smaller = _smaller,
_bigger = _bigger,
_parent = _parent
};
if (_parent._item.data.CompareTo(_item.data) == 1)
{
_parent._smaller = replacement;
}
else
{
_parent._bigger = replacement;
}
}
}
else
{
if (data.CompareTo(_item.data) == 1)
{
_bigger?.Remove(data);
if (_bigger != null) _bigger._parent = this;
}
else
{
_smaller?.Remove(data);
if (_smaller != null) _smaller._parent = this;
}
}
}
public void Add(T item)
{
if (_item.data.Equals(default(T)))
{
Set(item);
return;
}
if (item.CompareTo(_item.data)==1)
{
(_bigger=CreateOrReturnNode(_bigger)).Set(item);
}
else
{
(_smaller=CreateOrReturnNode(_smaller)).Set(item);
}
}
public int Contains(T value)
{
if (_item.data.Equals(value))
{
return _item.count;
}
if (value.CompareTo(_item.data).Equals(1))
{
return _bigger?.Contains(value) ?? 0;
}
return _smaller?.Contains(value) ?? 0;
}
private static Node<T> CreateOrReturnNode (Node<T> node = null)
{
return node ?? new Node<T>();
}
private Node<T> _parent;
private Node<T> _bigger;
private Node<T> _smaller;
}
}
}
c# .net .net-core
New contributor
$endgroup$
add a comment |
$begingroup$
I have been working on a homemade binary-tree and want to optimize my coding style. Is there anything I could improve? Working in .NET Core 3.0
.
using System;
namespace BinaryTree
{
[Serializable]
public class BinaryTree<T> where T:IComparable, new()
{
private Node<T> _parentNode;
public BinaryTree()
{
_parentNode = new Node<T>();
}
public void Add(T item)
{
_parentNode.Add(item);
}
public void Remove(T item)
{
_parentNode.Remove(item);
}
public int Contains(T item)
{
return _parentNode.Contains(item);
}
public void Clear()
{
_parentNode = new Node<T>();
}
[Serializable]
private class Node<T> where T:IComparable, new()
{
private (T data, int count) _item;
private Node(T item)
{
_item = (item, 1);
}
public Node(){}
private void Set(T data)
{
if (_item.data.Equals(default(T)))
{
_item = (data, 1);
return;
}
if (data.Equals(_item.data))
{
_item.count++;
}
else
{
Add(data);
}
}
public void Remove(T data)
{
if (data.Equals(_item.data))
{
if (_item.count > 1)
{
_item.count--;
}
else
{
if (_parent == null)
{
// Unallowed to remove root element.
return;
}
var replacement = new Node<T>(data)
{
_smaller = _smaller,
_bigger = _bigger,
_parent = _parent
};
if (_parent._item.data.CompareTo(_item.data) == 1)
{
_parent._smaller = replacement;
}
else
{
_parent._bigger = replacement;
}
}
}
else
{
if (data.CompareTo(_item.data) == 1)
{
_bigger?.Remove(data);
if (_bigger != null) _bigger._parent = this;
}
else
{
_smaller?.Remove(data);
if (_smaller != null) _smaller._parent = this;
}
}
}
public void Add(T item)
{
if (_item.data.Equals(default(T)))
{
Set(item);
return;
}
if (item.CompareTo(_item.data)==1)
{
(_bigger=CreateOrReturnNode(_bigger)).Set(item);
}
else
{
(_smaller=CreateOrReturnNode(_smaller)).Set(item);
}
}
public int Contains(T value)
{
if (_item.data.Equals(value))
{
return _item.count;
}
if (value.CompareTo(_item.data).Equals(1))
{
return _bigger?.Contains(value) ?? 0;
}
return _smaller?.Contains(value) ?? 0;
}
private static Node<T> CreateOrReturnNode (Node<T> node = null)
{
return node ?? new Node<T>();
}
private Node<T> _parent;
private Node<T> _bigger;
private Node<T> _smaller;
}
}
}
c# .net .net-core
New contributor
$endgroup$
I have been working on a homemade binary-tree and want to optimize my coding style. Is there anything I could improve? Working in .NET Core 3.0
.
using System;
namespace BinaryTree
{
[Serializable]
public class BinaryTree<T> where T:IComparable, new()
{
private Node<T> _parentNode;
public BinaryTree()
{
_parentNode = new Node<T>();
}
public void Add(T item)
{
_parentNode.Add(item);
}
public void Remove(T item)
{
_parentNode.Remove(item);
}
public int Contains(T item)
{
return _parentNode.Contains(item);
}
public void Clear()
{
_parentNode = new Node<T>();
}
[Serializable]
private class Node<T> where T:IComparable, new()
{
private (T data, int count) _item;
private Node(T item)
{
_item = (item, 1);
}
public Node(){}
private void Set(T data)
{
if (_item.data.Equals(default(T)))
{
_item = (data, 1);
return;
}
if (data.Equals(_item.data))
{
_item.count++;
}
else
{
Add(data);
}
}
public void Remove(T data)
{
if (data.Equals(_item.data))
{
if (_item.count > 1)
{
_item.count--;
}
else
{
if (_parent == null)
{
// Unallowed to remove root element.
return;
}
var replacement = new Node<T>(data)
{
_smaller = _smaller,
_bigger = _bigger,
_parent = _parent
};
if (_parent._item.data.CompareTo(_item.data) == 1)
{
_parent._smaller = replacement;
}
else
{
_parent._bigger = replacement;
}
}
}
else
{
if (data.CompareTo(_item.data) == 1)
{
_bigger?.Remove(data);
if (_bigger != null) _bigger._parent = this;
}
else
{
_smaller?.Remove(data);
if (_smaller != null) _smaller._parent = this;
}
}
}
public void Add(T item)
{
if (_item.data.Equals(default(T)))
{
Set(item);
return;
}
if (item.CompareTo(_item.data)==1)
{
(_bigger=CreateOrReturnNode(_bigger)).Set(item);
}
else
{
(_smaller=CreateOrReturnNode(_smaller)).Set(item);
}
}
public int Contains(T value)
{
if (_item.data.Equals(value))
{
return _item.count;
}
if (value.CompareTo(_item.data).Equals(1))
{
return _bigger?.Contains(value) ?? 0;
}
return _smaller?.Contains(value) ?? 0;
}
private static Node<T> CreateOrReturnNode (Node<T> node = null)
{
return node ?? new Node<T>();
}
private Node<T> _parent;
private Node<T> _bigger;
private Node<T> _smaller;
}
}
}
c# .net .net-core
c# .net .net-core
New contributor
New contributor
New contributor
asked 13 mins ago
TheRealViraTheRealVira
1011
1011
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
TheRealVira is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213286%2fbinarytreet-homemade-oop-code-written-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
TheRealVira is a new contributor. Be nice, and check out our Code of Conduct.
TheRealVira is a new contributor. Be nice, and check out our Code of Conduct.
TheRealVira is a new contributor. Be nice, and check out our Code of Conduct.
TheRealVira is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213286%2fbinarytreet-homemade-oop-code-written-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown