Data structure experiment - basic operation and algorithm design of binary tree (first root, middle root, last root traversal, search keywords)

① Traversing in root order to find the first keyword is key                                

BinaryNode<T> search(T key)                     

② Return the level where the key node is located

int level(T key)

3. Experimental methods and steps (requirements analysis, algorithm design ideas, flowcharts, etc.)

Experiment 1: Traversing in root order to find the first keyword is key

Analysis: First, construct an empty binary tree, and traverse the binary tree in root order. Returns the description string of all nodes of the binary tree traversed in root order, including empty subtree markers. Search in the subtree with p as the root and return the node whose key word appears for the first time in the root-first order traversal. Search the left and right subtrees separately. If not found, the return value is null.

Experiment 2 returns the level where the key node is located

Analysis: Return the level where the key node is located. If the tree is empty or the key is not found, return -1. Set the root node to be equal to 1. Search from the left and right subtrees until the level where the key is located is found.

4. The original record of the experiment (source program, data structure, etc.)

  public class BinaryNode<T>

{

    public T data; // data domain, store data elements

    public BinaryNode<T> left, right;// chain domain, pointing to the left and right child nodes respectively

       // Construct the node, data specifies the element, left and right point to the left child and right child nodes respectively

       public BinaryNode(T data, BinaryNode<T> left, BinaryNode<T> right)

       {

           this.data = data;

           this.left = left;

           this.right = right;

       }

       public BinaryNode(T data) // Construct the leaf node whose element is data

       {

           this(data, null, null);

       }

       public String toString() // Returns the description string of the node data field

       {

           return this.data.toString();

       }

       public boolean isLeaf() // Determine whether it is a leaf node

       {

           return this.left==null && this.right==null;

       }

}

 

 public class BinaryTree<T> // Binary tree class, binary linked list storage, T specifies the element type of the node

{

      public   BinaryNode<T> root; //root node, binary linked list node structure

      private Object value;

       public BinaryTree() // construct an empty binary tree

       {

           this.root=null;

       }

       public boolean isEmpty() // determine whether the binary tree is empty

       {

           return this.root==null;

       }

       public void preorder() // Output first root order traversal sequence

       {

          preorder(this.root);

          System.out.println();

       }

       private void preorder(BinaryNode<T> p)

       {

          if(p!=null);

          {

             System.out.println(p.data.toString()+"");

             preorder(p.left);

             preorder(p.right);

          }

       }

       public String toString() // Returns the description string of all nodes of the binary tree traversed in root order, including the empty subtree mark

       {

          return toString(this.root);

       }

       private String toString(BinaryNode<T> p)

       {

          if(p==null)

             return"^";

          return p.data.toString()+""+toString(p.left)+toString(p.right);  recursive call

       }

       public void inorder() // Medium root traversal

       {

          inorder(this.root);

          System.out.println();

       }

       private void inorder(BinaryNode<T>p)

       {

         

          if(p!=null)

          {

             inorder(p.left);

             System.out.print(p.data.toString()+"");

             inorder ( p . right );

          }

       }

       public void postorder() // back root traversal

       {

          postorder(this.root);

          System.out.println();

       }

       private void postorder(BinaryNode<T>p)

       {

          if(p!=null)

          {

             mailorder(p.left);

             mailorder(p.right);

             System.out.print(p.data.toString()+"");

          }

       }

// Find and return the first root order traversal The keyword that appears for the first time is the key node

       public BinaryNode<T> search(T key)

    {

    return search(root, key);

    }

//   Method 1: Search in the subtree with p as the root and return the first occurrence of the keyword as the key element node, if not found, return null , first root order traversal

     private BinaryNode<T> search(BinaryNode<T> p, T key)

   {

     if (p==null || key==null)

        return null;

     if (p.data.equals(key))

        return p; // Search is successful, return to find the node

     BinaryNode<T> find=search(p.left, key); // Search in the left subtree, call recursively

     if (find== null ) // if not found in the left subtree

        find=search(p.right, key); // Continue to search in the right subtree, call recursively

     return find; // return the search result

   }

     public boolean contains(T key) // Determine whether the keyword is a key element

  {

     return this.search(key)!=null;  //

}

  public BinaryTree(T[] prelist) // Construct a binary tree by traversing the sequence in the order of the first root and the middle root

 {

    this.root = create(prelist);

 }

 private int i=0;

 private BinaryNode<T> create(T[] prelist)

 {

   BinaryNode<T>p=null;

   if(i<prelist.length)

   {

      T elem=prelist[i];

      i++;

      if (elem!=null)

      {

          p=new BinaryNode<T>(elem);

          p.left=create(prelist);

          p.right=create(prelist);

      }

   }

   return p;

  

}

// Method 2: Return the level where the x node is located, if the tree is empty or x is not found, return -1

public int getLevel(T x)

{

    return getLevel(root, 1, x); // Let the level of the root node be 1

}

private int getLevel(BinaryNode<T> p, int i, T x)

{

    if (p==null)                            

        return -1;

    if (p.data.equals(x))

        return i; // Find success

    int level = getLevel(p.left, i+1, x); // search in the left subtree

    if (level==-1)

        level = getLevel(p.right, i+1, x); // Continue to search in the right subtree

    return level;

}

public static void main(String args[])

{

    String[] prelist= {"A","B","D",null,"G",null,null,"C","E",null,null,"F","H"};

    BinaryTree<String> bitree= new BinaryTree<String>(prelist);

    System. out . println (" The root-first order traversal sequence of the binary tree:   "+bitree.toString()); // Mark the empty subtree

    System. out . println (" Medium root order traversal sequence of binary tree:   "); bitree.inorder();

    System.out .println (" Backroot order traversal sequence of binary tree: " )   ; bitree.postorder();

    System. out . println (" Root first order traversal to find the first keyword is: "+bitree.search("B"));

    bitree.getLevel("E");

    System. out . println (" The level of the search keyword is: " +bitree.getLevel("E"));

   

}

}

5. Experimental results and analysis (calculation process and results, data curves, charts, etc.)

Experimental results:

 

Guess you like

Origin blog.csdn.net/weixin_45823684/article/details/122470096