#include<stdio.h>
#include<stdlib.h>
typedef struct tnode *BinTree;
struct tnode {
BinTree left, right;
char Data;
};
typedef struct snode *stack;
struct snode {
BinTree data;
stack next;
};
stack creatstack() {
stack s = (stack)malloc(sizeof(struct snode));
s->next=NULL;
return s;
}
int isempty(stack s) {
if (s->next == NULL)
return 1;
else return 0;
}
void push(stack s, BinTree BT) {
stack newnode = (stack)malloc(sizeof(struct snode));
newnode->data = BT;
newnode->next = s->next;
s->next = newnode;
}
BinTree pop(stack s) {
stack tmp = s->next;
BinTree T = s->next->data;
s->next = tmp->next;
free(tmp);
return T;
}
BinTree creatBT() {
char ch;
BinTree BT;
scanf_s("%c", &ch);
if (ch == '#')
BT = NULL;
else {
BT = (BinTree)malloc(sizeof(struct tnode));
BT->Data = ch;
BT->left = creatBT();
BT->right = creatBT();
}
return BT;
}
void inorder(BinTree BT) {
stack s = creatstack();
while (BT || !isempty(s)) {
while (BT) {
push(s, BT);
BT = BT->left;
}
while (!isempty(s)){
BT =pop(s);
printf("%c", BT->Data);
BT = BT->right;
}
}
}
int main() {
BinTree BT;
BT = creatBT();
inorder(BT);
}
#include<stdio.h>
#include<stdlib.h>
typedef struct tnode *BinTree;
struct tnode {
BinTree left, right;
char data;
};
typedef struct node*Node;
struct node {
BinTree data;
Node next;
};
typedef struct qnode* queue;
struct qnode{
Node front, rear;
};
queue creatqueue() {
queue Q = (queue)malloc(sizeof(struct qnode));
Q->front = Q->rear = NULL;
return Q;
}
int isempty(queue Q) {
if (Q->front == NULL)return 1;
else return 0;
}
void add(queue Q,BinTree BT) {
Node newnode = (Node)malloc(sizeof(struct node));
if (isempty(Q)) {
newnode->data = BT;
Q->front = Q->rear=newnode;
newnode->next = NULL;
}
else {
newnode->data = BT;
Q->rear->next = newnode;
newnode->next = NULL;
Q->rear = newnode;
}
}
BinTree Delete(queue Q){
Node tmp = Q->front;
BinTree x = Q->front->data;
if (Q->front->next == NULL) {
Q->front = Q->rear = NULL;
}
else {
Q->front = Q->front->next;
}
free(tmp);
return x;
}
BinTree creatBT() {
BinTree BT;
char ch;
scanf_s("%c", &ch);
if (ch == '#')
BT = NULL;
else {
BT = (BinTree)malloc(sizeof(struct tnode));
BT->data = ch;
BT->left = creatBT();
BT->right = creatBT();
}
return BT;
}
void leverorder(BinTree BT) {
BinTree T=BT;
queue Q = creatqueue();
add(Q, BT);
while (!isempty(Q)) {
T = Delete(Q);
printf("%C", T->data);
if (T->left)add(Q, T->left);
if (T->right)add(Q, T->right);
}
}
int main() {
BinTree BT=creatBT();
leverorder(BT);
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
struct node *left;
struct node *right;
int data;
}Node;
typedef struct {
Node *root;
} Tree;
void insert(Tree *tree,int value){
Node *node=malloc(sizeof(struct node));
node->data=value;
node->left=NULL;
node->right=NULL;
if(tree->root==NULL){
tree->root=node;
}
else{
Node *temp=tree->root;
while(temp!=NULL){
if(value<temp->data){
if(temp->left==NULL){
temp->left=node;
return;
}
else {
temp=temp->left;
}
}
else{
if(temp->right==NULL){
temp->right=node;
return ;
}
else temp=temp->right;
}
}
}
}
int get_height(Node *node){
if(node==NULL){
return 0;
}
else{
int left_h=get_height(node->left);
int right_h=get_height(node->right);
int max=left_h;
if(right_h>left_h) max=right_h;
return max+1;
}
}
void preorder(Node *node){
if(node!=NULL){
printf("%d\n",node->data);
preorder(node->left);
preorder(node->right);
}
}
void inorder(Node *node){
if(node!=NULL){
inorder(node->left);
printf("%d\n",node->data);
inorder(node->right);
}
}
void postorder(Node *node){
if(node!=NULL){
postorder(node->left);
postorder(node->right);
printf("%d\n",node->data);
}
}
int main(void){
int arr[]={6,3,8,2,5,1,7};
Tree tree;
tree.root=NULL;
int i;
for(i=0;i<7;i++){
insert(&tree,arr[i]);
}
int h=get_height(tree.root);
printf("%d",h);
return 0;
}