Java版
import java.util.*;
class Node {
Integer value;
Node left,right;
public Node(Integer value) {
this.value = value;
}
}
class BinaryTree {
Node root;
public Node getRoot() {
return root;
}
public Node Creat(ArrayList<Integer> inputList) {
if (inputList == null || inputList.isEmpty()) {
return null;
}
Node node = null;
Integer value = inputList.remove(0);
if (value != null) {
node = new Node(value);
node.left = Creat(inputList);
node.right = Creat(inputList);
}
return node;
}
public void preOrderTraversal(Node root) {
if (root == null) {
return;
}
System.out.print(root.value);
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
void InOrder2(Node root)
{
Stack <Node> s = new Stack<>();
Node p = root;
while(p!=null || !s.empty())
{
if(p!=null)
{
s.push(p);
p=p.left;
}
else
{
p = s.pop();
System.out.println(p.value);
p=p.right;
}
}
}
public int Depth(Node root){
if(root==null){
return 0;
}
int m = Depth(root.left);
int n = Depth(root.right);
return m>n? m+1:n+1;
}
public int NodeCount(Node root){
if(root==null) return 0;
else return NodeCount(root.left)+NodeCount(root.right)+1;
}
int LeafCount(Node root)
{
if(root==null) return 0;
if(root.left==null && root.right==null) return 1;
else return LeafCount(root.left)+LeafCount(root.right);
}
int Node_1_Count(Node root)
{
if(root==null) return 0;
if((root.left==null)&&(root.right!=null)||(root.left!=null)&&(root.right==null))
return 1;
else
return LeafCount(root.left)+LeafCount(root.right);
}
void PrintAllPath(Node root, int path[], int pathlen)
{
int i;
if(root != null)
{
path[pathlen] = root.value;
if(root.left==null && root.right==null)
{
for(i = pathlen; i >= 0; i--){
System.out.println(path[i]);
}
System.out.println();
}
else
{
PrintAllPath(root.left, path, pathlen + 1);
PrintAllPath(root.right, path, pathlen + 1);
}
}
}
public void levelOrderTraversal (Node node) {
Queue<Node> queue = new LinkedList<Node>();
queue.offer(node);
while (!queue.isEmpty()) {
node = queue.poll();
System.out.print(node.value + " ");
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
ArrayList<ArrayList<Integer>> Print(Node pRoot) {
ArrayList<ArrayList<Integer>> thelist = new ArrayList<ArrayList<Integer>>();
if(pRoot==null)return thelist;
Queue<Node> queue = new LinkedList<Node>();
queue.offer(pRoot);
while(!queue.isEmpty()){
ArrayList<Integer> list = new ArrayList<Integer>();
int size = queue.size();
for(int i=0;i<size;i++){
pRoot = queue.poll();
list.add(pRoot.value);
if (pRoot.left != null) {
queue.offer(pRoot.left);
}
if (pRoot.right != null) {
queue.offer(pRoot.right);
}
}
thelist.add(list);
}
return thelist;
}
public Node insert_1(Node root, int val){
if(root==null)
return new Node(val);
if(root.value<val)
root.right = insert_1(root.right,val);
else if(root.value>val)
root.left = insert_1(root.left,val);
else
root.value = val;
return root;
}
public static Node insert_2(Node root, int val) {
if (root == null) {
return new Node(val);
}
Node temp = root;
Node curr;
while (root != null) {
curr = root;
if (root.value < val) {
root = root.right;
if(root==null){
curr.right = new Node(val);
return temp;
}
} else if (root.value > val) {
root = root.left;
if(root==null){
curr.left = new Node(val);
return temp;
}
}
else {
System.out.println("已经有这个值了");
return temp;
}
}
return temp;
}
}
public class A {
public static void main(String[] args) {
Integer[] input = new Integer[]{1, 2, 3, null, null, 4, null, null, 5, null, 6};
ArrayList inputList = new ArrayList(Arrays.asList(input));
BinaryTree tree = new BinaryTree();
Node root = tree.getRoot();
root= tree.insert_1(root, 3);
tree.insert_1(root, 8);
tree.insert_1(root, 2);
tree.insert_1(root, 1);
tree.insert_1(root, 9);
tree.insert_1(root, 0);
tree.insert_1(root, 6);
tree.insert_1(root, 7);
tree.insert_1(root, 4);
System.out.print("先序遍历:");
tree.preOrderTraversal(root);
System.out.println();
System.out.print("层次遍历:");
tree.levelOrderTraversal(root);
System.out.println();
System.out.print("层次打印:");
System.out.println(tree.Print(root));
System.out.print("深度:");
System.out.println(tree.Depth(root));
System.out.print("结点个数:");
System.out.println(tree.NodeCount(root));
System.out.println("叶结点的个数:");
System.out.println(tree.LeafCount(root));
System.out.println("度为1的结点个数:");
System.out.println(tree.Node_1_Count(root));
System.out.println("二叉树中从每个叶子结点到根结点的所有路径:");
int[] path = new int[256];
int pathlen=0;
tree.PrintAllPath(root,path,pathlen);
}
}
C++版(链表实现)
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
typedef struct Node
{
char data;
struct Node *lchild,*rchild;
}*BiTree,BiTNode;
void CreateBiTree(BiTree &T)
{
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else
{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void PreOrder(BiTree T)
{
if(T)
{
cout<<T->data;
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
void InOrder1(BiTree T)
{
if(T)
{
InOrder1(T->lchild);
cout<<T->data;
InOrder1(T->rchild);
}
}
void PostOrder(BiTree T)
{
if(T)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
cout<<T->data;
}
}
void InOrder2(BiTree T)
{
stack <BiTree>s;
BiTree p = T;
while(p || !s.empty())
{
if(p)
{
s.push(p);
p=p->lchild;
}
else
{
p=s.top();
cout<<p->data;
s.pop();
p=p->rchild;
}
}
}
void LevelOrder(BiTree T)
{
queue<BiTree> q;
BiTree p=T;
q.push(p);
while(!q.empty())
{
p=q.front();
cout<<q.front()->data;
q.pop();
if(p->lchild)
q.push(p->lchild);
if(p->rchild)
q.push(p->rchild);
}
}
void DblOrder(BiTree T)
{
if(T)
{
cout<<T->data;
DblOrder(T->lchild);
cout<<T->data;
DblOrder(T->rchild);
}
}
void Copy(BiTree T , BiTree &NewT)
{
if(T==NULL)
{
NewT=NULL;
return;
}
else
{
NewT=new BiTNode;
NewT->data=T->data;
Copy(T->lchild,NewT->lchild);
Copy(T->rchild,NewT->rchild);
}
}
int Depth(BiTree T)
{
if(T==NULL)
return 0;
else
{
int m=Depth(T->lchild);
int n=Depth(T->rchild);
if(m>n)
return (m+1);
else
return (n+1);
}
}
int Node_Count(BiTree T)
{
if(T==NULL)
return 0;
else
return Node_Count(T->lchild) + Node_Count(T->rchild) + 1 ;
}
int Leaf_Count(BiTree T)
{
if(!T)
return 0;
if(!T->lchild && !T->rchild)
return 1;
else
return Leaf_Count(T->lchild) + Leaf_Count(T->rchild);
}
int Node_1_Count(BiTree T)
{
if(!T)
return 0;
if((!T->lchild) && (T->rchild) || (T->lchild) && (!T->rchild))
return 1;
else
return Leaf_Count(T->lchild) + Leaf_Count(T->rchild);
}
void Print_All_Path(BiTree T, char path[], int pathlen)
{
int i;
if(T != NULL)
{
path[pathlen] = T->data;
if(T->lchild == NULL && T->rchild == NULL)
{
for(i = pathlen; i >= 0; i--)
cout << path[i] << " " ;
cout << endl;
}
else
{
Print_All_Path(T->lchild, path, pathlen + 1);
Print_All_Path(T->rchild, path, pathlen + 1);
}
}
}
void ExChangeTree(BiTree &T)
{
BiTree temp;
if(T!=NULL)
{
temp=T->lchild;
T->lchild=T->rchild;
T->rchild=temp;
ExChangeTree(T->lchild);
ExChangeTree(T->rchild);
}
}
int main()
{
BiTree T;
cout<<"先序遍历输入(以#结束):";
CreateBiTree(T);
cout<<"中序遍历(非递归):";
InOrder2(T);
cout<<endl<<"中序遍历输出:";
InOrder1(T);
cout<<endl<<"先序遍历输出:";
PreOrder(T);
cout<<endl<<"后序遍历输出:";
PostOrder(T);
cout<<endl<<"层次遍历输出:";
LevelOrder(T);
cout<<endl<<"树的深度:"<<Depth(T);
cout<<endl<<"结点的个数:"<<Node_Count(T);
cout<<endl<<"叶结点的个数:"<<Leaf_Count(T);
cout<<endl<<"度为1的结点个数:"<<Node_1_Count(T);
cout<<endl<<"二叉树中从每个叶子结点到根结点的所有路径:"<<endl;
char path[256];
int pathlen=0;
Print_All_Path(T,path,pathlen);
BiTree tem=T;
ExChangeTree(tem);
cout<<"先序遍历输出交换后的结果:";
PreOrder(tem);
cout<<endl<<"双序遍历输出:";
DblOrder(T);
return 0;
}