生活随笔
收集整理的這篇文章主要介紹了
数据结构与算法之二叉树的先序遍历,中序遍历,后序遍历
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹的先序遍歷,中序遍歷,后移遍歷
目錄
實(shí)現(xiàn)二叉樹的先序,中序,后序遍歷,包括遞歸方式和非遞歸方式在二叉樹中找到一個(gè)節(jié)點(diǎn)的后繼節(jié)點(diǎn)
1. 實(shí)現(xiàn)二叉樹的先序,中序,后序遍歷,包括遞歸方式和非遞歸方式
1. 先序遍歷,中序遍歷,后序遍歷遞歸版
public static void preOrderRecur(Node head
) {if (head
== null
) {return;}System
.out
.print(head
.value
+ " ");preOrderRecur(head
.left
);preOrderRecur(head
.right
);}public static void inOrderRecur(Node head
) {if (head
== null
) {return;}inOrderRecur(head
.left
);System
.out
.print(head
.value
+ " ");inOrderRecur(head
.right
);}public static void posOrderRecur(Node head
) {if (head
== null
) {return;}posOrderRecur(head
.left
);posOrderRecur(head
.right
);System
.out
.print(head
.value
+ " ");}
2. 先序遍歷,中序遍歷,后序遍歷非遞歸版
public static void preOrderUnRecur(Node head
) {System
.out
.print("pre-order: ");if (head
!= null
) {Stack
<Node> stack
= new Stack<Node>();stack
.add(head
);while (!stack
.isEmpty()) {head
= stack
.pop();System
.out
.print(head
.value
+ " ");if (head
.right
!= null
) {stack
.push(head
.right
);}if (head
.left
!= null
) {stack
.push(head
.left
);}}}System
.out
.println();}public static void inOrderUnRecur(Node head
) {System
.out
.print("in-order: ");if (head
!= null
) {Stack
<Node> stack
= new Stack<Node>();while (!stack
.isEmpty() || head
!= null
) {if (head
!= null
) {stack
.push(head
);head
= head
.left
;} else {head
= stack
.pop();System
.out
.print(head
.value
+ " ");head
= head
.right
;}}}System
.out
.println();}public static void posOrderUnRecur1(Node head
) {System
.out
.print("pos-order: ");if (head
!= null
) {Stack
<Node> s1
= new Stack<Node>();Stack
<Node> s2
= new Stack<Node>();s1
.push(head
);while (!s1
.isEmpty()) {head
= s1
.pop();s2
.push(head
);if (head
.left
!= null
) {s1
.push(head
.left
);}if (head
.right
!= null
) {s1
.push(head
.right
);}}while (!s2
.isEmpty()) {System
.out
.print(s2
.pop().value
+ " ");}}System
.out
.println();}public static void posOrderUnRecur2(Node h
) {System
.out
.print("pos-order: ");if (h
!= null
) {Stack
<Node> stack
= new Stack<Node>();stack
.push(h
);Node c
= null
;while (!stack
.isEmpty()) {c
= stack
.peek();if (c
.left
!= null
&& h
!= c
.left
&& h
!= c
.right
) {stack
.push(c
.left
);} else if (c
.right
!= null
&& h
!= c
.right
) {stack
.push(c
.right
);} else {System
.out
.print(stack
.pop().value
+ " ");h
= c
;}}}System
.out
.println();}
3. 先序,中序,后序遍歷非遞歸解析
先序遍歷
1. 如果節(jié)點(diǎn)不為null,則參加棧來存儲(chǔ)節(jié)點(diǎn)。
2. 先將頭節(jié)點(diǎn)添加到棧。
3. 當(dāng)棧不為null時(shí),從棧中彈出一個(gè)頭節(jié)點(diǎn)
4. 由棧特性先進(jìn)后出和先序遍歷中左右知,先添加右節(jié)點(diǎn),再添加左節(jié)點(diǎn),這樣彈出的時(shí)候就是先左節(jié)點(diǎn),然后右節(jié)點(diǎn)。
中序遍歷
如果head不為null,則創(chuàng)建棧結(jié)構(gòu)進(jìn)行存儲(chǔ)。當(dāng)棧或者h(yuǎn)ead不為null時(shí),如果head不為null,則將它的左孩子全部壓入棧。當(dāng)head==null時(shí),則彈出棧頂節(jié)點(diǎn),將head指向它的右孩子。總結(jié)就是:當(dāng)前節(jié)點(diǎn)為null,從棧中拿一個(gè)節(jié)點(diǎn),打印,當(dāng)前節(jié)點(diǎn)向右移。當(dāng)前節(jié)點(diǎn)不為null,將它壓入棧,當(dāng)前節(jié)點(diǎn)向左移; 后序遍歷
由先序遍歷為中左右,后序遍歷為左右中,我們可以通過兩個(gè)棧,使進(jìn)入棧1的順序?yàn)橹杏易?#xff0c;然后將棧1元素放入棧2,就實(shí)現(xiàn)了左右中的結(jié)構(gòu)。如果head不為null,創(chuàng)建兩個(gè)棧s1,s2,。將head先壓入s1。當(dāng)棧s1不為null時(shí),棧s1彈出一個(gè)元素,將彈出的元素加入棧s2(即先序遍歷打印的位置)判斷head左節(jié)點(diǎn)是否為null,不為null就加入s1,判斷head右節(jié)點(diǎn)是否為null,不為null就加入s1.(先加左,后加右,出來就是先右,后左)打印出s2中的元素即可。
2. 在二叉樹中找到一個(gè)節(jié)點(diǎn)的后繼節(jié)點(diǎn)
題目描述
思路:
node節(jié)點(diǎn)如果有右孩子,則右孩子的最左節(jié)點(diǎn)就是后繼節(jié)點(diǎn)。如1的后繼節(jié)點(diǎn)是右孩子3的最左節(jié)點(diǎn)6node節(jié)點(diǎn)如果沒有右孩子,則往上找,找到當(dāng)前節(jié)點(diǎn)是父節(jié)點(diǎn)的左孩子就停,那個(gè)父節(jié)點(diǎn)就是后繼節(jié)點(diǎn)。如5的后繼節(jié)點(diǎn)是1的左孩子是2,所以5的后繼節(jié)點(diǎn)是1 備注:前驅(qū)的找法:
node節(jié)點(diǎn)如果有左孩子,那么左孩子最右節(jié)點(diǎn)就是前驅(qū)節(jié)點(diǎn),如1的前驅(qū)節(jié)點(diǎn)是左孩子2的最右節(jié)點(diǎn)5如果node節(jié)點(diǎn)沒有左孩子,則往上找,找到當(dāng)前節(jié)點(diǎn)是父節(jié)點(diǎn)的右孩子就停,那個(gè)父節(jié)點(diǎn)就是前驅(qū)節(jié)點(diǎn),如6的前驅(qū)節(jié)點(diǎn)是1的右孩子3,所以6的前驅(qū)節(jié)點(diǎn)是1
代碼實(shí)現(xiàn)
public class Code_SuccessorNode {public static class Node {public int value
;public Node left
;public Node right
;public Node parent
;public Node(int data
) {this.value
= data
;}}public static Node
getSuccessorNode(Node node
) {if (node
== null
) {return node
;}if (node
.right
!= null
) {return getLeftMost(node
.right
);} else {Node parent
= node
.parent
;while (parent
!= null
&& parent
.left
!= node
) {node
= parent
;parent
= node
.parent
;}return parent
;}}public static Node
getLeftMost(Node node
) {if (node
== null
) {return node
;}while (node
.left
!= null
) {node
= node
.left
;}return node
;}public static void main(String
[] args
) {Node head
= new Node(6);head
.parent
= null
;head
.left
= new Node(3);head
.left
.parent
= head
;head
.left
.left
= new Node(1);head
.left
.left
.parent
= head
.left
;head
.left
.left
.right
= new Node(2);head
.left
.left
.right
.parent
= head
.left
.left
;head
.left
.right
= new Node(4);head
.left
.right
.parent
= head
.left
;head
.left
.right
.right
= new Node(5);head
.left
.right
.right
.parent
= head
.left
.right
;head
.right
= new Node(9);head
.right
.parent
= head
;head
.right
.left
= new Node(8);head
.right
.left
.parent
= head
.right
;head
.right
.left
.left
= new Node(7);head
.right
.left
.left
.parent
= head
.right
.left
;head
.right
.right
= new Node(10);head
.right
.right
.parent
= head
.right
;Node test
= head
.left
.left
;System
.out
.println(test
.value
+ " next: " + getSuccessorNode(test
).value
);test
= head
.left
.left
.right
;System
.out
.println(test
.value
+ " next: " + getSuccessorNode(test
).value
);test
= head
.left
;System
.out
.println(test
.value
+ " next: " + getSuccessorNode(test
).value
);test
= head
.left
.right
;System
.out
.println(test
.value
+ " next: " + getSuccessorNode(test
).value
);test
= head
.left
.right
.right
;System
.out
.println(test
.value
+ " next: " + getSuccessorNode(test
).value
);test
= head
;System
.out
.println(test
.value
+ " next: " + getSuccessorNode(test
).value
);test
= head
.right
.left
.left
;System
.out
.println(test
.value
+ " next: " + getSuccessorNode(test
).value
);test
= head
.right
.left
;System
.out
.println(test
.value
+ " next: " + getSuccessorNode(test
).value
);test
= head
.right
;System
.out
.println(test
.value
+ " next: " + getSuccessorNode(test
).value
);test
= head
.right
.right
; System
.out
.println(test
.value
+ " next: " + getSuccessorNode(test
));}}
總結(jié)
以上是生活随笔為你收集整理的数据结构与算法之二叉树的先序遍历,中序遍历,后序遍历的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。