二叉树的相关操作

来源:互联网 发布:如何看淘宝商品的类目 编辑:程序博客网 时间:2024/04/27 13:17

package acm.result.test1;

/**
 * @author BTiger
 *
 */
public class Main {

 /**
  * 二叉树遍历,三种方法
  *
  * @param args
  */
 public static void main(String[] args) {
  Tree tree = getTree();
  Node root = tree.getRoot();

  // 先根遍历
  System.out.println("先根遍历");
  tree.rootFirst(root);
  System.out.println();

  // 中根遍历
  System.out.println("中根遍历");
  tree.middleFirst(root);
  System.out.println();

  // 后根遍历
  System.out.println("后根遍历");
  tree.rootLast(root);
  System.out.println();

  // 求二叉树节点个数
  System.out.println("二叉树的节点个数是:" + tree.getNodeCount());

  // 求二叉树高度
  System.out.println("二叉树高度是:" + tree.height());

  // 查找给定数值
  System.out.println("C的对应的节点是:" + tree.search("C").data);

  // 查找给定数值对应节点的父节点
  Node s = tree.search("H");
  Node parent = tree.getParent(s);
  if (parent != null)
   System.out.println("H的父母节点是:" + parent.data);

  // 查找所有祖先节点
  System.out.print("H的祖先节点分别是:");
  while (parent != null) {
   System.out.print(parent.data + " ");
   parent = tree.getParent(parent);
  }
 }

 /**
  * 构造二叉树
  *
  * @return
  */
 private static Tree getTree() {
  Node root = new Node();
  Node b = new Node();
  Node c = new Node();
  Node d = new Node();
  Node e = new Node();
  Node f = new Node();
  Node g = new Node();
  Node h = new Node();
  root.setData("A");
  b.setData("B");
  c.setData("C");
  d.setData("D");
  e.setData("E");
  f.setData("F");
  g.setData("G");
  h.setData("H");
  root.setLeftNode(b);
  root.setRightNode(c);
  b.setLeftNode(d);
  d.setRightNode(g);
  c.setLeftNode(e);
  c.setRightNode(f);
  f.setLeftNode(h);
  Tree tree = new Tree();
  tree.setRoot(root);
  return tree;
 }
}

class Tree {
 public Node root = null;

 public Node getRoot() {
  return root;
 }

 public void setRoot(Node root) {
  this.root = root;
 }

 public boolean isEmpty() {
  return this.root == null;
 }

 /**
  * 先根遍历
  *
  * @param root
  */
 public void rootFirst(Node root) {
  System.out.print(root.getData() + " ");// 打印根节点
  if (root.getLeftNode() != null) {// 打印左节点
   rootFirst(root.getLeftNode());
  }
  if (root.getRightNode() != null) {// 打印右节点
   rootFirst(root.getRightNode());
  }
 }

 /**
  * 中根遍历
  *
  * @param root
  */
 public void middleFirst(Node root) {
  if (root.getLeftNode() != null) {// 打印左节点
   middleFirst(root.getLeftNode());
  }
  System.out.print(root.getData() + " ");// 打印根节点
  if (root.getRightNode() != null) {// 打印右节点
   middleFirst(root.getRightNode());
  }
 }

 /**
  * 后根遍历
  *
  * @param root
  */
 public void rootLast(Node root) {
  if (root.getLeftNode() != null) {// 打印左节点
   rootLast(root.getLeftNode());
  }
  if (root.getRightNode() != null) {// 打印右节点
   rootLast(root.getRightNode());
  }
  System.out.print(root.getData() + " ");// 打印根节点
 }

 /**
  * 获取节点个数
  *
  * @return
  */
 public int getNodeCount() {
  return getNodeCount(root);

 }

 public int getNodeCount(Node root) {
  if (root != null)
   return 1 + getNodeCount(root.leftNode)
     + getNodeCount(root.rightNode);
  else
   return 0;

 }

 /**
  * 求高度 从根节点的左节点找即可
  *
  * @param
  */
 public int height() {
  return height(root);

 }

 public int height(Node root) {
  if (root != null) {
   int ld = height(root.leftNode);
   int rd = height(root.rightNode);
   return ld > rd ? ld + 1 : rd + 1;
  }
  return 0;
 }

 /**
  * 查找给定数据对应的节点
  */
 public Node search(String value) {
  return search(root, value);
 }

 public Node search(Node root, String value) {
  if (root != null) {
   if (value.equals(root.getData())) {
    return root;
   } else {
    Node ls = search(root.leftNode, value);
    return (ls != null) ? ls : search(root.rightNode, value);
   }
  }
  return null;
 }

 /**
  * 查找给定数据对应的节点的父母节点
  */
 public Node getParent(Node node) {
  return getParent(root, node);
 }

 public Node getParent(Node root, Node node) {
  Node find = null;
  if (root != null) {
   if (root.leftNode == node || root.rightNode == node) {
    find = root;
   } else {
    find = getParent(root.leftNode, node);
    return (find != null) ? find : getParent(root.rightNode, node);
   }
  }
  return find;
 }
}

class Node {
 public String data;
 public Node leftNode = null, rightNode = null;

 public boolean isLeaf() {
  return this.leftNode == null && this.rightNode == null;
 }

 public String getData() {
  return data;
 }

 public void setData(String data) {
  this.data = data;
 }

 public Node getLeftNode() {
  return leftNode;
 }

 public void setLeftNode(Node leftNode) {
  this.leftNode = leftNode;
 }

 public Node getRightNode() {
  return rightNode;
 }

 public void setRightNode(Node rightNode) {
  this.rightNode = rightNode;
 }

}

原创粉丝点击