二叉查找树<链表实现>

thinking:第一个结点作为根结点,然后接下来每个结点与根结点对比,如大于根结点则放右边,小于根结点则放左边,然后比较下一个结点,知道遇到空节点则储存、

输入样例:

9
6 3 8 5 2 9 4 7 10

输出样例:(前序遍历)

6 3 2 5 4 8 7 9 10

代码:

#include <stdlib.h>
#include <iostream>
using namespace std;

struct tree{
	tree *left;
	tree *right;
	int data;
};

typedef struct tree *b_tree;

b_tree insert(b_tree root,int node){
	b_tree parentnode;
	b_tree newnode;
	b_tree currentnode;
	
	newnode=(b_tree)malloc(sizeof(tree));
	newnode->data=node;
	newnode->right=NULL;
	newnode->left=NULL;
	if(root==NULL)
		return newnode;
		else{
			currentnode=root;
			while(currentnode!=NULL){
				parentnode=currentnode;
				if(currentnode->data>node)
					currentnode=currentnode->right;
					else
					currentnode=currentnode->left;
			}
			if(parentnode->data>node)
				parentnode->right=newnode;
				else
				parentnode->left=newnode;
				
		}
		return root;
}

b_tree creat(int *node,int len){
	b_tree root=NULL;
	for(int i=1;i<=len;i++)
		root=insert(root,node[i]);
	return root;
}

void print(b_tree root){
	if(root!=NULL){
		printf("%d ",root->data);
		print(root->right);
		print(root->left);
	}
}

int main(){
	freopen("in.txt","r",stdin);
	int i,n;
	cin >> n;
	b_tree root;
	int node[n+1];
	for(i=1;i<=n;i++)
		cin >> node[i];
		root=creat(node,n);
	print(root);
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言实现二叉查找树变为有序的双向链表的代码和注释说明: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树结构体 struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; // 定义双向链表结构体 struct ListNode { int val; struct ListNode *prev; struct ListNode *next; }; // 将二叉查找树转换为有序的双向链表 struct ListNode* treeToDoublyList(struct TreeNode* root) { if (root == NULL) { return NULL; } struct ListNode *head = NULL, *tail = NULL; // 中序遍历二叉树 inorder(root, &head, &tail); // 将双向链表首尾相连 head->prev = tail; tail->next = head; return head; } // 中序遍历二叉树 void inorder(struct TreeNode* root, struct ListNode** head, struct ListNode** tail) { if (root == NULL) { return; } inorder(root->left, head, tail); // 将当前节点转换为双向链表节点 struct ListNode *node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = root->val; node->prev = *tail; node->next = NULL; if (*tail != NULL) { (*tail)->next = node; } *tail = node; if (*head == NULL) { *head = node; } inorder(root->right, head, tail); } // 测试代码 int main() { // 构造二叉查找树 struct TreeNode *root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = 4; root->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->left->val = 2; root->left->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->left->left->val = 1; root->left->left->left = NULL; root->left->left->right = NULL; root->left->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->left->right->val = 3; root->left->right->left = NULL; root->left->right->right = NULL; root->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->right->val = 5; root->right->left = NULL; root->right->right = NULL; // 将二叉查找树转换为有序的双向链表 struct ListNode *head = treeToDoublyList(root); // 遍历双向链表 struct ListNode *cur = head; while (cur != NULL) { printf("%d ", cur->val); cur = cur->next; } printf("\n"); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值