二叉树

作者: 正义的米卡塔 | 来源:发表于2016-02-26 18:33 被阅读39次

闲来没事,学习了下二叉树

public class TreeNode {

/**

* 左边节点

*/

public TreeNode leftTree;

/**

* 右边节点

*/

public TreeNode rightTree;

/**

* 数据

*/

public int iDate;

public int parent;

public TreeNode(int i) {

// TODO Auto-generated constructor stub

this.iDate = i;

}

public void displayNode() {

}

public void add(TreeNode root, int idate) {

// parent=root;

TreeNode node = find(root, idate);

if (node != null) {

if (node.iDate < idate) {

node.rightTree = new TreeNode(idate);

node.rightTree.parent=node.iDate;

} else {

node.leftTree = new TreeNode(idate);

node.leftTree.parent=node.iDate;

}

}

}

private TreeNode find(TreeNode root, int i) {

// TODO Auto-generated method stub

if (root.iDate < i) {

if (root.rightTree != null) {

return find(root.rightTree, i);

} else {

return root;

}

} else {

if (root.leftTree != null) {

return find(root.leftTree, i);

} else {

return root;

}

}

}

}

----------------------------------

public class TreeTest {

public static int[] temp={7,3,9,8,10,2,1};

public static void main(String[] args) {

TreeNode node=new TreeNode(5);

for (int i = 0; i < temp.length; i++) {

node.add(node,temp[i]);

}

System.out.println(node);

// node.find(3);

}

}

相关文章

  • 数据结构与算法-二叉树02

    二叉树的定义 二叉树的特点 二叉树的五中基本形态 其他二叉树 斜二叉树 满二叉树 完全二叉树图片.png满二叉树一...

  • 二叉树

    二叉树 高度 深度真二叉树 满二叉树 完全二叉树 二叉树遍历前序 中序 后序层序遍历 翻转二叉树 递归法...

  • 二叉树 基础操作

    二叉树的使用 二叉树结构 先序创建二叉树 DFS 先序遍历二叉树 中序遍历二叉树 后序遍历二叉树 BFS 层次遍历...

  • 树与二叉树

    **树 ** 二叉树 满二叉树 完全二叉树 三种遍历方法 树与二叉树的区别 二叉查找树 平衡二叉树 红黑二叉树

  • 二叉树的宽度优先搜索(层次遍历,BFS)

    二叉树结构: 二叉树宽度优先搜索: 按照二叉树的层数依次从左到右访问二叉树的节点;例如:给定一个二叉树: 按照宽度...

  • 剑指 offer:39、平衡二叉树

    39. 平衡二叉树 题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 解题思路: 平衡二叉树:Wiki:在...

  • Algorithm小白入门 -- 二叉树

    二叉树二叉树构造二叉树寻找重复子树 1. 二叉树 基本二叉树节点如下: 很多经典算法,比如回溯、动态规划、分治算法...

  • 14-树&二叉树&真二叉树&满二叉树

    一、树 二、二叉树 三、真二叉树 四、满二叉树

  • 二叉树的应用

    完美二叉树(满二叉树) 除了最下一层的节点外,每层节点都有两个子节点的二叉树为满二叉树 完全二叉树 除二叉树最后一...

  • 12.树Tree(2)

    目录:1.二叉树的基本概念2.二叉树的性质3.二叉树的创建4.二叉树的遍历 1.二叉树的基本概念 2.二叉树的性质...

网友评论

      本文标题:二叉树

      本文链接:https://www.haomeiwen.com/subject/frdwkttx.html