Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using Binary Trees

Using Binary Trees

Scheduled Pinned Locked Moved C#
csharpdata-structureshelptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aynka2000
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • A aynka2000

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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, like Tree(int value) 4) you don't need a root 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups