--树状结构存储与展示

来源:互联网 发布:js中判断能否整除 编辑:程序博客网 时间:2024/05/17 02:57

--树状结构存储与展示
drop table article;
create table article
(
  id number primary key,
  cont  varchar2(4000),
  pid   number,
  isleaf number(1),--0代表非叶子节点,1代表根节点
  alevel number(2)--帖子的等级
);

insert into article values(1,'蚂蚁大战大象',0,0,0);
insert into article values(2,'蚂蚁大战大象',1,0,1);
insert into article values(3,'蚂蚁大战大象',2,1,2);
insert into article values(1,'蚂蚁大战大象',2,0,0);
commit;


--------使用递归解决问题
create or replace procedure  p(v_pid article.pid%type) is
  cursor c is  select * from article where pid=v_pid;
 begin
      for v_article in c loop
      dbms_output.put_line(v_article.cont);
       if(v_article.isleaf=0)then--如果是非叶子节点
          p(v_article.id);
       end if;
       end loop;
    end;


--show error;
set serveroutput on;

--exec p(0);
--------------------
create or replace procedure  p(v_pid article.pid%type,v_level binary_integer) is
  cursor c is  select * from article where pid=v_pid;
 v_preStr varchar2(1024) :=' ';
 begin

     for i in 1..v_level
       loop
        v_preStr := v_preStr||'**********';
       end loop;  

      for v_article in c loop
      dbms_output.put_line(v_article.cont);
       if(v_article.isleaf=0)then--如果是非叶子节点
          p(v_article.id,v_level+1);
       end if;
       end loop;
    end;
 

原创粉丝点击