先序遍历
public static void preOrderRe(TreeNode biTree){//递归实现
System.out.println(biTree.value);
TreeNode leftTree = biTree.left;
if(leftTree != null)
{
preOrderRe(leftTree);
}
TreeNode rightTree = biTree.right;
if(rightTree != null){
preOrderRe(rightTree);
}
}
public static void preOrder(TreeNode biTree) { //非递归实现
Stack <TreeNode> stack = new Stack <TreeNode> ();
while (biTree != null || !stack.isEmpty()) {
while (biTree != null) {
System.out.println(biTree.value);
stack.push(biTree);
biTree = biTree.left;
}
if (!stack.isEmpty()) {
biTree = stack.pop();
biTree = biTree.right;
}
}
}
中序遍历
public static void midOrderRe(TreeNode biTree) { //中序遍历递归实现
if (biTree == null) return;
else {
midOrderRe(biTree.left);
System.out.println(biTree.value);
midOrderRe(biTree.right);
}
}
public static void midOrder(TreeNode biTree) { //中序遍历费递归实现
Stack <TreeNode> stack = new Stack <TreeNode>();
while (biTree != null || !stack.isEmpty()) {
while (biTree != null) {
stack.push(biTree);
biTree = biTree.left;
}
if (!stack.isEmpty()) {
biTree = stack.pop();
System.out.println(biTree.value);
biTree = biTree.right;
}
}
}
层次遍历:
public void levelTrav(TreeNode root) {
if (root == null) return;
TreeNode node = root;
Queue <TreeNode> queue = new ArrayDeque <TreeNode>();
queue.enQuene(node);
if (!queue.isEmpty()) {
node = queue.DeQuene();
System.out.println(node.value);
if (node.left != null) queue.enQuene(node.left);
if (node.right != null) queue.enQuene(node.right);
}
}
前序线索二叉树线索化
void preThread(TBTNode p, TBTNode pre) {
if (p == NULL) return;
if (p.lchild == NULL) {
p.lchild = pre;
p.ltag = 1;
}
if (pre != NULL && pre.rchild == NULL) {
pre.rchild = p;
pre.rtag = 1;
}
pre = p;
if (p.ltag == 0) preThread(p.lchild, pre);
if (p.rtag == 0) preThread(p.rchild, pre);
}
中序遍历的二叉树线索化:
public void inThreadOrder(TreeNode node) {
if (node == null) {
return;
}
inThreadOrder(node.left);
if (node.left == null) {
node.left = mPreNode;
node.isLeftThread = true;
}
if (mPreNode != null && mPreNode.right == null) {
mPreNode.right = node;
mPreNode.isRightThread = true;
}
mPreNode = node;
inThreadOrder(node.right);
}
后序线索二叉树线索化
void postThread(TBTNode p, TBTNode pre) {
if (p == NULL) return;
postThread(p.lchild, pre);
postThread(p.rchild, pre);
if (p.lchild == NULL) {
p.lchild = pre;
p.ltag = 1;
}
if (pre != NULL && pre.rchild == NULL) {
pre.rchild = p;
pre.rtag = 1;
}
pre = p;
}
后序遍历
void postOrderNoncursion(BTNode bt) {
if (bt == NULL) return;
BTNode Stack1[maxSize];
BTNode Stack2[maxSize];
int top1 = top2 = -1;
BTNode p = NULL;
Stack1[++top] = bt;
while (top1 != -1) {
p = Stack1[top1--];
Stack2[++top2] = p;
if (p.lchild != NULL) Stack1[++top1] = p.lchild;
if (p.rchild != NULL) Stack1[++top1] = p.rchild;
}
while (top2 != -1) {
p = Stack2[top2--];
Visit(p);
}
}
假设二叉树采用二叉链表结构,设计一个递归算法求二叉树的高度
int TreeDepth(Tree T){
if(T==NULL) return 0;
int ldep=TreeDepth(T.lchild);
int rdep=TreeDepth(T.rchild);
return ldep>rdep ? ldep+1 : rdep+1;
}
设一颗二叉树中各结点的值各不相同,其先序遍历序列和中序遍历序列分别存在于两个一维数组A[1...n]和B[1...n]中,试编写算法建立该二叉树的二叉链表
TreeNode PreInCreate(int[] pre,int[] in,int pre_l,int pre_h,int in_l,int in_h){
TreeNode root = new TreeNode();
root.data = pre[pre_l];
int i = in_l;
while(in[i]!=root.data) i++;
int l = i - in_l;
int r = in_h -i;
if(l>=0)
root.lchild=PreInCreate(pre,in,pre_l + 1,pre_l + l,in_l,in_l + l - 1)
else
root.lchild=NULL;
if(r>=0)
root.lchild=PreInCreate(pre,in,pre_h - r + 1,pre_h,in_h - r + 1,in_h);
else
root.lchild=NULL;
return root;
}
假设二叉树采用二叉链表结构,计算一颗给定二叉树的所有双分支结点个数
int countNode(TreeNode T){
if(T==NULL) return 0;
if(T.lchild!=NULL && T.rchild!=NULL)
return countNode(T.lchild)+countNode(T.rchild)+1;
return countNode(T.lchild)+countNode(T.rchild);
}
设树B是一颗采用链式结构存储的二叉树,编写一个把树中所有结点的左、右子树进行交换的函数
void swap(TreeNode T){
if(T==NULL) return;
swap(T.lchild);
swap(T.rchild);
TreeNode temp = T.lchild;
T.lchild=T.rchild;
T.rchild=temp;
}
假设二叉树采用二叉链表存储结构,设计一个算法,求非空二叉树b的宽度(拥有结点数最多的那一层的结点个数)
// 获取最大宽度
public static int getMaxWidth(TreeNode root) {
if (root == null) return 0;
Queue <TreeNode> queue = new ArrayDeque <TreeNode> ();
int maxWitdth = 1; // 最大宽度
queue.add(root); // 入队
while (true) {
int len = queue.size(); // 当前层的节点个数
if (len == 0) break;
while (len > 0) { // 如果当前层,还有节点
TreeNode t = queue.poll();
len--;
if (t.left != null) queue.add(t.left); // 下一层节点入队
if (t.right != null) queue.add(t.right); // 下一层节点入队
}
maxWitdth = maxWitdth > queue.size() ? maxWitdth : queue.size();
}
return maxWitdth;
}
两个二叉树是否相似
bool IsSemblable(BiTree T1, BiTree T2) {
bool leftS = false,
rightS = false; //用来接受子树返回的信息
if (T1 == NULL && T2 == NULL) return true;
if (T1 == NULL || T2 == NULL) return false;
int leftS = IsSemblable(T1 - >lchild, T2 - >lchild); //递归左子树
int rightS = IsSemblable(T1 - >rchild, T2 - >rchild); //递归右子树
return leftS && rightS; //返回两个子树的信息
}
编程以求孩子兄弟表示法存储的森林的叶子结点数
class node{
int data;
node firstChild;
node nextBro;
}
int Leaves(node t) {
if (t == NULL) return 0 ;
return t.firstChild == NULL ? 1 + Leaves(t.nextBro) : Leaves(t.nextBro);
}
以孩子兄弟链表为存储结构,设计递归算法求树的深度
int Height(TreeNode bt) {
//递归求以孩子兄弟链表表示的树的深度
int hc,hs;
if (bt == NULL) return 0 ;
hc = height(bt.firstchild); //第一子女树高
hs = height(bt - >nextsibling); //兄弟树高
return hc + 1 > hs ? hc + l : hs;
}
编写一个算法,判定给定的二叉树是否是平衡二叉树
public boolean isBalanced(TreeNode tNode) {
if (tNode == null) return true;
if (Math.abs(depth(tNode.leftNode) - depth(tNode.righNode)) > 1) return false;
return isBalanced(tNode.leftNode) && isBalanced(tNode.righNode);
}
public int depth(TreeNode tNode) {
if (tNode == null) {
return 0;
}
return Math.max(depth(tNode.leftNode), depth(tNode.righNode)) + 1;
}