C# Updating a Binary Search Tree of words
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
So, I have a Binary Search Tree implementation intended for HTML documents, which accepts a document name and some HTML content and constructs the tree of words along with their count in the document and the document name. The tree is then visualized inside a TreeView control like this:
- Root: html - d1, C: 4
- - Left: head - d1, C: 2
- - - Left: document - d1, C: 2
- - - - Left: body - d1, C: 2
- - Right: title - d1, C: 2
- - - Left: simple - d1, C: 2
- - - - Left: p - d1, C: 4
- - - - - Right: paragraphs - d1, C: 1
- - - Right: two - d1, C: 1
And the input HTML document looks like this (the stopwords and special characters are removed):
<html>
<head>
<title>
A Simple HTML Document
</title>
</head>
<body>
<p> This is a very simple HTML document</p>
<p>It only has two paragraphs</p>
</body>
</html>
Now, my task is to update (and expand) this tree using other documents. The problem is that the nodes with words that already exist in the tree need to be appended, instead of getting added as new ones, like this:
- Root: html - d1, C: 4, d2, C: 2
-- Left: head - d1, C: 2, d2, C: 2, d3, C:3 (and so on)
This is my main code for the tree:
public class Node
{
public string data;
public Node left { get; set; }
public Node right { get; set; }
public Node(string data)
{
this.data = data;
left = right = null;
}
}
public class Tree
{
public Node root;
public Tree()
{
root = null;
}
public void insert(string data, TreeView view)
{
Node newItem = new Node(data);
if (root == null)
{
root = newItem;
}
else
{
Node current = root;
Node parent = null;
while (current != null)
{
parent = current;
if (String.Compare(data, current.data) < 0)
{
current = current.left;
if (current == null)
{
parent.left = newItem;
}
}
else
{
current = current.right;
if (current == null)
{
parent.right = newItem;
}
}
}
}
}
public String search(string element, Node root)
{
Node current = root;
if (current == null)
{
return "Not found";
}
if (String.Compare(element, current.data) == 0)
{
return element;
}
if (String.Compare(element, current.data) < 0)
{
return this.search(element, current.left);
}
else
{
return this.search(element, current.right);
}
}
}
public void preOrder(Node node, TreeNode treeNode)
{
treeNode.Text += node.data;
if (node.left != null)
{
preOrder(node.left, treeNode.Nodes.Add("Left: "));
}
if (node.right != null)
{
preOrder(node.right, treeNode.Nodes.Add("Right: "));
}
}
void DisplayTree(Tree tree)
{
preOrder(tree.root, treeView1.Nodes.Add("Root: "));
}
The onclick method:
private void button6_Click(object sender, EventArgs e)
{
string d_name = textBox1.Text;
if(d_name == "")
{
MessageBox.Show("Please enter a document name.", "Indexing");
return;
}
Tree glb_tree = new Tree();
string data = richTextBox1.Text;
if(data == "")
{
MessageBox.Show("Missing input.", "Indexing");
return;
}
//creates a list of the needed words
List<string> dataList = createList(data, d_name);
glb_tree = createTree(dataList, treeView1, glb_tree, doc_names, d_name);
}
And the tree function:
public Tree createTree(List<string> input, TreeView treeView, Tree bst, List<string> doc_names, string cur_name)
{
int word_count = 1;
string res = "";
string tree_res = "";
var match = doc_names.FirstOrDefault(stringToCheck => stringToCheck.Contains(cur_name));
// Do not allow existing document names
if (match == null)
{
doc_names.Add(cur_name);
}
else
{
MessageBox.Show("Document Name already exists!", "Tree Create");
return null;
}
for (int i = 0; i < input.Count; i++)
{
for (int j = i + 1; j < input.Count; j++)
{
//Calculate word count
if (input[i] == input[j])
{
input[j] = "";
word_count++;
}
}
res = input[i] + " - " + cur_name + ", C:" + word_count;
//Some elements in word list are empty, skip them
if (res.StartsWith(" -"))
{
word_count = 1;
continue;
}
bst.insert(res, treeView);
word_count = 1;
}
DisplayTree(bst);
treeView1.ExpandAll();
return bst;
}
EDIT:
Never mind, solved my problem, it was actually pretty simple. I just needed to modify the list that goes to the tree as an input, and not modify the tree itself. So I just added a simple helper-function for appending new data:
public List<string> appendToList(List<string> cur_data, string data, string dname)
{
List<string> newDataList = createList(data, dname);
bool found = false;
if (newDataList != null && cur_data != null)
{
for(int i=0; i<newDataList.Count; i++)
{
found = false;
for(int j=0; j < cur_data.Count; j++)
{
string op1 = newDataList[i].Substring(0, newDataList[i].IndexOf(' '));
string op2 = cur_data[j].Substring(0, cur_data[j].IndexOf(' '));
if (op1.Equals(op2))
{
int wcsub = newDataList[i].IndexOf(':') + 2;
int wclength = newDataList[i].IndexOf(';') - wcsub;
string wc = newDataList[i].Substring(wcsub, wclength);
cur_data[j] += dname + ": " + wc + "; ";
found = true;
break;
}
}
if (!found)
{
cur_data.Add(newDataList[i]);
}
}
}
return cur_data;
}
c# tree treeview binary-search-tree insert-update
add a comment |
So, I have a Binary Search Tree implementation intended for HTML documents, which accepts a document name and some HTML content and constructs the tree of words along with their count in the document and the document name. The tree is then visualized inside a TreeView control like this:
- Root: html - d1, C: 4
- - Left: head - d1, C: 2
- - - Left: document - d1, C: 2
- - - - Left: body - d1, C: 2
- - Right: title - d1, C: 2
- - - Left: simple - d1, C: 2
- - - - Left: p - d1, C: 4
- - - - - Right: paragraphs - d1, C: 1
- - - Right: two - d1, C: 1
And the input HTML document looks like this (the stopwords and special characters are removed):
<html>
<head>
<title>
A Simple HTML Document
</title>
</head>
<body>
<p> This is a very simple HTML document</p>
<p>It only has two paragraphs</p>
</body>
</html>
Now, my task is to update (and expand) this tree using other documents. The problem is that the nodes with words that already exist in the tree need to be appended, instead of getting added as new ones, like this:
- Root: html - d1, C: 4, d2, C: 2
-- Left: head - d1, C: 2, d2, C: 2, d3, C:3 (and so on)
This is my main code for the tree:
public class Node
{
public string data;
public Node left { get; set; }
public Node right { get; set; }
public Node(string data)
{
this.data = data;
left = right = null;
}
}
public class Tree
{
public Node root;
public Tree()
{
root = null;
}
public void insert(string data, TreeView view)
{
Node newItem = new Node(data);
if (root == null)
{
root = newItem;
}
else
{
Node current = root;
Node parent = null;
while (current != null)
{
parent = current;
if (String.Compare(data, current.data) < 0)
{
current = current.left;
if (current == null)
{
parent.left = newItem;
}
}
else
{
current = current.right;
if (current == null)
{
parent.right = newItem;
}
}
}
}
}
public String search(string element, Node root)
{
Node current = root;
if (current == null)
{
return "Not found";
}
if (String.Compare(element, current.data) == 0)
{
return element;
}
if (String.Compare(element, current.data) < 0)
{
return this.search(element, current.left);
}
else
{
return this.search(element, current.right);
}
}
}
public void preOrder(Node node, TreeNode treeNode)
{
treeNode.Text += node.data;
if (node.left != null)
{
preOrder(node.left, treeNode.Nodes.Add("Left: "));
}
if (node.right != null)
{
preOrder(node.right, treeNode.Nodes.Add("Right: "));
}
}
void DisplayTree(Tree tree)
{
preOrder(tree.root, treeView1.Nodes.Add("Root: "));
}
The onclick method:
private void button6_Click(object sender, EventArgs e)
{
string d_name = textBox1.Text;
if(d_name == "")
{
MessageBox.Show("Please enter a document name.", "Indexing");
return;
}
Tree glb_tree = new Tree();
string data = richTextBox1.Text;
if(data == "")
{
MessageBox.Show("Missing input.", "Indexing");
return;
}
//creates a list of the needed words
List<string> dataList = createList(data, d_name);
glb_tree = createTree(dataList, treeView1, glb_tree, doc_names, d_name);
}
And the tree function:
public Tree createTree(List<string> input, TreeView treeView, Tree bst, List<string> doc_names, string cur_name)
{
int word_count = 1;
string res = "";
string tree_res = "";
var match = doc_names.FirstOrDefault(stringToCheck => stringToCheck.Contains(cur_name));
// Do not allow existing document names
if (match == null)
{
doc_names.Add(cur_name);
}
else
{
MessageBox.Show("Document Name already exists!", "Tree Create");
return null;
}
for (int i = 0; i < input.Count; i++)
{
for (int j = i + 1; j < input.Count; j++)
{
//Calculate word count
if (input[i] == input[j])
{
input[j] = "";
word_count++;
}
}
res = input[i] + " - " + cur_name + ", C:" + word_count;
//Some elements in word list are empty, skip them
if (res.StartsWith(" -"))
{
word_count = 1;
continue;
}
bst.insert(res, treeView);
word_count = 1;
}
DisplayTree(bst);
treeView1.ExpandAll();
return bst;
}
EDIT:
Never mind, solved my problem, it was actually pretty simple. I just needed to modify the list that goes to the tree as an input, and not modify the tree itself. So I just added a simple helper-function for appending new data:
public List<string> appendToList(List<string> cur_data, string data, string dname)
{
List<string> newDataList = createList(data, dname);
bool found = false;
if (newDataList != null && cur_data != null)
{
for(int i=0; i<newDataList.Count; i++)
{
found = false;
for(int j=0; j < cur_data.Count; j++)
{
string op1 = newDataList[i].Substring(0, newDataList[i].IndexOf(' '));
string op2 = cur_data[j].Substring(0, cur_data[j].IndexOf(' '));
if (op1.Equals(op2))
{
int wcsub = newDataList[i].IndexOf(':') + 2;
int wclength = newDataList[i].IndexOf(';') - wcsub;
string wc = newDataList[i].Substring(wcsub, wclength);
cur_data[j] += dname + ": " + wc + "; ";
found = true;
break;
}
}
if (!found)
{
cur_data.Add(newDataList[i]);
}
}
}
return cur_data;
}
c# tree treeview binary-search-tree insert-update
add a comment |
So, I have a Binary Search Tree implementation intended for HTML documents, which accepts a document name and some HTML content and constructs the tree of words along with their count in the document and the document name. The tree is then visualized inside a TreeView control like this:
- Root: html - d1, C: 4
- - Left: head - d1, C: 2
- - - Left: document - d1, C: 2
- - - - Left: body - d1, C: 2
- - Right: title - d1, C: 2
- - - Left: simple - d1, C: 2
- - - - Left: p - d1, C: 4
- - - - - Right: paragraphs - d1, C: 1
- - - Right: two - d1, C: 1
And the input HTML document looks like this (the stopwords and special characters are removed):
<html>
<head>
<title>
A Simple HTML Document
</title>
</head>
<body>
<p> This is a very simple HTML document</p>
<p>It only has two paragraphs</p>
</body>
</html>
Now, my task is to update (and expand) this tree using other documents. The problem is that the nodes with words that already exist in the tree need to be appended, instead of getting added as new ones, like this:
- Root: html - d1, C: 4, d2, C: 2
-- Left: head - d1, C: 2, d2, C: 2, d3, C:3 (and so on)
This is my main code for the tree:
public class Node
{
public string data;
public Node left { get; set; }
public Node right { get; set; }
public Node(string data)
{
this.data = data;
left = right = null;
}
}
public class Tree
{
public Node root;
public Tree()
{
root = null;
}
public void insert(string data, TreeView view)
{
Node newItem = new Node(data);
if (root == null)
{
root = newItem;
}
else
{
Node current = root;
Node parent = null;
while (current != null)
{
parent = current;
if (String.Compare(data, current.data) < 0)
{
current = current.left;
if (current == null)
{
parent.left = newItem;
}
}
else
{
current = current.right;
if (current == null)
{
parent.right = newItem;
}
}
}
}
}
public String search(string element, Node root)
{
Node current = root;
if (current == null)
{
return "Not found";
}
if (String.Compare(element, current.data) == 0)
{
return element;
}
if (String.Compare(element, current.data) < 0)
{
return this.search(element, current.left);
}
else
{
return this.search(element, current.right);
}
}
}
public void preOrder(Node node, TreeNode treeNode)
{
treeNode.Text += node.data;
if (node.left != null)
{
preOrder(node.left, treeNode.Nodes.Add("Left: "));
}
if (node.right != null)
{
preOrder(node.right, treeNode.Nodes.Add("Right: "));
}
}
void DisplayTree(Tree tree)
{
preOrder(tree.root, treeView1.Nodes.Add("Root: "));
}
The onclick method:
private void button6_Click(object sender, EventArgs e)
{
string d_name = textBox1.Text;
if(d_name == "")
{
MessageBox.Show("Please enter a document name.", "Indexing");
return;
}
Tree glb_tree = new Tree();
string data = richTextBox1.Text;
if(data == "")
{
MessageBox.Show("Missing input.", "Indexing");
return;
}
//creates a list of the needed words
List<string> dataList = createList(data, d_name);
glb_tree = createTree(dataList, treeView1, glb_tree, doc_names, d_name);
}
And the tree function:
public Tree createTree(List<string> input, TreeView treeView, Tree bst, List<string> doc_names, string cur_name)
{
int word_count = 1;
string res = "";
string tree_res = "";
var match = doc_names.FirstOrDefault(stringToCheck => stringToCheck.Contains(cur_name));
// Do not allow existing document names
if (match == null)
{
doc_names.Add(cur_name);
}
else
{
MessageBox.Show("Document Name already exists!", "Tree Create");
return null;
}
for (int i = 0; i < input.Count; i++)
{
for (int j = i + 1; j < input.Count; j++)
{
//Calculate word count
if (input[i] == input[j])
{
input[j] = "";
word_count++;
}
}
res = input[i] + " - " + cur_name + ", C:" + word_count;
//Some elements in word list are empty, skip them
if (res.StartsWith(" -"))
{
word_count = 1;
continue;
}
bst.insert(res, treeView);
word_count = 1;
}
DisplayTree(bst);
treeView1.ExpandAll();
return bst;
}
EDIT:
Never mind, solved my problem, it was actually pretty simple. I just needed to modify the list that goes to the tree as an input, and not modify the tree itself. So I just added a simple helper-function for appending new data:
public List<string> appendToList(List<string> cur_data, string data, string dname)
{
List<string> newDataList = createList(data, dname);
bool found = false;
if (newDataList != null && cur_data != null)
{
for(int i=0; i<newDataList.Count; i++)
{
found = false;
for(int j=0; j < cur_data.Count; j++)
{
string op1 = newDataList[i].Substring(0, newDataList[i].IndexOf(' '));
string op2 = cur_data[j].Substring(0, cur_data[j].IndexOf(' '));
if (op1.Equals(op2))
{
int wcsub = newDataList[i].IndexOf(':') + 2;
int wclength = newDataList[i].IndexOf(';') - wcsub;
string wc = newDataList[i].Substring(wcsub, wclength);
cur_data[j] += dname + ": " + wc + "; ";
found = true;
break;
}
}
if (!found)
{
cur_data.Add(newDataList[i]);
}
}
}
return cur_data;
}
c# tree treeview binary-search-tree insert-update
So, I have a Binary Search Tree implementation intended for HTML documents, which accepts a document name and some HTML content and constructs the tree of words along with their count in the document and the document name. The tree is then visualized inside a TreeView control like this:
- Root: html - d1, C: 4
- - Left: head - d1, C: 2
- - - Left: document - d1, C: 2
- - - - Left: body - d1, C: 2
- - Right: title - d1, C: 2
- - - Left: simple - d1, C: 2
- - - - Left: p - d1, C: 4
- - - - - Right: paragraphs - d1, C: 1
- - - Right: two - d1, C: 1
And the input HTML document looks like this (the stopwords and special characters are removed):
<html>
<head>
<title>
A Simple HTML Document
</title>
</head>
<body>
<p> This is a very simple HTML document</p>
<p>It only has two paragraphs</p>
</body>
</html>
Now, my task is to update (and expand) this tree using other documents. The problem is that the nodes with words that already exist in the tree need to be appended, instead of getting added as new ones, like this:
- Root: html - d1, C: 4, d2, C: 2
-- Left: head - d1, C: 2, d2, C: 2, d3, C:3 (and so on)
This is my main code for the tree:
public class Node
{
public string data;
public Node left { get; set; }
public Node right { get; set; }
public Node(string data)
{
this.data = data;
left = right = null;
}
}
public class Tree
{
public Node root;
public Tree()
{
root = null;
}
public void insert(string data, TreeView view)
{
Node newItem = new Node(data);
if (root == null)
{
root = newItem;
}
else
{
Node current = root;
Node parent = null;
while (current != null)
{
parent = current;
if (String.Compare(data, current.data) < 0)
{
current = current.left;
if (current == null)
{
parent.left = newItem;
}
}
else
{
current = current.right;
if (current == null)
{
parent.right = newItem;
}
}
}
}
}
public String search(string element, Node root)
{
Node current = root;
if (current == null)
{
return "Not found";
}
if (String.Compare(element, current.data) == 0)
{
return element;
}
if (String.Compare(element, current.data) < 0)
{
return this.search(element, current.left);
}
else
{
return this.search(element, current.right);
}
}
}
public void preOrder(Node node, TreeNode treeNode)
{
treeNode.Text += node.data;
if (node.left != null)
{
preOrder(node.left, treeNode.Nodes.Add("Left: "));
}
if (node.right != null)
{
preOrder(node.right, treeNode.Nodes.Add("Right: "));
}
}
void DisplayTree(Tree tree)
{
preOrder(tree.root, treeView1.Nodes.Add("Root: "));
}
The onclick method:
private void button6_Click(object sender, EventArgs e)
{
string d_name = textBox1.Text;
if(d_name == "")
{
MessageBox.Show("Please enter a document name.", "Indexing");
return;
}
Tree glb_tree = new Tree();
string data = richTextBox1.Text;
if(data == "")
{
MessageBox.Show("Missing input.", "Indexing");
return;
}
//creates a list of the needed words
List<string> dataList = createList(data, d_name);
glb_tree = createTree(dataList, treeView1, glb_tree, doc_names, d_name);
}
And the tree function:
public Tree createTree(List<string> input, TreeView treeView, Tree bst, List<string> doc_names, string cur_name)
{
int word_count = 1;
string res = "";
string tree_res = "";
var match = doc_names.FirstOrDefault(stringToCheck => stringToCheck.Contains(cur_name));
// Do not allow existing document names
if (match == null)
{
doc_names.Add(cur_name);
}
else
{
MessageBox.Show("Document Name already exists!", "Tree Create");
return null;
}
for (int i = 0; i < input.Count; i++)
{
for (int j = i + 1; j < input.Count; j++)
{
//Calculate word count
if (input[i] == input[j])
{
input[j] = "";
word_count++;
}
}
res = input[i] + " - " + cur_name + ", C:" + word_count;
//Some elements in word list are empty, skip them
if (res.StartsWith(" -"))
{
word_count = 1;
continue;
}
bst.insert(res, treeView);
word_count = 1;
}
DisplayTree(bst);
treeView1.ExpandAll();
return bst;
}
EDIT:
Never mind, solved my problem, it was actually pretty simple. I just needed to modify the list that goes to the tree as an input, and not modify the tree itself. So I just added a simple helper-function for appending new data:
public List<string> appendToList(List<string> cur_data, string data, string dname)
{
List<string> newDataList = createList(data, dname);
bool found = false;
if (newDataList != null && cur_data != null)
{
for(int i=0; i<newDataList.Count; i++)
{
found = false;
for(int j=0; j < cur_data.Count; j++)
{
string op1 = newDataList[i].Substring(0, newDataList[i].IndexOf(' '));
string op2 = cur_data[j].Substring(0, cur_data[j].IndexOf(' '));
if (op1.Equals(op2))
{
int wcsub = newDataList[i].IndexOf(':') + 2;
int wclength = newDataList[i].IndexOf(';') - wcsub;
string wc = newDataList[i].Substring(wcsub, wclength);
cur_data[j] += dname + ": " + wc + "; ";
found = true;
break;
}
}
if (!found)
{
cur_data.Add(newDataList[i]);
}
}
}
return cur_data;
}
c# tree treeview binary-search-tree insert-update
c# tree treeview binary-search-tree insert-update
edited Nov 25 '18 at 10:44
JadeBlue
asked Nov 23 '18 at 15:05
JadeBlueJadeBlue
134
134
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
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: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53449009%2fc-sharp-updating-a-binary-search-tree-of-words%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
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f53449009%2fc-sharp-updating-a-binary-search-tree-of-words%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