首页 > 编程知识 正文

二叉树算法分析,二叉树算法复杂度

时间:2023-05-06 00:18:02 阅读:158694 作者:1826

为什么在实用的二叉树1中,在有序数组中插入删除数据太晚了?插入或删除一个数据会移动后面的所有数据吗2、在链表中查找数据太慢了吗2、只能从头开始或从头开始一个一个地在树中解决问题吗实现这些特征,作为最有趣的数据结构之一的被称为树的用语,如下图的树所示,是平衡木和非平衡木的二叉树的类public class Tree {/** *和节点*/private Node root; /** *生成方法*/public Tree(}/***生成方法* * @param root *和节点*/publictree ) noderoot ) {this.root=root; }}class Node { /* key */int key; * /对象值; /*左节点*/Node leftChildNode; /*右节点*/Node rightChildNode; /** *构建方法* * @param key *关键字* @param value *值*/publicnode(intkey,Object value ) { super; this.key=key; this.value=value; }

二叉树插入功能

/** *插入节点* * @param key * key * @param value *值*/publicvoidinsert(intkey,Object value ) nodenode=newnode ) key,key } else { node current node=this.root; while(true ) if (key current node.key ) if ) current node.right child node==null (current node.right child node=null ) } else { current node=current node.right child node; } else { if (current node.leftchild node==null ) { current node.leftchild node=node; 返回; } else { current node=current node.leftchild node; }}}}}

二叉树搜索功能

查找/** *节点* * @ param key * @ return */publicnodefind (intkey ) if ) this.root!=null } { node current node=this.root; wile(currentnode.key!=key () if ) keycurrentnode.key ) current node=current node.right child node; } else { current node=current node.leftchild node; } if (当前节点==null ) {return null; } } }返回当前节点; }

二叉树的展示功能(中顺序扫描) ) ) ) ) ) )。

privatevoidshow(nodenode ) { if } node!=null } { this.show (node.leftchild node ); system.out.println (node.key ' : ' node.value; this.show(node.rightchildnode; }

测试

publicstaticvoidmain (string [ ] args ) node root=new node (50,24 ); treetree=newtree(root; tree.insert (20,530 ); tree.insert (540,520 ); tree.insert (4,540; tree.insert (0,550 ); tree.insert (8,520; tree.show (; }

完整的代码

包树; /** *二叉树* * @ author jyc 506 * */public classtree {/* * *节点*/private Node root; /** *生成方法*/public Tree(}/***生成方法* * @param root *和节点*/publictree ) noderoot ) {this.root=root; }/** *节点* * @ param key * @ return */publicnodefind (intkey ) if ) this.root!=null } { node current node=this.root; wile(currentnode.key!=key () if ) keycurrentnode.key ) current node=current node.right child node; } else { current node=current node.leftchild node; } if (当前节点==null ) {return null; } } }返回空值; }/** *插入节点* * @param key * key * @param value *值*/publicvoidinsert(intkey,Object value ) nodenode=newnode ) key } else { node current node=this.root; while(true ) if (key current node.key ) if ) current node.right child node==null (current node.right child node=null ) } else { current node=current node.right child node; } else { if (current node.leftchild node==null ) { current node.leftchild node=node; 返回; } else { current node=current node.leftchild node; }}}}}/** *展示*/public void show () (this.show ) ) root ); }/** *中顺扫描* * @ param node */privatevoidshow (node ) ) if ) node!=null } { this.show (node.leftchild node ); system.out.println (node.key ' : ' node.value ); this.show(node.rightchildnode; } publicstaticvoidmain (string [ ] args ) node root=new node (50,24 ); treetree=newtree(root; tree.insert (20,530 ); tree.insert (540,520 ); tree.insert (4,540; tree.insert (0,550 ); tree.insert (8,520; tree.show (; }}class Node {/* key */int key; * /对象值; /*左节点*/Node leftChildNode; /*右节点*/Node rightChildNode; /** *构建方法* * @param key *关键字* @param value *值*/publicnode(intkey,Object value ) {super; this.key=key; this.value=value; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。