用于查找树是否为单声道(所有元素都是唯一的)的功能?

时间:2013-08-07 19:09:12

标签: c++ unix recursion binary-search-tree binary-search

我试图将Total函数和isMono函数添加到此代码中。总计已经

需要帮助函数ismono,它返回树是否是单声道(所有元素都是唯一的,也就是说没有元素出现过多次)。请

#ifndef T_H

#define T_H

#include <iostream>
#include <iomanip>
using namespace std;

struct tnode {
    int info ;
    int count;
    tnode * right, *left;
};

tnode * insert(int target,tnode * t);
tnode * makenode(int x);
tnode * tsearch(int x,tnode * t);
void inorder(tnode * t);
int height(tnode * t);
int count(tnode * t) ;
int total(tnode * t) ;

#endif

int main() {
int n,c;
tnode * t = NULL, *x;
    while (cin >> n) {t=insert(n,t);cout << n <<' ';}
    cout << endl;
    inorder(t);
    cout << endl;
    c = count(t);
    cout << "count: "<< c  <<endl;
    cout << endl;
    c = height(t);
    cout << "height: "<< c  <<endl;
    cout << endl;
    c=200;
    while (c-->0) if (x = tsearch(c,t)) cout << c << " is on the tree."<<endl;
return 0;
}

#include "t.h"

int count(tnode * t) {
    if (t == NULL) return 0;
    return 1 +  count(t->left) + count (t->right);
}

#include "t.h"

int height(tnode * t) {
    if (t == NULL) return -1;
    return 1 + max(height(t->left) , height (t->right));
}

#include "t.h"

//write out t in order
void inorder(tnode * t) {
    if (t == NULL) return;
    inorder (t->left);//write out lst in order
    cout <<setw(5) << t->info <<setw(5) << t->count<< endl;
    inorder (t->right);//write out rst in order
}

#include "t.h"

tnode * insert(int x, tnode * t) {
tnode * tmp = tsearch(x,t);
if (tmp != NULL) {
    tmp->count++;
    return t;
}
if (t == NULL) return makenode(x);
if ( x < t->info ) {
    t->left = insert(x,t->left);
    return t;
}
t->right = insert(x,t->right);
return t;
}

#include "t.h"

tnode * makenode(int x) {
tnode * t = new tnode;
    t->info =x;
    t->count =1;
    t->right = t->left = NULL;
return t;
}

2 个答案:

答案 0 :(得分:1)

只穿过树!

//Gets the value of the node at the leftmost node
int left_most_value(tnode * t) {
    if (t == NULL) return (0); //Something went wrong
    if (t->left == NULL) return(t->info);
    else return(left_most_value(t));
}

//Gets the value of the node at the rightmost node    
int right_most_value(tnode * t) {
    if (t == NULL) return (0); //Something went wrong
    if (t->right == NULL) return(t->info);
    else return(right_most_value(t));
}

//Returns true (1) if node does NOT have duplicate
int node_is_mono(tnode * t) {
    //Ignore leaf nodes
    if (t->left == NULL && t->right == NULL) return 1;

    //Check the left side
    if (t->left != NULL && right_most_value(t->left) == t->info) return(0);

    //Check the right side
    if (t->right != NULL && left_most_value(t->right) == t->info) return(0);

    //This node is mono
    return(1);
}

int tree_is_mono(tnode * t) {
    if (t == NULL) return(1);

    //If one node has a duplicate, then the entire tree is NOT mono 
    if (node_is_mono(t) == 0) return 0;
    else return(tree_is_mono(t->left) && tree_is_mono(t->right));
}

算法的解释

在树中的每个节点上,您需要执行以下步骤。

  1. 从右侧节点的子节点中查找节点的最左侧节点。如果它们的值相等,则树 NOT mono(停止搜索并return false
  2. 从左侧节点的子节点中找到节点的最右侧节点。如果它们的值相等,则树单声道(停止搜索并return false
  3. 我们发现没有相同值的节点,所以树是单声道的! return true

答案 1 :(得分:0)

如果您使用的是C ++,则可以使用std::setstd::unordered_set来存储树中的对象列表。然后,使用树遍历(按顺序,预订,后序等),并对遍历中的每个对象执行以下操作:

  1. 您可以检查它是否已经在set中。如果是,则return false;
  2. 您将元素添加到set
  3. 然后,当您完成遍历时,您知道没有任何重复,所以只需return true;