1、二叉链表节点类的定义;
2、二叉树类的定义;
3、建立下图所示的二叉树
以字符串的形式“根左右”定义一棵二叉树时,写出创建二叉树的操作:
4、编程实现以上二叉树的递归前序、非递归前序、中序和后序遍历操作,输出遍历序列;
5、完成统计以上二叉树中叶子结点的个数或计算以上二叉树的深度;
6、定义二叉查找树类;实现二叉查找树的查找、插入和删除操作;
7、从键盘上输入六个整数45、24、53、12、37、9构造二叉查找树,输出二叉查找树的中序遍历结果;
8、在二叉查找树上插入元素2和42。
9、在二叉查找树上查找37和50,并输出能否查找成功;
10、删除数据元素24和53,输出其中序遍历结果。
import java.util.Scanner;
import java.util.Stack;
class BTNode<AnyType>{ //节点类
AnyType data;
BTNode leftChildNode,rightChildNode;
public BTNode(){
data=null;
leftChildNode=rightChildNode=null;
}
public BTNode(AnyType d,BTNode<AnyType> lc,BTNode<AnyType> rc){
data=d;
leftChildNode=lc;
rightChildNode=rc;
}
public BTNode(AnyType d){
data=d;
leftChildNode=null;
rightChildNode=null;
}
public BTNode<AnyType>getleftChild(){
return leftChildNode;
}
public BTNode<AnyType>getrightChild(){
return rightChildNode;
}
public Object getData(){
return data;
}
}
public class BinaryTree<AnyType>{
public BTNode<AnyType>root;
int i=0;
int count=0;
public BinaryTree(){}
public BinaryTree(AnyType rootNodeltem){
root.data=rootNodeltem;
root.leftChildNode=root.rightChildNode=null;
}
public BinaryTree(BTNode<AnyType>t){
root=t;
}
public BTNode<AnyType>createBinaryTree(AnyType [] preOrder){//先序建树
BTNode<AnyType>root=null;
if(i<preOrder.length){
AnyType data=preOrder[i];
i++;
if(!data.equals("null")){
root=new BTNode<AnyType>(data);
root.leftChildNode=createBinaryTree(preOrder);
root.rightChildNode=createBinaryTree(preOrder);
}
}
return root;
}
public void preOrder(BTNode<AnyType>root){ //递归先序遍历
if(root!=null){
System.out.print(root.data+" ");
preOrder(root.leftChildNode);
preOrder(root.rightChildNode);
}
}
public void inOrder(BTNode<AnyType>root){ //中序遍历
if(root!=null){
inOrder(root.leftChildNode);
System.out.print(root.data+" ");
inOrder(root.rightChildNode);
}
}
public void postOrder(BTNode<AnyType>root){ //后序遍历
if(root!=null){
postOrder(root.leftChildNode);
postOrder(root.rightChildNode);
System.out.print(root.data+" ");
}
}
public void preOrderTraverse(BTNode<AnyType> root){ //非递归先序遍历
Stack<BTNode<AnyType>>S=new Stack<BTNode<AnyType>>();
BTNode<AnyType>p=root;
while(p!=null||!S.isEmpty()){
if(p!=null){
System.out.print(p.data+" ");
S.push(p);
p=p.leftChildNode;
}
else{
p=S.pop();
p=p.rightChildNode;
}
}
}
public BTNode<AnyType>search(BTNode<AnyType>root,AnyType x){ //查找某个元素
if(root==null)return null;
if(root.getData().equals(x))return root;
BTNode<AnyType>p=search(root.leftChildNode,x);
if(p==null)
return p;
else
return (search(root.rightChildNode,x));
}
public int countLeafNode(BTNode<AnyType>root){ //计算叶子节点的个数
if(root==null)return 0;
if((root.leftChildNode==null&&root.rightChildNode==null ))
return 1;
else
return countLeafNode(root.leftChildNode)+countLeafNode(root.rightChildNode);
}
public int depth(){ //求树的深度
return depth(root);
}
public int depth(BTNode<AnyType> root){
int leftdepth;
int rightdepth;
if(root==null)
return 0;
leftdepth=depth(root.leftChildNode);
rightdepth=depth(root.rightChildNode);
boolean la=leftdepth<rightdepth;
if(la==true)
return rightdepth+1;
else
return leftdepth+1;
}
public static void main(String args[]){
String tree[]=new String[13];
Scanner cin=new Scanner(System.in);
for(int i=0;i<13;i++){
tree[i]=cin.next();
}
BinaryTree<String>bt=new BinaryTree<String>();
BTNode<String>node=bt.createBinaryTree(tree);
BinaryTree<String>text=new BinaryTree<String>(node);
System.out.println("递归先序遍历的结果是:");
bt.preOrder(text.root);
System.out.println();
System.out.println("非递归先序遍历的结果");
bt.preOrderTraverse(text.root);
System.out.println();
System.out.println("中序遍历结果是:");
bt.inOrder(text.root);
System.out.println();
System.out.println("后序遍历结果是:");
bt.postOrder(text.root);
System.out.println();
System.out.println("请输入要查找的元素:");
String exist=cin.next();
System.out.println("如果存在则输出:");
try{
System.out.println(bt.search(text.root,exist).data);
}catch(Exception e){
System.out.println("您查找的元素不存在!");
}
System.out.println("叶子节点个数是"+bt.countLeafNode(text.root));
System.out.println("树的深度是:"+bt.depth(text.root));
BinarySearchTree<Integer>bst=new BinarySearchTree<Integer>();//二叉查找树
bst.init();
}
}
class BinarySearchTree<AnyType extends Comparable <?super AnyType>>{
private BTNode root=null;
public BinarySearchTree(){}
public BinarySearchTree(BTNode<AnyType> node){
root=node;
}
BTNode<AnyType>insert(AnyType x,BTNode<AnyType> t){ //二叉查找树插入节点
if(t==null)
return new BTNode(x,null,null);
int compareResult=x.compareTo(t.data);
if(compareResult<0)
t.leftChildNode=insert(x,t.leftChildNode);
else if(compareResult>0)
t.rightChildNode=insert(x,t.rightChildNode);
else ;
return t;
}
BTNode<AnyType>remove(AnyType x,BTNode<AnyType> t){ //二叉查找树删除节点
if(t==null)
return t;
int compareResult=x.compareTo(t.data);
if(compareResult<0)
t.leftChildNode=remove(x,t.leftChildNode);
else if(compareResult>0)
t.rightChildNode=remove(x,t.rightChildNode);
else if(t.leftChildNode!=null&&t.rightChildNode!=null){
t.data=findMin(t.rightChildNode).data;
t.rightChildNode=remove(t.data,t.rightChildNode);
}
else t=(t.leftChildNode!=null)?t.leftChildNode:t.rightChildNode;
return t;
}
public BTNode<AnyType>findMin(BTNode<AnyType> t){ //二叉查找树找最小值
if(t!=null){
while(t.leftChildNode!=null)
t=t.leftChildNode;
}
return t;
}
public BTNode createBSTree(int n,AnyType a[]){ //创建二叉查找树
for(int i=0;i<n;i++){
root=insert(a[i],root);
}
return root;
}
public void inOrder(BTNode<AnyType> rootNode){ //中序输出
if(rootNode!=null){
inOrder(rootNode.leftChildNode);
System.out.print(rootNode.data+" ");
inOrder(rootNode.rightChildNode);
}
}
public boolean contains(AnyType x){
return contains(x,root);
}
public boolean contains(AnyType x,BTNode<AnyType> t){ //二叉查找树查找某个元素
if(t==null)return false;
int compareResult=x.compareTo(t.data);
if(compareResult<0)
return contains(x,t.leftChildNode);
else if(compareResult>0)
return contains(x,t.rightChildNode);
else
return true;
}
public void init(){
Integer a[]=new Integer[100];
Scanner cin=new Scanner(System.in);
System.out.println("请输入要创建的二叉查找树树的元素个数:");
int n=cin.nextInt();
System.out.println("请输入二叉树的元素:");
for(int i=0;i<n;i++)
a[i]=cin.nextInt();
BinarySearchTree<Integer>bst=new BinarySearchTree<Integer>();
BTNode<Integer> node=bst.createBSTree(n, a);
BinarySearchTree<Integer>test=new BinarySearchTree<Integer>(node);
System.out.println("中序遍历的结果是:");
bst.inOrder(test.root);
System.out.println();
System.out.println("请输入插入的元素,输入0结束");
while(true){
int x=cin.nextInt();
if(x==0)
break;
bst.insert(x, test.root);
}
System.out.println("插入元素后的中序遍历结果是:");
System.out.println("请输入要查找的元素,输入0结束:");
while(true){
int y=cin.nextInt();
if(y==0)
break;
if(bst.contains(y))
System.out.println(y+"这个元素存在");
else
System.out.println(y+"这个元素不存在");
}
bst.inOrder(test.root);
System.out.println();
System.out.println("请输入要删除的元素,输入0结束");
while(true){
int xx=cin.nextInt();
if(xx==0)
break;
bst.remove(xx, test.root);
System.out.println("删除元素后的中序遍历结果:");
bst.inOrder(test.root);
System.out.println();
}
}
}
//a b d null null null c e null null f null null
//6 45 24 53 12 37 9