首页 > 编程知识 正文

完全二叉树怎么理解,二叉树的5个性质

时间:2023-05-06 01:47:21 阅读:161746 作者:416

csuoj2157强力冬瓜pcqdkf配送:点击链接强力冬瓜pi : binarysearchtreeanalogsubmitpagetimelimit :1 secmemorylimit 3360128 MB submittete le=' ht-family : ' Helvetica neue ',Helvetica,' PingFang SC ',' Hiragino Sans GB ',' Microsoft YaHei ',' nnei ' fore ack ground-color : RGB (255、255、255 );' Description Binary Search Tree,abbreviated as BST,isakindofbinarytreemaintainsthefollowingproperty 3360

each node has a Key value,whichcanbeusedtocomparewitheachother.foreverynodeinthetree, everykeyvalueinitsleftsubtreeissmallerthanitsownkeyvalue.foreverynodeinthetree、 everykeyvalueinitsrightsubtreeisequaltoorlargerthanitsownkeyvalue.nowweneedtoanalogabst,weonlyrequireonekindofoperation 3330

First,wehaveanemptybst.inputisasequenceofnumbers.weneedtoinsertthemonebyoneflowingtherulesbelow 3360

iftheinsertedvalueissmallerthantheroot ' svalue,insert it to the left subtree。

iftheinsertedvalueislargerthanorequaltothevalueoftheroot ' svalue,insert it to the right subtree。

After each input,we need to output the preorder,inorder,postorder traversal sequences。

About tree traversal,thefollowingisfromwikipedia :

epth-firsttraversaltotraverseanon-emptybinarytreeinpreorder,performthefollowingoperationsrecursivelyateachnode,startitiite

visit the root.traversetheleftsubtree.traversetherightsubtree.totraverseanon-emptybinarytreeininorder (

traversetheleftsubtree.visit the root.traversetherightsubtree.totraverseanon-emptybinarytreeinpostorder,PerformtheFolller

traversetheleftsubtree.traversetherightsubtree.visit the root.lookatthefolowingexample 3360

aftereachintegerinsertedthestructureofthetreeisillustratedintheflowing :

3/ 16/ 59 inputthefirstintegeroftheinputist,thenumberoftestcasehastwolines.thefirstlinecontainanintion thenumberofnumbersneedtobeinsertedintothebst.thesecondlinecontainnintegersseparatedbyspace,eachintegeriniter

Output Each test case,outputmustcontainthreelines 3360 the preorder, inorderandpostordertraversalsequence.thenumbersineachlineshouldbeseparatedbyasinglespaceandyoushouldnotputanythingattttheened

示例输入1536951示例输出31659135691596963 # include iostream # includecstdiousingnamespacestd; #definelllonglongintint T,n,I; ll x; structbstnode//BST{lla; structbstnode* left; structbstnode* right; }*bst; voidinsert(llx,structbstnode *t ) /创建节点(if ) t==null ) { t=newbstnode; t-left=NULL; t-right=NULL; t-a=x; }elseif(xt-a ) insert(x ) x,t-left ); ELSEif(x=t-a ) insert(x ) x,t-right; else; }voidpreorder(structbstnode*t ) /前置) if(t!=null(printf ) ' %lld ',t-a ); preorder(t-left ); preorder(t-right ); }voidmidorder(structbstnode*t ) /中序) if(t!=null}{midorder(t-left ); printf('%lld ',t-a ); 混合顺序(t-right ); }voidbackorder(structbstnode*t ) /后序) if(t!=null}{backorder(t-left ); 备份(t-right ); printf('%lld ',t-a ); }voidfreebstnode(BSTnode**t ) /内存释放(if ) t==空) return; freebstnode (() ) t )-left ); freebstnode () () (t )-right ); delete *t; *t=NULL; }int main () (Scanf ) ' %d ',t ); wile(t-- ) Scanf ) ' %d ',n ); for(I=0; in; I ) Scanf('%lld ',x ); 插入(x,bst ); }preorder(BST; 打印((n ); 混合顺序(BST; 打印((n ); 备份器(BST; FreeBstnode(BST; 打印((n ) n ) ); } return 0; }

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