//二叉树非递归中序遍历
#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);
}



//二叉搜索树:
//跟>zuo
//gen<you;
#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]);
	}
//	preorder(tree.root);
	int h=get_height(tree.root);
	printf("%d",h);
	return 0; 
}
 
 
 
 
 
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值