LongTree的正確分析理解

mybillliu發表於2005-01-04
LongTree的正確理解分析

在網上有一篇檔案介紹LongTreer的原理的,但是對於leftChildren和rightSiblings的講解似乎有些不完整。
leftChildren儲存某結點兒子在keys[]中的陣列索引值,同理rightSiblings儲存其本結點兄弟在keys[]中的陣列索引值。
透過一些測試讓我理解這一功能:
下面是從JiveMessage表中擷取的部分資料

messageID	ParentMessageID	threadID	forumID
2	NULL	         1	31
31	2	         1	31
46	2	         1	31
61	2	         1	31
62	2	         1	31
32	31	         1	31
47	46	         1	31
<p class="indent">

在LongTree中的addChild方法加入了部分程式碼,也修改了部分程式碼,目的是為了輸出leftChildren 、rightSiblings的數值

public void addChild(long parentKey, long newKey)
  {
    //	找到一個給定的節點parentKey的父結點
    char parentIndex = findKey(parentKey, (char) 1);
    //System.out.println("父節點索引號:" + (int)parentIndex + "-----父結點:" + parentKey +
    //   "-----子結點:" + (int)newKey);
    if (parentIndex == 0)
    {
      throw new IllegalArgumentException("Parent key " + parentKey +
                                         " not found when adding child " +
                                         newKey + ".");
    }

    keys[nextIndex] = newKey;
    leftChildren[nextIndex] = 0;
    rightSiblings[nextIndex] = 0;
    System.out.println("keys[" + (int) nextIndex + "] = " + (int) newKey);

    // Adjust references. Check to see if the parent has any children.
    if (leftChildren[parentIndex] == 0)
    {
      // No children, therefore make the new key the first child.
      leftChildren[parentIndex] = nextIndex;
      System.out.println("leftChildren[" + (int) parentIndex + "] = " +
                         (int) nextIndex);
    }
    else
    {
      // The parent has children, so find the right-most child.
      long siblingIndex = leftChildren[parentIndex];

      while (rightSiblings[new Long(siblingIndex).intValue()] != 0)
      {
        siblingIndex = rightSiblings[new Long(siblingIndex).intValue()];
      }

      // Add the new entry as a sibling of that last child.

      //rightSiblings[new Long(siblingIndex).intValue()] = nextIndex;

      int tmpint = new Long(siblingIndex).intValue();
      rightSiblings[tmpint] = nextIndex ;
      System.out.println("rightSiblings[" + tmpint +"] = "  + (int)nextIndex);
    }

    // Finally, increment nextIndex so it's ready for next add.
    nextIndex++;
  }
<p class="indent">

下面是輸出值

keys[2] = 31
leftChildren[1] = 2
keys[3] = 32
leftChildren[2] = 3
keys[4] = 46
rightSiblings[2] = 4
keys[5] = 47
leftChildren[4] = 5
keys[6] = 61
rightSiblings[4] = 6
keys[7] = 62
rightSiblings[6] = 7

得到了這麼一組值,該如何理解呢?
假設有
leftChildren[X] = Y
正確理解是
keys[Y]是keys[X]的一個兒子結點
同理
rightSiblings[X] = Y
正確理解是
keys[Y]是keys[X]的一個兄弟結點

也就是說leftChildren、rightSiblings儲存的是它們子結點、兄弟結點在keys[]中的陣列索引值而已,而不能說leftChildren儲存的是某一結點的子結點

時間有限,懶得去好好的整理文字,大家能理解就行

願和更多的對Jive有深入研究的朋友交流
QQ:45214493(對JIVE沒有研究者勿加)


相關文章