Home » SQL & PL/SQL » SQL & PL/SQL » Binary search tree traversal (Oracle 10g,XP)
Binary search tree traversal [message #406237] Tue, 02 June 2009 23:44 Go to next message
rakeshramm
Messages: 175
Registered: September 2006
Location: Oracle4u.com
Senior Member

Please any one post a code of binary search tree traversal in
PLSQL .I am looking for it in Internet but didn't got it yet .Please help .Thanks in Advance
Re: Binary search tree traversal [message #406245 is a reply to message #406237] Wed, 03 June 2009 00:48 Go to previous messageGo to next message
Michel Cadot
Messages: 64144
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
No difference in PL/SQL than in (about) any other language.

Regards
Michel
Re: Binary search tree traversal [message #406262 is a reply to message #406245] Wed, 03 June 2009 01:20 Go to previous messageGo to next message
rakeshramm
Messages: 175
Registered: September 2006
Location: Oracle4u.com
Senior Member


Will you give a sample code .
Re: Binary search tree traversal [message #406269 is a reply to message #406262] Wed, 03 June 2009 01:29 Go to previous messageGo to next message
Michel Cadot
Messages: 64144
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Can you give me a sample of code in any (procedural) language and I can give you the same in PL/SQL.

Regards
Michel
Re: Binary search tree traversal [message #406276 is a reply to message #406237] Wed, 03 June 2009 01:58 Go to previous messageGo to next message
rakeshramm
Messages: 175
Registered: September 2006
Location: Oracle4u.com
Senior Member

      public class BinarySearchTree

      {

      private class node

      {

      int data;

      node leftchild;

      node rightchild;

      }

      public BinarySearchTree()

      { //create an empty Binary Search Tree }

      //node root = new node;

      root=null;

      }

      public boolean empty()

      { // return true if the tree is empty, otherwise return false }

      if ( root == null ){

      return true;

      }else{

      return false;

      }

      }

      public void Insert(int x)

      {//insert a value x }

      //search for a location to insert

      node p=root;

      node q=null;

      while(p!=null){

      q=p;

      if(x == p.data){ return;

      }

      if(x < p.data){ p=p.leftchild;

      }else{ p=p.rightchild;

      }

      }

      // create the new node

      p=new node();

      p.data = x;

      p.leftchild = p.rightchild = null;

      //attaching the new node to the tree

      if(root==null){ root = p;

      }else if(x < q.data){ q.leftchild=p;

      }else{ q.rightchild = p;

      }

      return;
     }

      public void Delete(int x)

      {//if value x is in the tree, remove x }

      //make node for q and p

      node q = new node();

      node p = new node();

      q=p=root;

      while(q==null)

      {

      if(x==p.data)

      break;

      q=p;

      if(x < p.data)

      p = q.leftchild;

      else

      p=p.rightchild;

      }

      if(p!=null){

      p=p.rightchild;

      }

      }

      public void Display()

      {// Display the data values from smallest to largest }

      Display (root);

      }

      public void Display(node p)

      {

      if(p!= null){

      Display(p.leftchild);

      System.out.println(p.data);

      Display(p.rightchild);

      }

      }

      private node root;//pointer to the root node

      }

      ----------------MAIN------------------------------------------------


      public class BinarySeachtreeMain{

      public static void main(String[] arg)

      {

      BinarySearchTree X = new BinarySearchTree();

      X.Insert(50);

      X.Insert(10);

      X.Insert(20);

      X.Insert(80);

      X.Insert(60);

      X.Insert(30);

      X.Display();

       

      X.Delete (20);
 
      System.out.println ("Tree after deleting 20:");
 
      X.Display();

      X.Delete(50);
 
      System.out.println ("Tree after deleting 50:");

      X.Display();
 
       
 
      if(!X.empty())

      System.out.println("Tree is not empty.");
 
      }
 
      }


A Code in Java

[Updated on: Wed, 03 June 2009 02:05]

Report message to a moderator

Re: Binary search tree traversal [message #406289 is a reply to message #406276] Wed, 03 June 2009 03:10 Go to previous messageGo to next message
Michel Cadot
Messages: 64144
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Now what is you problem in converting Java code into PL/SQL code?

Please do not post useless empty lines and indent your code.
public class BinarySearchTree
{
 private class node
 {
  int data;
  node leftchild;
  node rightchild;
 }

 public BinarySearchTree()
 { //create an empty Binary Search Tree }
   //node root = new node;
   root = null;
 }

 public boolean empty()
 { // return true if the tree is empty, otherwise return false }
  if ( root == null ) { return true; }
  else { return false; }
 }

 public void Insert(int x)
 {//insert a value x }
  //search for a location to insert
  node p = root;
  node q = null;
  while ( p != null ) {
    q = p;
    if ( x == p.data ) { return; }
    if ( x < p.data ) { p=p.leftchild; }
    else { p = p.rightchild; }
  }
  // create the new node
  p = new node();
  p.data = x;
  p.leftchild = p.rightchild = null;
  //attaching the new node to the tree
  if ( root == null ) { root = p; }
  else if ( x < q.data ) { q.leftchild=p; }
  else { q.rightchild = p; }
  return;
 }

 public void Delete(int x)
 {//if value x is in the tree, remove x }
  //make node for q and p
  node q = new node();
  node p = new node();
  q = p = root;
  while ( q == null ) {
    if ( x == p.data ) break;
    q = p;
    if ( x < p.data ) p = q.leftchild;
    else p = p.rightchild;
  }
  if ( p != null ) { p = p.rightchild; }
 }

 public void Display()
 {// Display the data values from smallest to largest }
  Display (root);
 }

 public void Display(node p)
 {
  if ( p != null ) {
    Display(p.leftchild);
    System.out.println(p.data);
    Display(p.rightchild);
  }
 }

 private node root;//pointer to the root node
}

----------------MAIN------------------------------------------------

public class BinarySeachtreeMain{
public static void main(String[] arg)
{
 BinarySearchTree X = new BinarySearchTree();
 X.Insert(50);
 X.Insert(10);
 X.Insert(20);
 X.Insert(80);
 X.Insert(60);
 X.Insert(30);
 X.Display();

 X.Delete (20);
 System.out.println ("Tree after deleting 20:");
 X.Display();

 X.Delete(50);
 System.out.println ("Tree after deleting 50:");
 X.Display();
       
 if (!X.empty()) System.out.println("Tree is not empty.");
 }
}

Regards
Michel

[Updated on: Wed, 03 June 2009 03:20]

Report message to a moderator

Re: Binary search tree traversal [message #406291 is a reply to message #406262] Wed, 03 June 2009 03:23 Go to previous messageGo to next message
S.Rajaram
Messages: 1027
Registered: October 2006
Location: United Kingdom
Senior Member
I am curious to know why do you want to do this in pl/sql? Any justification ?

Regards

Raj
Re: Binary search tree traversal [message #406328 is a reply to message #406291] Wed, 03 June 2009 05:54 Go to previous messageGo to next message
rakeshramm
Messages: 175
Registered: September 2006
Location: Oracle4u.com
Senior Member

Sir ,it is because by developing the code in PLSQL it can be used for both Java and Vb.net developers at same time and thus the usability increases .

Thanks in advance
Re: Binary search tree traversal [message #406331 is a reply to message #406328] Wed, 03 June 2009 06:13 Go to previous message
S.Rajaram
Messages: 1027
Registered: October 2006
Location: United Kingdom
Senior Member
Well if you ask me I don't think it is a wise approach. Every language has its own pros and cons. I wouldn't be inclined to go for a generic approach to be used across different languages. So If I were you, I will reconsider the design. I am not saying you cannot do it but if you ask me it's not worth the effort. Have you looked into the associate array in pl/sql. Internally it could be using a Btree structure to store the index key.

Regards

Raj
Previous Topic: ORA-06502: PL/SQL: numeric or value error: invalid LOB locator
Next Topic: xml indexes in 11g
Goto Forum:
  


Current Time: Fri Dec 09 11:26:11 CST 2016

Total time taken to generate the page: 0.24007 seconds