2012-07-24 38 views
0

我在C#中做了一个简单的程序来在二叉树中添加一个节点。 我有一个对象字段'根'来保存主父节点。这样每次我添加一个节点时,我都会通过检索父节点中的数据从树中遍历一个节点。对象在没有任何代理的情况下自动更新

这里是我的代码

public class BTNode 
    { 
     public int Value { get; set; }   
     public BTNode Left { get; set; } 
     public BTNode Right { get; set; } 
     public BTNode Root { get; set; } 
    } 


public class BinaryTree 
    { 
     public BinaryTree() 
     { 
      size = 0; 
      Root = null; //To hold the main Parent Node 
     } 
     int size; 
     object Root; 

     public void AddNode(int value) 
     { 
      BTNode NewNode = new BTNode(); 
      if (Root != null) 
      { 
       NewNode = (BTNode)Root; //If tree exists, Get the Root Node 
      } 

      while (NewNode.Root != null) 
      { 
       if (value < NewNode.Value) 
       { 
        NewNode.Root = NewNode.Left; 
       } 
       else if (value > NewNode.Value) 
       { 
        NewNode.Root = NewNode.Right; 
       } 
      } 

      size++; 
      NewNode.Value = value; //OBJECT 'ROOT' GETTING UPDATED AT THIS POINT 
      NewNode.Root = NewNode; //self pointer 
      NewNode.Left = null; 
      NewNode.Right = null; 

      if (size == 1) 
      { 
       Root = (object) NewNode; //if this is the Parent Node(First Node) 
      }        //then update the Root to be the parent Node 
     } 
    } 

我想抱着二叉树的唯一父节点“根”。我只想要执行的最后一行时,大小= 1,即如果它的第一个节点的树,但无论我做什么,Root都会针对每个节点进行更新。我正在努力知道为什么会发生这种情况,请帮助我。我在这里错过了任何的合作,逻辑。

+0

当你说'NewNode.Root = NewNode.Left;'你不是重新分配每个节点的'Root'吗?我认为相反,你只是想说'NewNode = NewNode.Left;' – mellamokb 2012-07-24 13:46:39

+0

我实际上创建了NewNode.Root,以便我可以将它用作参考或指向其子节点的指针。我想我可以像你说的那样编码。我不需要BTNode类中的Root属性。谢谢。 – Rasm 2012-07-24 15:21:54

回答

1
  1. 您的Root属性可以输入到BTNode。这样,您就不需要投它
  2. 线NewNode = (BTNode)Root;越来越根节点参考。您对NewNode所做的任何更改都会影响根节点。您是否知道值类型参考类型
  3. 我不明白你为什么要去(检查的Root),而不是(检查左或右节点)。

检查此解决方案,请。它使用一个简单的递归方法来放置新的节点:

public class BinaryTree 
    { 
     public BinaryTree() 
     { 
      size = 0; 
      Root = null; //To hold the main Parent Node 
     } 
     int size; 
     BTNode Root; 

     public void AddNode(int value) 
     { 
      size++; 
      BTNode NewNode = new BTNode() 
      { 
       Value = value 
      }; 

      if (this.Root == null) 
      { 
       this.Root = NewNode; 
       return; 
      } 

      this.PlaceNewNode(this.Root, NewNode); 
     } 

     public void PlaceNewNode(BTNode RootNode, BTNode NewNode) 
     { 
      if (NewNode.Value < RootNode.Value) 
      { 
       if (RootNode.Left != null) 
       { 
        PlaceNewNode(RootNode.Left, NewNode); 
       } 
       else 
       { 
        RootNode.Left = NewNode; 
        return; 
       } 
      } 
      else 
      { 
       if (RootNode.Right != null) 
       { 
        PlaceNewNode(RootNode.Right, NewNode); 
       } 
       else 
       { 
        RootNode.Right = NewNode; 
        return; 
       } 
      } 
     } 
    } 

希望这有助于。

Regards

+0

谢谢安德烈。我实际上首先尝试了BTNode类型的根,然后将其更改为一个对象。 我需要根节点,因为我想要添加到树中的新节点从树的根进行比较,而不是将它与最后插入的节点进行比较,所以每次添加一个节点时,我都会从根并在适当的地方插入节点。感谢您的回答,现在就马上进行检查。 – Rasm 2012-07-24 14:54:07

+0

我明白,修改代码非常有用,所以你可以保留根的引用。你最欢迎 – 2012-07-24 15:51:55

+0

*容易*,而不是*使用* – 2012-07-24 16:00:00

0

我觉得你对Root这个词的用法很混乱。一棵树只能有一个根对象。节点对象可以有一个父节点(除非它是根节点)。树结构很好地适用于递归代码。

下面是一些代码,它类似于什么安德烈公布。

public class BTNode 
{ 
    public BTNode() { } 
    public BTNode(int value) { Value = value; } 

    public int Value { get; set; } 
    public BTNode Left { get; set; } 
    public BTNode Right { get; set; } 
    public BTNode Parent { get; set; } 
} 

public class BinaryTree 
{ 
    public BinaryTree() { } 

    public BTNode Root { get; private set; } 
    public int Size { get; private set; } 

    public void AddNode(int value) 
    { 
     BTNode insertNode = new BTNode(value); 
     if (Root == null) 
      Root = insertNode; 
     else 
      AddNodeToTree(Root, insertNode); 
     Size++; 
    } 

    private void AddNodeToTree(BTNode parentNode, BTNode insertNode) 
    { 
     if (insertNode.Value >= parentNode.Value) 
     { 
      if (parentNode.Right != null) 
       AddNodeToTree(parentNode.Right, insertNode); 
      else 
      { 
       parentNode.Right = insertNode; 
       insertNode.Parent = parentNode; 
      } 
     } 
     else 
     { 
      if (parentNode.Left != null) 
       AddNodeToTree(parentNode.Left, insertNode); 
      else 
      { 
       parentNode.Left = insertNode; 
       insertNode.Parent = parentNode; 
      } 
     } 
    } 

    public BTNode FindNode(int value) 
    { 
     return FindNode(Root, value); 
    } 

    public BTNode FindNode(BTNode parent, int value) 
    { 
     BTNode node = null; 
     if (parent != null) 
     { 
      if (parent.Value == value) 
       node = parent; 
      else 
      { 
       if (parent.Value < value) 
        node = FindNode(parent.Right, value); 
       else 
        node = FindNode(parent.Left, value); 
      } 
     } 
     return node; 
    } 
} 
+0

谢谢mdm。我想存储二叉树的根,并在插入新节点时使用它来遍历树。感谢您的解决方案。你的两个代码都可以工作。 我应该为BTNode类中的属性root使用另一个名称。 – Rasm 2012-07-24 15:17:58

相关问题