好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java实现二叉树遍历的三种方式

本文实例为大家分享了java实现二叉树遍历的具体代码,供大家参考,具体内容如下

二叉树如下:

遍历结果如下:

以下是实现代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

package bintree;

 

import java.util.stack;

 

/**

  * @author bin.zhang

  * @version 2017年8月29日 上午10:22:01

  */

public class bintreetraversal {

  public static void main(string[] args) {

   system.out.print( "前序:" );

   traversal.preorder();

   traversal.preorderrecursion(traversal.createbintree());

   system.out.print( "中序:" );

   traversal.inorder();

   traversal.inorderrecursion(traversal.createbintree());

   system.out.print( "后序:" );

   traversal.postorder();

   traversal.postorderrecursion(traversal.createbintree());

  }

}

 

/**

  * 节点数据结构

  *

  * @author bin.zhang

  * @version 2017年8月30日 上午11:49:38

  */

class bintreenode {

 

  bintreenode() {

  }

 

  bintreenode( char data, int flag, bintreenode lchild, bintreenode rchild) {

   this .data = data;

   this .flag = flag;

   this .lchild = lchild;

   this .rchild = rchild;

  }

 

  char data;

  int flag;

  bintreenode lchild, rchild;

 

}

 

class traversal {

 

  /**

   * 创建一棵二叉树

   *

   * @author bin.zhang

   * @return 根节点

   */

  public static bintreenode createbintree() {

   bintreenode r3 = new bintreenode( 'f' , 0 , null , null );

   bintreenode l2 = new bintreenode( 'd' , 0 , null , null );

   bintreenode r2 = new bintreenode( 'e' , 0 , null , r3);

   bintreenode l1 = new bintreenode( 'b' , 0 , l2, r2);

   bintreenode r1 = new bintreenode( 'c' , 0 , null , null );

   bintreenode t = new bintreenode( 'a' , 0 , l1, r1);

   return t;

  }

 

  // 前序

  public static void preorder() {

 

   bintreenode p = createbintree();

 

   stack<bintreenode> stack = new stack<bintreenode>();

 

   while (p != null || !stack.empty()) {

    if (p != null ) {

     system.out.print(p.data);

     stack.push(p);

     p = p.lchild;

    }

    else {

     p = stack.pop();

     p = p.rchild;

    }

   }

   system.out.println();

 

  }

 

  // 前序递归

  public static void preorderrecursion(bintreenode top) {

   if (top != null ) {

    system.out.println(top.data);

    preorderrecursion(top.lchild);

    preorderrecursion(top.rchild);

   }

  }

 

  // 中序

  public static void inorder() {

 

   bintreenode p = createbintree();

 

   stack<bintreenode> stack = new stack<bintreenode>();

 

   while (p != null || !stack.empty()) {

    if (p != null ) {

     stack.push(p);

     p = p.lchild;

    }

    else {

     p = stack.pop();

     system.out.print(p.data);

     p = p.rchild;

    }

   }

   system.out.println();

  }

 

  // 中序递归

  public static void inorderrecursion(bintreenode top) {

   if (top != null ) {

    inorderrecursion(top.lchild);

    system.out.println(top.data);

    inorderrecursion(top.rchild);

   }

  }

 

  // 后序

  public static void postorder() {

 

   bintreenode p = createbintree();

 

   stack<bintreenode> stack = new stack<bintreenode>(); // 初始化栈

 

   int mark = 1 ; // 转向标志

   while (p != null || !stack.empty()) { // 遍历

    if (p != null && mark != 0 ) {

     stack.push(p);

     p = p.lchild;

    } // 转向左子树

    else {

     p = stack.pop();

     p.flag++; // 退栈

     if (p.flag == 1 ) {

      stack.push(p);

      p = p.rchild;

      mark = 1 ;

     } // 转向右子树

     else if (p.flag == 2 && !stack.empty()) { // 输出结点

      system.out.print(p.data);

      mark = 0 ;

     }

     else if (p.flag == 2 && stack.empty()) { // 输出根结点并退出

      system.out.print(p.data);

      break ;

     }

    } // if-else

   } // while

   system.out.println();

  }

 

  // 后序递归

  public static void postorderrecursion(bintreenode top) {

   if (top != null ) {

    postorderrecursion(top.lchild);

    postorderrecursion(top.rchild);

    system.out.println(top.data);

   }

  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/zhangbinu/article/details/77679901

查看更多关于java实现二叉树遍历的三种方式的详细内容...

  阅读:9次