2-3树:我如何搜索节点?

时间:2015-04-24 02:33:06

标签: c++ search insert tree

这是我的代码到目前为止: 它是一个2-3树实现

        <?php # index.php

        require_once ('./includes/config.inc.php');

        $page_title = 'Title';
        include ('includes/header.html');

        if (!isset($_SESSION['admin_int_id'])) {

           $url = 'http://' . $_SERVER['HTTP_HOST']
            . dirname($_SERVER['PHP_SELF']);
           // Check for a trailing slash.
           if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
                $url = substr ($url, 0, -1); // Chop off the slash.
           }
           $url .= '/login.php'; 

        ob_end_clean(); // Delete the buffer.
        header("Location: $url"); 
        exit(); // Quit the script.
        }

        if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) )
           { // Accessed through view_users.php   
            $id = $_GET['id'];

        } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) )
           { // Form has been submitted.   
            $id = $_POST['id'];
        } else { // No valid ID, kill the script.
            echo '<h1 id="mainhead">Page Error</h1>
            <p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
            include ('./includes/header.html'); 
            exit();
        }

        require_once ('/database.php'); // Connect to the db.

        $display = 1000;

        if (isset($_GET['np'])) { // Already been determined.
            $num_pages = $_GET['np'];
        } else { // Need to determine.

            $query = "SELECT COUNT(*) FROM tally_point, users WHERE tally_point.users_id = users.users_id ORDER BY tally_points_entry_date DESC";   
            $result = @mysql_query ($query);
            $row = @mysql_fetch_array ($result, MYSQL_NUM);
            $num_records = $row[0];

            if ($num_records > $display) { // More than 1 page.
               $num_pages = ceil($num_records/$display);
            } else {
               $num_pages = 1;
            }

        } 

        if (isset($_GET['s'])) {
            $start = $_GET['s'];
        } else {
            $start = 0;
        }

        $link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
        $link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
        $link3 = "{$_SERVER['PHP_SELF']}?sort=dra";

        if (isset($_GET['sort'])) {

            switch ($_GET['sort']) {
               case 'lna':
                  $order_by = 'tally_points_in ASC';
                  $link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
                  break;
               case 'lnd':
                  $order_by = 'tally_points_in DESC';
                  $link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
                  break;
               case 'fna':
                  $order_by = 'total ASC';
                  $link2 = "{$_SERVER['PHP_SELF']}?sort=fnd";
                  break;
               case 'fnd':
                  $order_by = 'total DESC';
                  $link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
                  break;
               case 'dra':
                  $order_by = 'tally_points_entry_date ASC';
                  $link3 = "{$_SERVER['PHP_SELF']}?sort=drd";
                  break;
               case 'drd':
                  $order_by = 'tally_points_entry_date DESC';
                  $link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
                  break;      
               default:
                  $order_by = 'tally_points_entry_date DESC';
                  break;
            }

            $sort = $_GET['sort'];

        } else { // Use the default sorting order.
            $order_by = 'tally_points_entry_date DESC';
            $sort = 'dra';
        }


        $query =   "SELECT ta.tally_points_in, ta.order_id, ta.total, ta.tpt_id , DATE_FORMAT(ta.tally_points_entry_date, '%d-%m-%Y') AS dr, ta.users_id
                    FROM tally_point AS ta
                    WHERE ta.users_id=$id 
                    ORDER BY
        ".$order_by." LIMIT ".$start.", ".$display;

        $result = @mysql_query ($query); // Run the query.

        echo ' 
        <table width="500" cellspacing="1" cellpadding="7">
        <tr class="top">
            <td align="left"><b>Date</b></td>
            <td align="center"><b>Credit</b></td>
            <td align="center"><b>Debit</b></td>
            <td align="center"><b>Description</b></td>

        </tr>
        ';

        $bg = '#ffffff'; // Set the background color.

        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $pointsitem = $row['order_id'];
            $pointstype = $row['tpt_id']; 

            $bg = ($bg=='#eaeced' ? '#ffffff' : '#eaeced'); // Switch the background color.   
            //$entries = floor($row['ltd_entry_amount']/200);
            echo '<tr bgcolor="' . $bg . '">';
            echo '<td align="left">' . $row['dr'] . '</td>';
            echo  '<td align="center"><strong>' . $row['tally_points_in'] . '</strong></td> ';
            echo  '<td align="center">' . $row['total'] . '</td>';


            $type = '<td align="center">';
            if($pointstype > '0') {
            $query = "SELECT tpt_name
            FROM tally_point_type
            WHERE'" . $row['tpt_id'] . "'=$pointstype"; //THIS ALSO SEEMS WRONG column name should have backticks if you're trying to escape it and maybe value should be quoted? Also these values are the same, no? 
            $result = mysql_query($query);
            $tpt_name = mysql_fetch_assoc($result);
            $type .='<strong>' . $tpt_name['tpt_name'] . '</strong></td></tr>';
            } else {
            $type .='<strong><a href="view-ind-order.php?id=' . $pointsitem . '">Order Details</a></strong></td></tr>';
            }
            echo $type;


            }


        echo '</table>';

        mysql_free_result ($result); // Free up the resources.  

        mysql_close(); // Close the database connection.

        if ($num_pages > 1) {

            echo '<br /><p>';
            $current_page = ($start/$display) + 1;

            if ($current_page != 1) {
               echo '<a href="view_points_2.php?s=' . ($start - $display) . '&np=' .
                 $num_pages . '&sort=' . $sort .'">Previous</a> ';   
            }

            for ($i = 1; $i <= $num_pages; $i++) {
               if ($i != $current_page) {
                  echo '<a href="view_points_2.php?s=' . (($display * ($i - 1))) . 
                    '&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> ';   
               } else {
                  echo $i . ' ';
               }
            }

            if ($current_page != $num_pages) {
               echo '<a href="view_points_2.php?s=' . ($start + $display) . '&np=' . 
                 $num_pages . '&sort=' . $sort .'">Next</a> ';   
            }

            echo '</p>';

        }  
        include ('./includes/footer.html'); // Include the HTML footer.
        ?>

所以我可以使用文本文件在我的树中插入数字,但现在我需要一个函数来帮助我搜索节点任何有关如何开始的指针?
非常感谢帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

看起来你非常接近。如果你知道如何搜索链表,那就是一个类似的过程。

列表:

while (this_node.value != search_term)
    this_node = this_node.next;

2-3树有点复杂:

while (this_node.value != search_term)
    pick_which_node_is_next();

从你的代码看起来就像你可以处理这个,没问题。