408-数据结构-代码题

文章介绍了C++中二叉树的链式存储结构、中序遍历方法,以及链表的操作,包括快慢指针寻找中间节点、反转链表和链表重排。
摘要由CSDN通过智能技术生成

2014

2014 二叉树(链式存储)

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

typedef struct Node{
	struct Node *left;
	struct Node *right;
	int high=0;
	double weight; 
}node;

double sum=0;

void visit(node *t){
	int lop=0;
	if(t->left != NULL){
		t->left->high = t->high+1;
		lop = 1;
	}
	if(t->right != NULL){
		t->right->high = t->high+1;
		lop = 1;
	}
	if(lop == 0){
		sum += t->weight * t->high;
	}
}

void dfs(node *t){
	if(t != NULL){
		cout << t->weight << endl;
		visit(t);
		dfs(t->left);
		dfs(t->right);
	}
}

node* buildnode(int w){
	node* tmp = (node *)malloc(sizeof(node));
//	tmp->high = 0;
	tmp->weight = w;
	tmp->left = tmp->right = NULL;
	return tmp;
}

void first(node *t){
	if(t != NULL){
	//	visit(t);
		cout << t->weight << "  " << t->high << endl;
		first(t->left);
		first(t->right);
	}
	
// also 
//	if(t == NULL)  return; 
//	//	visit(t);
//	cout << t->weight << endl;
//		
//	first(t->left);
//	
//	first(t->right);
//	
}

int main(){
	
	node* n1 = buildnode(1);
	node* n2 = buildnode(2);
	node* n3 = buildnode(3);
	node* n4 = buildnode(4);
	node* n5 = buildnode(5);
	node* n6 = buildnode(6);
	node* n7 = buildnode(7);
	
	n1->left = n2;
	n1->right = n3; 
	n2->left = n4;
	n2->right = n5;
	n3->right = n6;
	n4->right = n7;
	
	dfs(n1);
	cout << "sum = " <<  sum << endl; 
	first(n1);
	
	return 0;
} 

2017

2015 单链表(带头结点)

#include<iostream>
#include<cstring>
using namespace std;

typedef struct Node{
	int data;
	struct Node *next; 
}node,*list;
 
const int N = 100010;
int a[N];
void func(Node *head){
	node *p;
	node *r;
	p = head;
	while(1){
		int x = p->next->data;
		if(x < 0) x = -1*x;
		if(a[x] == 0){
			a[x] = 1;
			p = p->next;
		}
		else{
			r = p->next;
			p->next = r->next;
			free(r);
		}
		if(p->next == NULL) break;
	}
}
 
node* create(int n){
	node* p;
	p = (node*)malloc(sizeof(node));
	p->data = n;
	p->next = NULL;
}
 
void func2(Node *head){
	node *p;
	p = head;
	while(p->next != NULL){
		cout << p->next->data << " ";
		p = p->next;
	}
	return;
}
 

int main(){
	node *head;
	node *s1;
	node *s2;
	node *s3;
	node *s4;
	
	head = create(-1);
	s1 = create(2);
	s2 = create(2);
	s3 = create(1);
	s4 = create(1);
	
	head->next = s1;
	s1->next = s2;
	s2->next = s3;
	s3->next = s4;
	
	// bianli
	func2(head); 
	
	func(head);
	
	cout << endl;
	
	func2(head);

	return 0;
}
 

2017

2017 二叉树中序遍历

#include<iostream>

using namespace std;

typedef struct Node{
	char data;
	struct Node *left, *right;
}node;

void visit(node *t){
	cout << t->data << " ";
}

void bianli(node *t,int h){
	if(t != NULL){
	// 根 和 叶子 
		if(h > 0 && t->left!=NULL && t->right!=NULL) cout << "(";
		bianli(t->left,h+1);
		visit(t);
		bianli(t->right,h+1);
		if(h > 0 && t->left!=NULL && t->right!=NULL) cout << ")";
	}	
}

node* create(char data){
	node* s;
	s = (node *)malloc(sizeof(node));
	s->data = data;
	s->right = NULL;
	s->left = NULL;
	return s;
}

