二叉树可视化Java语言实现(完整版-打开即可运行).doc_第1页
二叉树可视化Java语言实现(完整版-打开即可运行).doc_第2页
二叉树可视化Java语言实现(完整版-打开即可运行).doc_第3页
二叉树可视化Java语言实现(完整版-打开即可运行).doc_第4页
二叉树可视化Java语言实现(完整版-打开即可运行).doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、public class BinaryNode private E element;private BinaryNode left;private BinaryNode right;public E getElement() return element;public void setElement(E element) this.element = element;public BinaryNode getLeft() return left;public void setLeft(BinaryNode left) this.left = left;public BinaryNode getRight() return right;public void setRight(BinaryNode right) this.right = right;/ 递归方法构造tree,单个输出:public static void structPrint(BinaryNode e) if (e.element 12)return;else / System.out.print( + e.element + );BinaryNode left = new BinaryNode();left.setElement(e.element * 2 + 1);BinaryNode right = new BinaryNode();right.setElement(e.element * 2 + 2);/ System.out.print( );/ System.out.print( + e.element + );/ System.out.print(+left.getElement()+ +right.getElement()+);if (left.getElement() 12)return;else e.setLeft(left);e.setRight(right);structPrint(left);structPrint(right);/ 递归前序遍历public static void inorder(BinaryNode root) if (root != null) / System.out.print(root.getElement();/调整位置得到中、后序。inorder(root.getLeft();/ System.out.print(root.getElement();inorder(root.getRight();System.out.print( + root.element + );/ 查找叶子(前序)public static BinaryNode search(BinaryNode root, int e) if (root != null) if (root.getElement() = e) / System.out.print(* + root.element + *);return root; else search(root.getLeft(), e);search(root.getRight(), e);return root;public static void test(BinaryNode root, int e) BinaryNode parent = new BinaryNode();int i1 = (e - 2) / 2;parent = BinaryNode.search(root, i1);System.out.println(*! + BinaryNode.search(root, i1).getElement()+ !*);/ 删除节点(前序):public static void delete(BinaryNode root, int e) / if(e=0) BinaryNode.setnull(root);if (e = 0) root.setLeft(null);root.setRight(null);root.setElement(null); else if (root != null) if (root.getElement() * 2 + 1 = e) root.setLeft(null); else if (root.getElement() * 2 + 2 = e) root.setRight(null);/ System.out.print(* + root.element + *);delete(root.getLeft(), e);delete(root.getRight(), e);/ 使用删除中的setnull;public static void setnull(BinaryNode root) / root.setElement(null);/ root.setLeft(null);/ root.setRight(null);/ System.out.println(root.getElement();root = null;二、package Lessons13_9;public class Show1_12 public static void main(String args) BinaryNode root=new BinaryNode(); root.setElement(0); System.out.println(前序遍历显示结果:);BinaryNode.structPrint(root);System.out.println();System.out.println(-分割线1-); System.out.println(后序遍历显示结果:);BinaryNode.inorder(root);System.out.println();System.out.println(-分割线2-);System.out.println(查询结果显示:);BinaryNode.search(root,4);System.out.println();System.out.println(total nodes number1: +Show1_12.nodes(root);System.out.println(-分割线3-); System.out.println(删除结果显示:); BinaryNode.delete(root,0);/BinaryNode.inorder(root);System.out.println();System.out.println(-分割线4-);/BinaryNode.setnull(root);/root = null;/root = null;/BinaryNode.structPrint(root);/BinaryNode.test(root, 7);/BinaryNode.inorder(root);/System.out.println(total nodes number2.1: +Show1_12.nodes(root);/BinaryNode.setnull(root);/BinaryNode.inorder(root);/root = null;/System.out.println();/System.out.println(total nodes number2: +Show1_12.nodes(root); /total nodes number: static int nodes(BinaryNode tree) if (tree.getElement()=0)return 0; else int left = nodes(tree.getLeft();int right = nodes(tree.getRight();return left + right + 1;/注意:必须+1! 三、package Lessons13_9;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TreeControl extends JPanel private BinaryNode tree;/private JTextField jtfKey = new JTextField(5);private TreeView View = new TreeView();/private JButton jbtInsert = new JButton(Insert);/private JButton jbtDelete = new JButton(Delete);public TreeControl(BinaryNode tree)this.tree = tree;setUI();private void setUI()this.setLayout(new BorderLayout();add(View,BorderLayout.CENTER);JPanel panel = new JPanel();/.add(new JLabel(Enter a key: );/panel.add(jtfKey);/panel.add(jbtInsert);/panel.add(jbtDelete);/add(panel,BorderLayout.SOUTH);/*jbtInsert.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)int key = Integer.parseInt(jtfKey.getText();if(BinaryNode.search(tree, key) != null)JOPtionPanel=showMessageDialog(null,key+is already in the tree );elsetree.insert(key);view.repaint(););*/jbtDelete;class TreeView extends JPanelprivate int radius = 20;private int vGap = 50;protected void paintComponent(Graphics g)super.paintComponent(g);if(tree!=null)/displayTree(g,tree,getWidth()/2,30,getWidth()/4);displayTree(g,tree,getWidth()/2,30,getWidth()/4);private void displayTree(Graphics g,BinaryNode root,int x, int y, int hGap)/这里x,y是结点中心!g.drawOval(x-radius,y-radius,2*radius,2*radius);g.drawString(root.getElement()+, x-6, y+4);if(root.getLeft()!=null)connectLeftChild(g,x,y,x-hGap,y+vGap);displayTree(g,root.getLeft(),x-hGap,y+vGap,hGap/2);if(root.getRight()!=null)connectRightChild(g,x,y,x+hGap,y+vGap);displayTree(g,root.getRight(),x+hGap,y+vGap,hGap/2);private void connectLeftChild(Graphics g,int x1,int y1,int x2,int y2)double d = Math.sqrt(vGap*vGap+(x2-x1)*(x2-x1);int a1=(int)(radius*vGap/d);int a2=(int)(radius*(x1-x2)/d);int x11=(int)(x1 - a2);int y11=(int)(y1 + a1);int x21=(int)(x2 + a2);int y21=(int)(y2 - a1);g.drawLine(x11,y11,x21,y21);/g.drawLine(x1,y1,x2,y2);private void connectRightChild(Graphics g,int x1,int y1,int x2,int y2)double d = Math.sqrt(vGap*vGap+(x2-x1)*(x2-x1);int a1=(int)(radius*vGap/d);int a2=(int)(radius*(x2-x1)/d);int x11=(int)(x1 + a2);int y11=(int)(y1 + a1);int x21=(int)(x2 - a2);i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论