二叉树遍历


http://blog.csdn.net/pi9nc/article/details/13008511

 二叉树:

     遍历规则:

     先序遍历:
       1.访问根节点

 2. 先序遍历根节点的左子树

 3. 先序遍历根节点的右子树
    中序遍历:

       1.中序遍历根节点的左子树

 2.访问根节点

 3.中序遍历根节点的右子树
            后续遍历:
 1.后序遍历根节点的左子树
 2.后序遍历根节点的右子树
   3.访问根节点 

  612x265

  这是程序2所确定的二叉树图:

  403x156

 

 java实现:

public class BinaryTree {

private class Node {

int key;
String name;

Node leftChild;
Node rightChild;

//Node parent;

Node(int key, String name) {

this.key = key;
this.name = name;

}

public String toString() {

return name + " has the key " + key;

/*
* return name + " has the key " + key + "\nLeft Child: " + leftChild +
* "\nRight Child: " + rightChild + "\n";
*/

}

}

Node root;

public void addNode(int key, String name) {

// Create a new Node and initialize it

Node newNode = new Node(key, name);

// If there is no root this becomes root

if (root == null) {

root = newNode;

} else {

// Set root as the Node we will start
// with as we traverse the tree

Node focusNode = root;

// Future parent for our new Node

Node parent;

while (true) {

// root is the top parent so we start
// there

parent = focusNode;

// Check if the new node should go on
// the left side of the parent node

if (key < focusNode.key) {

// Switch focus to the left child

focusNode = focusNode.leftChild;

// If the left child has no children

if (focusNode == null) {

// then place the new node on the left of it

parent.leftChild = newNode;
return; // All Done

}

} else { // If we get here put the node on the right

focusNode = focusNode.rightChild;

// If the right child has no children

if (focusNode == null) {

// then place the new node on the right of it

parent.rightChild = newNode;
return; // All Done

}

}

}
}

}

// All nodes are visited in ascending order
// Recursion is used to go to one node and
// then go to its child nodes and so forth

public void inOrderTraverseTree(Node focusNode) {

if (focusNode != null) {

// Traverse the left node

inOrderTraverseTree(focusNode.leftChild);

// Visit the currently focused on node

System.out.println(focusNode);

// Traverse the right node

inOrderTraverseTree(focusNode.rightChild);

}

}

public void preorderTraverseTree(Node focusNode) {

if (focusNode != null) {

System.out.println(focusNode);

preorderTraverseTree(focusNode.leftChild);
preorderTraverseTree(focusNode.rightChild);

}

}

public void postOrderTraverseTree(Node focusNode) {

if (focusNode != null) {

postOrderTraverseTree(focusNode.leftChild);
postOrderTraverseTree(focusNode.rightChild);

System.out.println(focusNode);

}

}

public Node findNode(int key) {

// Start at the top of the tree

Node focusNode = root;

// While we haven't found the Node
// keep looking

while (focusNode.key != key) {

// If we should search to the left

if (key < focusNode.key) {

// Shift the focus Node to the left child

focusNode = focusNode.leftChild;

} else {

// Shift the focus Node to the right child

focusNode = focusNode.rightChild;

}

// The node wasn't found

if (focusNode == null)
return null;

}

return focusNode;

}

// TODO
/*
int depth(Node u) {
int d = 0;
while (u != r) {
u = u.parent;
d++;
}
return d;
}
*/

int size(Node u) {
if (u == null) return 0;
return 1 + size(u.leftChild) + size(u.rightChild);
}

public static void main(String[] args) {

BinaryTree theTree = new BinaryTree();

theTree.addNode(50, "Boss");

theTree.addNode(25, "Vice President");

theTree.addNode(15, "Office Manager");

theTree.addNode(30, "Secretary");

theTree.addNode(75, "Sales Manager");

theTree.addNode(85, "Salesman 1");

// Different ways to traverse binary trees

System.out.println("中序遍历");
theTree.inOrderTraverseTree(theTree.root);

System.out.println("先序遍历");
theTree.preorderTraverseTree(theTree.root);

System.out.println("后序遍历");
theTree.postOrderTraverseTree(theTree.root);

// Find the node with key 75

System.out.println("\nNode with the key 75");

System.out.println(theTree.findNode(75));

}
import java.util.Stack;
import java.util.HashMap;

public class BinTree {
private char date;
private BinTree lchild;
private BinTree rchild;

public BinTree(char c) {
date = c;
}

// 先序遍历递归
public static void preOrder(BinTree t) {
if (t == null) {
return;
}
System.out.print(t.date);
preOrder(t.lchild);
preOrder(t.rchild);
}

// 中序遍历递归
public static void InOrder(BinTree t) {
if (t == null) {
return;
}
InOrder(t.lchild);
System.out.print(t.date);
InOrder(t.rchild);
}

// 后序遍历递归
public static void PostOrder(BinTree t) {
if (t == null) {
return;
}
PostOrder(t.lchild);
PostOrder(t.rchild);
System.out.print(t.date);
}

// 先序遍历非递归
public static void preOrder2(BinTree t) {
Stack<BinTree> s = new Stack<BinTree>();
while (t != null || !s.empty()) {
while (t != null) {
System.out.print(t.date);
s.push(t);
t = t.lchild;
}
if (!s.empty()) {
t = s.pop();
t = t.rchild;
}
}
}

// 中序遍历非递归
public static void InOrder2(BinTree t) {
Stack<BinTree> s = new Stack<BinTree>();
while (t != null || !s.empty()) {
while (t != null) {
s.push(t);
t = t.lchild;
}
if (!s.empty()) {
t = s.pop();
System.out.print(t.date);
t = t.rchild;
}
}
}

// 后序遍历非递归
public static void PostOrder2(BinTree t) {
Stack<BinTree> s = new Stack<BinTree>();
Stack<Integer> s2 = new Stack<Integer>();
Integer i = new Integer(1);
while (t != null || !s.empty()) {
while (t != null) {
s.push(t);
s2.push(new Integer(0));
t = t.lchild;
}
while (!s.empty() && s2.peek().equals(i)) {
s2.pop();
System.out.print(s.pop().date);
}

if (!s.empty()) {
s2.pop();
s2.push(new Integer(1));
t = s.peek();
t = t.rchild;
}
}
}

public static void main(String[] args) {
BinTree b1 = new BinTree('a');
BinTree b2 = new BinTree('b');
BinTree b3 = new BinTree('c');
BinTree b4 = new BinTree('d');
BinTree b5 = new BinTree('e');

/**
* a
* / /
* b c
* / /
* d e
*/
b1.lchild = b2;
b1.rchild = b3;
b2.lchild = b4;
b2.rchild = b5;

BinTree.preOrder(b1);
System.out.println();
BinTree.preOrder2(b1);
System.out.println();
BinTree.InOrder(b1);
System.out.println();
BinTree.InOrder2(b1);
System.out.println();
BinTree.PostOrder(b1);
System.out.println();
BinTree.PostOrder2(b1);
}
}   

http://www.jianshu.com/p/49c8cfd07410

 

http://blog.csdn.net/pi9nc/article/details/13008511

http://blog.csdn.net/hackbuteer1/article/details/6583988 

 

posted @ 2017-03-03 00:35  托马斯布莱克  阅读(181)  评论(0编辑  收藏  举报