int main(){
	node *s1 = create('*');
	node *s2 = create('+');
	node *s3 = create('4');
	node *s4 = create('-');
	node *s5 = create('3');
	node *s6 = create('2');
	node *s7 = create('1');
	
	s1->left = s2;
	s1->right = s3;
	s2->left = s4;
	s2->right = s5;
	s4->right = s6;
	s4->left = s7;
	
	bianli(s1,0);
	
	return 0;	
} 

2019

2019 链表(快慢指针、反转链表、链表重排)

#include<iostream>
#include<cstring>

using namespace std;


typedef struct Node{
	int data;
	Node *next;
}node;

node* create(int data){
	node *n = (node *)malloc(sizeof(node));
	n->data = data;
	n->next = NULL;
	return n;
}

void print(node *head){
	node* p;
	p = head;
	while(p->next != NULL){
		cout << p->next->data << " ";
		p = p->next;
	}
	cout << endl;
}


void reverse(node *head){
	node *p;
	p = head->next;
	head->next = NULL;
	while(p != NULL){
		node *t = p->next;
		p->next = head->next;
		head->next = p;
		p = t;
	}
}

void change(node *head){
	
	node *q, *p;
	
	// 1. 快慢指針 來确定中间位置
	q = head;
	p = head;
	while(q->next != NULL){
		p = p->next;
		q = q->next;
		if(q->next != NULL) q = q->next;
	} 
	
//	cout << "## " << q->data << " " << p->data << endl;
	
	// 2. 反转链表
	// 头插法 头就是 p
	
	reverse(p);
	
//	cout << "p:  " << p->data << endl;
	
//	node *h;
//	h = p;
//	h->next = NULL;
//	p = p->next;
//	while(p!=NULL){
//		node *t = p->next;
//		p->next = head->next;
//		h->next = p;
//		p = t;
//	}

//	cout << "T: ";
//	print(head);
	
	// 3. 按题目要求进行插入
	node *f = head->next;
	node *r = p->next;
	p->next = NULL;
	
//	r = r->next;
//	if(r->next == NULL)  cout << "!!!!!!!!!!!!!!!!!!";
//	cout << "r:  " << r->data << endl;
	
	while(r!=NULL){
		node* t2 = r->next;
		r->next = f->next;
		f->next = r;
		f = r->next; 
		r = t2;
	} 
	 
}


int main(){
	
	node* head = create(-1);
	node* n1 = create(1);
	node* n2 = create(2);
	node* n3 = create(3);
	node* n4 = create(4);
	node* n5 = create(5);
	
	head->next = n1;
	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = n5;
	n5->next = NULL;
	// n1->n2->n3->n4->n5
	
	print(head);
	
//	reverse(head);
	
//	print(head);
	
	change(head);
	
	print(head);
	
	return 0;
}
 

模拟题1

输出二叉树任意节点到根节点的路径(链式存储)

#include<iostream>
#include<cstring>

using namespace std;

typedef struct Node{
	int num;
	struct Node *left, *right;
	struct Node *front;
}node;

void visit(node *t){
	if(t->left != NULL){
		t->left->front = t;
	}
	if(t->right != NULL){
		t->right->front = t;
	}
}

void first(node* t){
	if(t!=NULL){
		visit(t);
		cout << t->num << endl;
		first(t->left);
		first(t->right);
	}
}




void print(node *t){
	cout << "the route is ";
	while(1){
		cout << t->num << " ";
		t = t->front;
		if(t == NULL) break;
	}
	cout << endl;
}

node* create(int num){
	node* s;
	s = (node *)malloc(sizeof(node));
	s->num = num;
	s->front = NULL;
	s->right = NULL;
	s->left = NULL;
	return s;
}

int main(){
	node *s1 = create(1);
	node *s2 = create(2);
	node *s3 = create(3);
	node *s4 = create(4);
	node *s5 = create(5);
	node *s6 = create(6);
	node *s7 = create(7);
	
	s1->left = s2;
	s1->right = s3;
	s2->left = s4;
	s2->right = s5;
	s3->left = s6;
	s4->left = s7;
	
	first(s1);
	
	print(s7);
	
	return 0;	
} 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值