所以我在接受采访时被问到,如果可能检查树是否为avl tree,可能是: T(n)= O(log(n))
并且不知道答案,在谷歌搜索后,我看到的最佳算法是O(n)(“https://www.geeksforgeeks.org/how-to-determine-if-a-binary-tree-is-balanced/”)
是用O(log(n))生成算法吗?
答案 0 :(得分:0)
答案有点依赖。如果平衡因子被明确地存储在节点中,则可以通过从根节点读取值来在O(log n)
时间内检查平衡。所以假设平衡因子没有明确存储。
请注意,在#include <Servo.h>
Servo servo1;
Servo servo2;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
servo1.attach(2);
servo2.attach(4);
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
servo1.write(map(integerFromPC, 0, 255, 0, 180));
strtokIndx = strtok(NULL, ",");
floatFromPC = atoi(strtokIndx); // convert this part to a float
servo2.write(map(floatFromPC, 0, 255, 0, 180));
}
//============
void showParsedData() {
}
时间内,无法读取整个输入。