您的位置:首页 > Web前端 > Node.js

Find the nearest common ancestor of any two nodes in a binary search tree

2011-06-20 15:16 507 查看
自己写的,贴出来show一下,接受拍砖.

1. creat binary tree

  

static void CreateBinaryTree(int i, int? isOut = null)

{

 

 

if (binaryRootNode == null)

{

 

 

BinaryNode node = new BinaryNode();

node.data = i;

}

 

 

else

{

 

 

BinaryNode currentNode = binaryRootNode;

 

 

BinaryNode addNode = new BinaryNode();

 

 

while (true)

{

 

 

if (i < currentNode.data)

{

 

 

if (currentNode.left == null)

{

addNode.data = i;

addNode.parent = currentNode;

currentNode.left = addNode;

 

 

break;

}

 

 

else

{

currentNode = currentNode.left;

}

}

 

 

else if (i > currentNode.data)

{

 

 

if (currentNode.right == null)

{

addNode.data = i;

addNode.parent = currentNode;

currentNode.right = addNode;

 

 

break;

}

 

 

else

{

currentNode = currentNode.right;

}

}

 

 

else

{

 

 

break;

}

 

}

 

 

if (isOut != null)

{

findedNode = addNode;

}

}

}

 

 

2.get the nearest Common Ancestor

 

 

 

 

 

 

static int GetNearestCommonAncestor(BinaryNode childNode1, BinaryNode childNode2)

{

 

 

BinaryNode currentNode = binaryRootNode;//root node

 

 

if ((childNode1.data < currentNode.data && childNode2.data > currentNode.data) || (childNode1.data > currentNode.data && childNode2.data < currentNode.data))

{

 

 

return currentNode.data;

}

 

 

else if (childNode1.data == currentNode.data || childNode2.data == currentNode.data)

{

 

 

return currentNode.data; //in this situation, I think there is not exist ancestor

}

 

 

//get ancestor for childNode1

 

 

List<int> ancestorListForChildNode1 = new List<int>();

 

 

int rootData=currentNode.data;

currentNode = childNode1;

 

 

while (rootData != currentNode.data)

{

 

 

if (currentNode.parent.data > currentNode.data)

{

ancestorListForChildNode1.Add(currentNode.parent.data);

}

 

 

else

{

ancestorListForChildNode1.Insert(0, currentNode.parent.data);

}

currentNode = currentNode.parent;

}

 

 

//find ancerstor for childNode2 and check the parent node if exist in the ancestorListForChildNode1

currentNode = childNode2;

 

 

 

while (rootData != currentNode.data)

{

 

 

int low = 0;

 

 

int high = ancestorListForChildNode1.Count;

 

 

int tempParent = currentNode.parent.data;

 

 

while (low < high)

{

 

 

int mid=(low + high) / 2;

 

 

if (ancestorListForChildNode1[mid] > tempParent)

{

high = (low + high) / 2;

}

 

 

else if (ancestorListForChildNode1[mid] < tempParent)

{

low = (low + high) / 2;

}

 

 

else

{

 

 

return tempParent;

}

 

 

if (mid == 0 || mid == ancestorListForChildNode1.Count - 1)

{

 

 

break;

}

}

currentNode = currentNode.parent;

}

 

 

return rootData;

}

 

3. main method

 

 

 

 

 

static

 

 

void Main(string[] args)

 

 

{

binaryRootNode.data = 30;

 

 

 

for (int
i = 2; i < 60; i += 4)

{

CreateBinaryTree(i);

}

CreateBinaryTree(12);

CreateBinaryTree(11);

CreateBinaryTree(13,25);

 

 

 

BinaryNode
childNode1 = findedNode;

CreateBinaryTree(25, 3);

 

 

 

BinaryNode
childNode2 = findedNode;

 

 

 

int
nAncestor= GetNearestCommonAncestor(childNode1, childNode2);

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  search tree null string