Skip to content

Commit 3eab7e1

Browse files
committed
Offloaded the Tree Nodes class from the main Trees Class
1 parent 64f3af6 commit 3eab7e1

File tree

4 files changed

+171
-179
lines changed

4 files changed

+171
-179
lines changed

DataStructures/Trees/AVLTree.cs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,9 @@
55

66
namespace DataStructures.Trees
77
{
8-
/// <summary>
9-
/// AVL Tree Node.
10-
/// </summary>
11-
12-
public class AVLTreeNode<T> : BSTNode<T> where T : IComparable<T>
13-
{
14-
private int _height = 0;
15-
16-
public AVLTreeNode() : this(default(T), 0, null, null, null) { }
17-
public AVLTreeNode(T value) : this(value, 0, null, null, null) { }
18-
public AVLTreeNode(T value, int height, AVLTreeNode<T> parent, AVLTreeNode<T> left, AVLTreeNode<T> right)
19-
{
20-
base.Value = value;
21-
Height = height;
22-
Parent = parent;
23-
LeftChild = left;
24-
RightChild = right;
25-
}
26-
27-
public virtual int Height
28-
{
29-
get { return this._height; }
30-
set { this._height = value; }
31-
}
32-
33-
public new AVLTreeNode<T> Parent
34-
{
35-
get { return (AVLTreeNode<T>)base.Parent; }
36-
set { base.Parent = value; }
37-
}
38-
39-
public new AVLTreeNode<T> LeftChild
40-
{
41-
get { return (AVLTreeNode<T>)base.LeftChild; }
42-
set { base.LeftChild = value; }
43-
}
44-
45-
public new AVLTreeNode<T> RightChild
46-
{
47-
get { return (AVLTreeNode<T>)base.RightChild; }
48-
set { base.RightChild = value; }
49-
}
50-
}
51-
52-
53-
/*********************************************************************/
54-
55-
568
/// <summary>
579
/// AVL Tree Data Structure.
5810
/// </summary>
59-
6011
public class AVLTree<T> : BinarySearchTree<T> where T : IComparable<T>
6112
{
6213
public new AVLTreeNode<T> Root

DataStructures/Trees/AVLTreeNode.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace DataStructures.Trees
2+
{
3+
/// <summary>
4+
/// AVL Tree Node.
5+
/// </summary>
6+
public class AVLTreeNode<T> : BSTNode<T> where T : System.IComparable<T>
7+
{
8+
private int _height = 0;
9+
10+
public AVLTreeNode() : this(default(T), 0, null, null, null) { }
11+
public AVLTreeNode(T value) : this(value, 0, null, null, null) { }
12+
public AVLTreeNode(T value, int height, AVLTreeNode<T> parent, AVLTreeNode<T> left, AVLTreeNode<T> right)
13+
{
14+
base.Value = value;
15+
Height = height;
16+
Parent = parent;
17+
LeftChild = left;
18+
RightChild = right;
19+
}
20+
21+
public virtual int Height
22+
{
23+
get { return this._height; }
24+
set { this._height = value; }
25+
}
26+
27+
public new AVLTreeNode<T> Parent
28+
{
29+
get { return (AVLTreeNode<T>)base.Parent; }
30+
set { base.Parent = value; }
31+
}
32+
33+
public new AVLTreeNode<T> LeftChild
34+
{
35+
get { return (AVLTreeNode<T>)base.LeftChild; }
36+
set { base.LeftChild = value; }
37+
}
38+
39+
public new AVLTreeNode<T> RightChild
40+
{
41+
get { return (AVLTreeNode<T>)base.RightChild; }
42+
set { base.RightChild = value; }
43+
}
44+
}
45+
}

DataStructures/Trees/BinarySearchTree.cs

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,15 @@
11
using System;
2-
using System.Linq;
32
using System.Collections;
43
using System.Collections.Generic;
54

65
using DataStructures.Common;
76

87
namespace DataStructures.Trees
98
{
10-
/// <summary>
11-
/// The binary search tree node.
12-
/// </summary>
13-
14-
public class BSTNode<T> : IComparable<BSTNode<T>> where T : IComparable<T>
15-
{
16-
private T _value;
17-
private BSTNode<T> _parent;
18-
private BSTNode<T> _left;
19-
private BSTNode<T> _right;
20-
21-
public BSTNode() : this(default(T), 0, null, null, null) { }
22-
public BSTNode(T value) : this(value, 0, null, null, null) { }
23-
public BSTNode(T value, int subTreeSize, BSTNode<T> parent, BSTNode<T> left, BSTNode<T> right)
24-
{
25-
Value = value;
26-
Parent = parent;
27-
LeftChild = left;
28-
RightChild = right;
29-
}
30-
31-
public virtual T Value
32-
{
33-
get { return this._value; }
34-
set { this._value = value; }
35-
}
36-
37-
public virtual BSTNode<T> Parent
38-
{
39-
get { return this._parent; }
40-
set { this._parent = value; }
41-
}
42-
43-
public virtual BSTNode<T> LeftChild
44-
{
45-
get { return this._left; }
46-
set { this._left = value; }
47-
}
48-
49-
public virtual BSTNode<T> RightChild
50-
{
51-
get { return this._right; }
52-
set { this._right = value; }
53-
}
54-
55-
/// <summary>
56-
/// Checks whether this node has any children.
57-
/// </summary>
58-
public virtual bool HasChildren
59-
{
60-
get { return (this.ChildrenCount > 0); }
61-
}
62-
63-
/// <summary>
64-
/// Checks whether this node has left child.
65-
/// </summary>
66-
public virtual bool HasLeftChild
67-
{
68-
get { return (this.LeftChild != null); }
69-
}
70-
71-
/// <summary>
72-
/// Checks whether this node has right child.
73-
/// </summary>
74-
public virtual bool HasRightChild
75-
{
76-
get { return (this.RightChild != null); }
77-
}
78-
79-
/// <summary>
80-
/// Checks whether this node is the left child of it's parent.
81-
/// </summary>
82-
public virtual bool IsLeftChild
83-
{
84-
get { return (this.Parent != null && this.Parent.LeftChild == this); }
85-
}
86-
87-
/// <summary>
88-
/// Checks whether this node is the left child of it's parent.
89-
/// </summary>
90-
public virtual bool IsRightChild
91-
{
92-
get { return (this.Parent != null && this.Parent.RightChild == this); }
93-
}
94-
95-
/// <summary>
96-
/// Checks whether this node is a leaf node.
97-
/// </summary>
98-
public virtual bool IsLeafNode
99-
{
100-
get { return (this.ChildrenCount == 0); }
101-
}
102-
103-
/// <summary>
104-
/// Returns number of direct descendents: 0, 1, 2 (none, left or right, or both).
105-
/// </summary>
106-
/// <returns>Number (0,1,2)</returns>
107-
public virtual int ChildrenCount
108-
{
109-
get
110-
{
111-
int count = 0;
112-
113-
if (this.HasLeftChild)
114-
count++;
115-
if (this.HasRightChild)
116-
count++;
117-
118-
return count;
119-
}
120-
}
121-
122-
/// <summary>
123-
/// Compares to.
124-
/// </summary>
125-
public virtual int CompareTo(BSTNode<T> other)
126-
{
127-
if (other == null)
128-
return -1;
129-
130-
return this.Value.CompareTo(other.Value);
131-
}
132-
}//end-of-bstnode
133-
134-
135-
/******************************************************************************/
136-
137-
1389
/// <summary>
13910
/// Implements a generic Binary Search Tree data structure.
14011
/// </summary>
14112
/// <typeparam name="T">Type of elements.</typeparam>
142-
14313
public class BinarySearchTree<T> : IBinarySearchTree<T> where T : IComparable<T>
14414
{
14515
/// <summary>
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+

2+
namespace DataStructures.Trees
3+
{
4+
/// <summary>
5+
/// The binary search tree node.
6+
/// </summary>
7+
public class BSTNode<T> : System.IComparable<BSTNode<T>> where T : System.IComparable<T>
8+
{
9+
private T _value;
10+
private BSTNode<T> _parent;
11+
private BSTNode<T> _left;
12+
private BSTNode<T> _right;
13+
14+
public BSTNode() : this(default(T), 0, null, null, null) { }
15+
public BSTNode(T value) : this(value, 0, null, null, null) { }
16+
public BSTNode(T value, int subTreeSize, BSTNode<T> parent, BSTNode<T> left, BSTNode<T> right)
17+
{
18+
Value = value;
19+
Parent = parent;
20+
LeftChild = left;
21+
RightChild = right;
22+
}
23+
24+
public virtual T Value
25+
{
26+
get { return this._value; }
27+
set { this._value = value; }
28+
}
29+
30+
public virtual BSTNode<T> Parent
31+
{
32+
get { return this._parent; }
33+
set { this._parent = value; }
34+
}
35+
36+
public virtual BSTNode<T> LeftChild
37+
{
38+
get { return this._left; }
39+
set { this._left = value; }
40+
}
41+
42+
public virtual BSTNode<T> RightChild
43+
{
44+
get { return this._right; }
45+
set { this._right = value; }
46+
}
47+
48+
/// <summary>
49+
/// Checks whether this node has any children.
50+
/// </summary>
51+
public virtual bool HasChildren
52+
{
53+
get { return (this.ChildrenCount > 0); }
54+
}
55+
56+
/// <summary>
57+
/// Checks whether this node has left child.
58+
/// </summary>
59+
public virtual bool HasLeftChild
60+
{
61+
get { return (this.LeftChild != null); }
62+
}
63+
64+
/// <summary>
65+
/// Checks whether this node has right child.
66+
/// </summary>
67+
public virtual bool HasRightChild
68+
{
69+
get { return (this.RightChild != null); }
70+
}
71+
72+
/// <summary>
73+
/// Checks whether this node is the left child of it's parent.
74+
/// </summary>
75+
public virtual bool IsLeftChild
76+
{
77+
get { return (this.Parent != null && this.Parent.LeftChild == this); }
78+
}
79+
80+
/// <summary>
81+
/// Checks whether this node is the left child of it's parent.
82+
/// </summary>
83+
public virtual bool IsRightChild
84+
{
85+
get { return (this.Parent != null && this.Parent.RightChild == this); }
86+
}
87+
88+
/// <summary>
89+
/// Checks whether this node is a leaf node.
90+
/// </summary>
91+
public virtual bool IsLeafNode
92+
{
93+
get { return (this.ChildrenCount == 0); }
94+
}
95+
96+
/// <summary>
97+
/// Returns number of direct descendents: 0, 1, 2 (none, left or right, or both).
98+
/// </summary>
99+
/// <returns>Number (0,1,2)</returns>
100+
public virtual int ChildrenCount
101+
{
102+
get
103+
{
104+
int count = 0;
105+
106+
if (this.HasLeftChild)
107+
count++;
108+
if (this.HasRightChild)
109+
count++;
110+
111+
return count;
112+
}
113+
}
114+
115+
/// <summary>
116+
/// Compares to.
117+
/// </summary>
118+
public virtual int CompareTo(BSTNode<T> other)
119+
{
120+
if (other == null)
121+
return -1;
122+
123+
return this.Value.CompareTo(other.Value);
124+
}
125+
}//end-of-bstnode
126+
}

0 commit comments

Comments
 (0)