二叉树类的实现

时间:2015-05-29 12:09:51

标签: c++ algorithm binary-tree

我有以下代码。它创建了一个二叉树类。函数是insert(),pre_order(),post_order(),in_order()。但是当我调试它时,我得到了零值。我也插入9个值,但只有7个零。为什么我做错了?

#include <iostream>

using namespace std;

//Begin the construction of the BINARY TREE
struct tree_node {
  tree_node *left;
  tree_node *right;
  int data;
};

//Declaring the class
class bst {
  tree_node *root; //creating the root of the binary tree

  public:
    bst() {
      root = NULL; //intialize the default construction, set the root to NULL
    }

    int is_empty() { //check for empty graph
      return (root == NULL);
    }

    //Manipulating the Binary Tree
    void insert(int item);
    void remove_it(int value); //difficult implementation

    //Graph Traversal of Binary Tree
    void in_order_trav();
    void in_order(tree_node *);
    void pre_order_trav();
    void pre_order(tree_node *);
    void post_order_trav();
    void post_order(tree_node *);
};

void bst::insert(int item) {
  tree_node *p = new tree_node;
  tree_node *parent;
  p->left = NULL;
  p->right = NULL;
  parent = NULL;

  if (is_empty()) {
    root = p;
  }
  else {
    tree_node *ptr;
    ptr = root;

    while (ptr != NULL) {
      parent = ptr;

      if (item > ptr->data)
        ptr = ptr->right;
      else
        ptr = ptr->left;
    }

    if (item < parent->data) 
      parent->left = p;
    else
      parent->right = p;
  }
}
/*************In Order Traversal*****************************/
// Begin
void bst::in_order_trav() {
  in_order(root);
}

void bst::in_order(tree_node *ptr) {
  if (ptr!=NULL) {
    in_order(ptr->left);
    cout << " " << ptr->data << "     ";
    in_order(ptr->right);
  }
}
// End
/***********************************************************/

/*************Pre Order Traversal*****************************/
// Begin
void bst::pre_order_trav() {
  pre_order(root);
}

void bst::pre_order(tree_node *ptr) {
  if (ptr!=NULL) {
    cout << "  " << ptr->data << "     ";
    pre_order(ptr->left);
    pre_order(ptr->right);
  }
}
// End
/***********************************************************/

/*************Post Order Traversal*****************************/
// Begin
void bst::post_order_trav() {
  post_order(root);
}

void bst::post_order(tree_node *ptr) {
  if(ptr!=NULL) {
    post_order(ptr->left);
    post_order(ptr->right);
    cout << "  " << ptr->data << "     ";
  }
}
// End
/***********************************************************/

int main() {
  bst bin_tree; //create the Binary Tree
  bin_tree.insert(20);
  bin_tree.insert(30); 
  bin_tree.insert(52);  
  bin_tree.insert(254); 
  bin_tree.insert(2); 
  bin_tree.insert(24); 
  bin_tree.insert(25); 
  bin_tree.insert(42); 
  bin_tree.insert(59); 
  bin_tree.in_order_trav(); //in order traversal
  bin_tree.pre_order_trav(); //pre order traversal
  bin_tree.post_order_trav(); //post order traversal
}

2 个答案:

答案 0 :(得分:0)

应在函数insert()处初始化节点值(p-> data = item),如下所示

 DefaultClientConfig defaultClientConfig = new DefaultClientConfig();
    defaultClientConfig.getClasses().add(JacksonJsonProvider.class);

    Client client = Client.create(defaultClientConfig);
    WebResource webResource = client.resource(apiUrl);
    ClientResponse response =
                webResource.path(apiUrl).accept("application/json")
                .type("application/json").post(ClientResponse.class, params);

答案 1 :(得分:0)

好的解决方案很傻! -.-

我忘了在插入例程中添加该行!

bool Compare(DataColumn[] primary, DataColumn[] secondary)
{
  if (primary.Length != secondary.Length) return false;
  var names = new HashSet<string>(secondary.Select(col => col.ColumnName));

  return primary.All(col => names.Contains(col.ColumnName));
}