Using Binary Trees
-
I tried Using binary tree in c# I just tried the insert,delete and find method using recursion But I'm not sure whether is correct and how to use it in main Can somebody please tell is it correct Thanks for checking below is my coding using System; class Node { public int skey; public Node nleft; public Node nRight; public Node left { get {return nleft; } set {nleft=value; } } public Node right { get {return nRight; } set {nRight=value; } } public int vkey { get {return skey; } set {skey=value; } } } class Tree { private Node root; public Tree() { root = null; } //find Node search_binary_tree(Node node,int key) { if (node==null) return null; // not found if (key < node.vkey) return search_binary_tree(node.left, key); else if (key > node.vkey) return search_binary_tree(node.right, key); else return node; } void InsertNode(Node node,Node newNode ) { if (root == null) { root =newNode; } else if (newNode.vkey <= node.vkey) InsertNode(node.left, newNode); else InsertNode(node.right, newNode); } void DeleteNode(Node node) { Node temp = node; if (node.left == null) { node = node.right; temp=null; } else if (node.right == null) { node = node.left; temp=null; } else { // Node has two children - get max of left subtree temp = node.left; while (temp.right != null) { temp = temp.right; } node.vkey = temp.vkey; DeleteNode(temp); } } void traverse_binary_tree(Node treenode) { if (treenode!=null) { traverse_binary_tree(treenode.left); Console.WriteLine(treenode.vkey); traverse_binary_tree(treenode.right); } } So how can i apply main method for this Please Help:) Ayn
-
I tried Using binary tree in c# I just tried the insert,delete and find method using recursion But I'm not sure whether is correct and how to use it in main Can somebody please tell is it correct Thanks for checking below is my coding using System; class Node { public int skey; public Node nleft; public Node nRight; public Node left { get {return nleft; } set {nleft=value; } } public Node right { get {return nRight; } set {nRight=value; } } public int vkey { get {return skey; } set {skey=value; } } } class Tree { private Node root; public Tree() { root = null; } //find Node search_binary_tree(Node node,int key) { if (node==null) return null; // not found if (key < node.vkey) return search_binary_tree(node.left, key); else if (key > node.vkey) return search_binary_tree(node.right, key); else return node; } void InsertNode(Node node,Node newNode ) { if (root == null) { root =newNode; } else if (newNode.vkey <= node.vkey) InsertNode(node.left, newNode); else InsertNode(node.right, newNode); } void DeleteNode(Node node) { Node temp = node; if (node.left == null) { node = node.right; temp=null; } else if (node.right == null) { node = node.left; temp=null; } else { // Node has two children - get max of left subtree temp = node.left; while (temp.right != null) { temp = temp.right; } node.vkey = temp.vkey; DeleteNode(temp); } } void traverse_binary_tree(Node treenode) { if (treenode!=null) { traverse_binary_tree(treenode.left); Console.WriteLine(treenode.vkey); traverse_binary_tree(treenode.right); } } So how can i apply main method for this Please Help:) Ayn
Your implementation has several flaws. 1) You don't need two classes Tree and Node, you can merge them into one. 2) methods like AddNode(), RemoveNode() and Search() have to be
public
3) the contructor of a Node/Tree should ideally have a value parameter, likeTree(int value)
4) you don't need aroot
node in your tree, left and right childs are enough 5) when adding/searching/removing check for < and >= and not just > and < Once you fix that, you may use it like that:Tree binTree = new Tree(5);
binTree.Add(10);
binTree.Add(3);There are lots of bintree examples on the internet, you can almost copy them 1 to 1, even the Java ones ;) regards