我正在尝试实施一个包含有序库存清单的二叉搜索树。库存商品属性存储在节点中:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var myPicker: UIPickerView!
let pickerData0 = ["0R","1R","2R","3R","4R","5R","6R"]
let pickerData1 = ["0B","1B","2B","3B","4B","5B","6B"]
let pickerData2 = ["0Y","1Y","2Y","3Y","4Y","5Y","6Y"]
let pickerData3 = ["0G","1G","2G","3G","4G","5G","6G"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myPicker.dataSource = self
myPicker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Mark: Delegates and data sources
//Mark: Data Sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 4
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData0.count
}
//Mark: Delegates
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let redIndex = myPicker.selectedRowInComponent(0)
let blueIndex = myPicker.selectedRowInComponent(1)
let yellowIndex = myPicker.selectedRowInComponent(2)
let greenIndex = myPicker.selectedRowInComponent(3)
if redIndex + blueIndex + yellowIndex + greenIndex == 6 {
myPicker.backgroundColor = UIColor.cyanColor()
} else {
myPicker.backgroundColor = UIColor.whiteColor()
}
}
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
switch component {
case 0:
var pickerLabel = view as! UILabel!
if view == nil {
pickerLabel = UILabel()
pickerLabel.textColor = UIColor.redColor()
pickerLabel.backgroundColor = UIColor.grayColor()
pickerLabel.textAlignment = NSTextAlignment.Center
pickerLabel.text = pickerData0[row]
pickerLabel.font = UIFont(name: "Georgia", size: 26)
}
return pickerLabel
case 1:
var pickerLabel = view as! UILabel!
if view == nil {
pickerLabel = UILabel()
pickerLabel.textColor = UIColor.blueColor()
pickerLabel.backgroundColor = UIColor.grayColor()
pickerLabel.textAlignment = NSTextAlignment.Center
pickerLabel.text = pickerData1[row]
pickerLabel.font = UIFont(name: "Georgia", size: 26)
}
return pickerLabel
case 2:
var pickerLabel = view as! UILabel!
if view == nil {
pickerLabel = UILabel()
pickerLabel.textColor = UIColor.yellowColor()
pickerLabel.backgroundColor = UIColor.grayColor()
pickerLabel.textAlignment = NSTextAlignment.Center
pickerLabel.text = pickerData2[row]
pickerLabel.font = UIFont(name: "Georgia", size: 26)
}
return pickerLabel
case 3:
var pickerLabel = view as! UILabel!
if view == nil {
pickerLabel = UILabel()
pickerLabel.textColor = UIColor.greenColor()
pickerLabel.backgroundColor = UIColor.grayColor()
pickerLabel.textAlignment = NSTextAlignment.Center
pickerLabel.text = pickerData3[row]
pickerLabel.font = UIFont(name: "Georgia", size: 26)
}
return pickerLabel
default:
return pickerView
}
}
}
这个想法是提示用户输入上述属性,然后将输入的项添加到节点。这是我到目前为止所写的:
typedef struct item item_t;
struct item{
char name;
int price;
int quantity;
item_t *left;
item_t *right;
};
我希望item_t *root = NULL;
item_t *current_leaf = NULL;
void prompt_user(){
/*
In here contains the code that prompts the user for the item attributes
and stores it in a variable called input
*/
insert_node(input);
}
void insert_node(char *input){
/*If tree doesnt have a root...*/
if (root == NULL){
/*Create one...*/
root = create_node(input);
}
else{
item_t *cursor = root;
item_t *prev = NULL;
int is_left = 0;
int comparison;
while(cursor != NULL){
/*comparison will be 1 is the key of input is less than the key
of the cursor, and 2 otherwise...*/
comparison = compare(input, cursor);
prev = cursor;
if(comparison == 1){
is_left = 1;
cursor = cursor->left;
}
else if (comparison == 2){
is_left = 0;
cursor = cursor->right;
}
}
if(is_left){
*prev->left = create_node(input);
current_leaf = prev->left;
}
else{
*prev->right = create_node(input);
current_leaf = prev->right;
}
}
}
item_t create_node(char *input){
item_t *new_node = (item_t*)malloc(sizeof(item_t));
if (new_node == NULL){
printf("Out of memory. Shutting down.\n");
exit(EXIT_FAILURE);
}
/*Add data to the node...*/
update_item(input, new_node);
new_node->left = NULL;
new_node->right = NULL;
current_leaf = new_node;
return new_node;
}
始终指向已输入的第一个项目,root
指向最后处理的项目。如果正在处理的项目(current_leaf
)小于上次处理的项目(compare
),则input
会返回1。 current_leaf
用于设置新节点(叶子)的数据。
上述内容尚未完全完成,但这是我目前正在做的事情。我正在努力弄清楚如何撰写update_item
以及如何正确更新add_node
。
当我尝试编译时,我收到以下错误:
current_leaf
答案 0 :(得分:3)
item_t create_node(char *input)
应该是
item_t *create_node(char *input)
您返回的是一个结构,但您应该返回struct item
类型的指针。