目錄
首先寫一個二叉樹的C#實現
public class BinNode
{
public int Element;
public BinNode Left;
public BinNode Right;
public BinNode(int element
{
this
this
this
}
public bool IsLeaf()
{
return this
}
}
static void PreOrder(BinNode root)
{
if (root == null)
return;
//visit current node
Console
PreOrder(root
PreOrder(root
}
static void PostOrder(BinNode root)
{
if (root == null)
return;
PostOrder(root
PostOrder(root
//visit current node
Console
}
static void InOrder(BinNode root)
{
if (root == null)
return;
InOrder(root
//visit current node
Console
InOrder(root
}
我們發現
有
算法
static void PrintTree
{
if (root == null) return;
BinNode tmp = null;
Queue queue = new Queue();
queue
while (queue
{
tmp = (BinNode)queue
Console
if (tmp
queue
if (tmp
queue
}
}
話說
算法
如果沒有Queue給我們用
public class Link
{
public Link Next;
public BinNode Data;
public Link(Link next
{
this
this
}
}
看過了Queue的實現
對於單鏈表而言
當first指針走不下去了
static void PrintTree
{
if (root == null) return;
Link head = new Link(null
Link first = head;
Link second = head;
while (first != null)
{
if (first
{
second
second = second
}
if (first
{
second
second = second
}
Console
first = first
}
}
平衡二叉樹的定義
算法思路
static int GetDepth(BinNode root)
{
if (root == null)
return
int leftLength = GetDepth(root
int rightLength = GetDepth(root
return (leftLength > rightLength
leftLength : rightLength) +
}
注意這裡的+
static bool IsBalanceTree(BinNode root)
{
if (root == null)
return true;
int leftLength = GetDepth(root
int rightLength = GetDepth(root
int distance = leftLength > rightLength
leftLength
if (distance >
return false;
else
return IsBalanceTree(root
}
上述程序的邏輯是
本題網上有很多算法
算法
static bool GetPositionByNode(BinNode root
{
if (root == null)
return false;
if (root == node)
{
stack
return true;
}
if (GetPositionByNode(root
{
stack
return true;
}
return false;
}
然後我們要同時彈出這兩個容器的元素
static BinNode FindParentNode(BinNode root
{
Stack stack
GetPositionByNode(root
Stack stack
GetPositionByNode(root
BinNode tempNode = null;
while (stack
{
tempNode = (BinNode)stack
stack
}
return tempNode;
}
算法
我們選擇一個
////
////
////
////
如果要找的是
我們發現
這時
由上面觀察
static bool GetPositionByNode(BinNode root
{
if (root == null)
return false;
if (root == node)
return true;
int temp = pos;
//這麼寫很別扭
pos = temp *
if (GetPositionByNode(root
{
return true;
}
else
{
//找不到左邊找右邊
pos = temp *
return GetPositionByNode(root
}
}
static int FindParentPosition(int larger
{
if (larger == smaller) return larger;
int left = GetLen(larger)
while (left >
{
larger = larger >>
left
}
while (larger != smaller)
{
larger = larger >>
smaller = smaller >>
}
return smaller;
}
static int GetLen(int num)
{
int length =
while (num !=
{
num = num >>
length++;
}
return length;
}
函數GetNodeByPosition的思想是
static BinNode GetNodeByPosition(BinNode root
{
if (num ==
int pow = (int)Math
//第一個位不算
num
while (pow >
{
if ((num
root = root
else
root = root
pow
}
return root;
}
總結上面的
static BinNode FindParentNode(BinNode root
{
int pos
GetPositionByNode(root
int pos
GetPositionByNode(root
int parentposition =
if (pos
{
parentposition = FindParentPosition(pos
}
else //pos
{
parentposition = FindParentPosition(pos
}
return GetNodeByPosition(root
}
算法思想
下面詳細分析第
這個是最簡單的
前序遍歷是root
因為在第一個while循環中
static void PreOrder(BinNode root)
{
Stack stack = new Stack();
BinNode temp = root;
//入棧
while (temp != null)
{
Console
if (temp
stack
temp = temp
}
//出棧
while (stack
{
temp = (BinNode)stack
Console
while (temp != null)
{
if (temp
stack
temp = temp
}
}
}
//後序遍歷比較麻煩
static void PostOrder(BinNode root)
{
Stack stack = new Stack();
BinNode temp = root;
//入棧
while (temp != null)
{
if (temp != null)
stack
temp = temp
}
//出棧
while (stack
{
BinNode lastvisit = temp;
temp = (BinNode)stack
if (temp
{
Console
}
else if (temp
{
stack
temp = temp
stack
while (temp != null)
{
if (temp
stack
temp = temp
}
}
}
}
//中序遍歷
static void InOrder(BinNode root)
{
Stack stack = new Stack();
BinNode temp = root;
//入棧
while (temp != null)
{
if (temp != null)
stack
temp = temp
}
//出棧
while (stack
{
temp = (BinNode)stack
Console
if (temp
{
temp = temp
stack
while (temp != null)
{
if (temp
stack
temp = temp
}
}
}
}
算法思想
為此
此外
static void FindBinNode(BinNode root
{
if (root == null)
return;
stack
//Leaf
if (root
{
if (root
{
Stack tempStack = new Stack();
while (stack
{
tempStack
}
while (tempStack
{
Console
stack
}
Console
}
}
if (root
FindBinNode(root
if (root
FindBinNode(root
stack
}
算法思想
//// arr[
//// arr[
//// arr[
相應編碼如下
public static void InsertArrayIntoTree(int[] arr
{
root = new BinNode(arr[pos]
root
//if Left value less than arr length
if (pos *
{
return;
}
else
{
InsertArrayIntoTree(arr
}
//if Right value less than arr length
if (pos *
{
return;
}
else
{
root
InsertArrayIntoTree(arr
}
}
比如
算法思想
由於不能使用動態數組
public static bool VerifyArrayOfBST(int[] arr
{
if (arr == null || arr
{
return false;
}
int root = arr[length + start
int i = start;
for (; i < length
{
if (arr[i] >= root)
break;
}
int j = i;
for (; j < length
{
if (arr[j] < root)
return false;
}
bool left = true;
if (i > start)
{
left = VerifyArrayOfBST(arr
}
bool right = true;
if (j > i)
{
right = VerifyArrayOfBST(arr
}
return left
}
算法
static void PreOrder(ref BinNode root)
{
if (root == null)
return;
//visit current node
BinNode temp = root
root
root
PreOrder(ref root
PreOrder(ref root
}
算法
static void PreOrder
{
if (root == null)
return;
Stack stack = new Stack();
stack
while (stack
{
//visit current node
BinNode temp = root
root
root
if (root
stack
if (root
stack
}
}
算法思想
static BinNode Find(BinNode root)
{
BinNode min = FindMinNode(root);
BinNode max = FindMaxNode(root);
double find = (double)(min
return FindNode(root
}
static BinNode FindMinNode(BinNode root)
{
BinNode min = root;
while (min
{
min = min
}
return min;
}
static BinNode FindMaxNode(BinNode root)
{
BinNode max = root;
while (max
{
max = max
}
return max;
}
遞歸尋找BST的節點
static BinNode FindNode(BinNode root
{
//如果小於相等
if (root
{
if (root
return root;
BinNode find = FindNode(root
//不一定找得到
return find
root : find;
}
//如果大於
else //temp
{
if (root
return root;
BinNode find = FindNode(root
//不一定找得到
return find
root : find;
}
}
////
////
////
////
轉變為Link
算法思想
static void ConvertNodeToLink(BinNode root
{
if (root == null)
return;
BinNode temp = root;
if (temp
ConvertNodeToLink(temp
//visit current node
link
link = link
if (temp
ConvertNodeToLink(temp
}
但是我們發現
static DoubleLink ReverseDoubleLink(BinNode root
{
ConvertNodeToLink(root
DoubleLink temp = link;
while (temp
{
temp = temp
}
return temp;
}
這麼寫有點蠢
算法
此外
代碼如下所示
link
link = link
那麼
為此
link
link = link
這樣
算法代碼如下所示
static void ConvertNodeToLink
{
if (root == null)
return;
BinNode temp = root;
if (temp
ConvertNodeToLink
//visit current node
link
link = link
if (temp
ConvertNodeToLink
}
以下算法屬於二叉樹的基本概念
玩二叉樹
唉
From:http://tw.wingwit.com/Article/program/sjjg/201405/30936.html