每周专业学习周记-1
Posted xxxxxxxxxx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每周专业学习周记-1相关的知识,希望对你有一定的参考价值。
每周专业学习周记-1
标签(空格分隔): 周记
说明:接下来的每一周都将会跟新我的专业学习周记,内容包括代码+总结。目前主要除了课程内的《数据结构与算法》课程之外,还有Python语言学习,Python作为现在比较热门并且易入门的编程语言,学习它无论是用作工具还是提升自己都会有很好的帮助,教材使用【美】《Python编程从入门到实践》。
接下来的每周我将会持续更新我的学习情况。
数据结构篇
#include <stdio.h>
#include <stdlib.h>
#define DataType char
int count;
typedef struct Node
{
DataType data;
struct Node* LChild;
struct Node* RChild;
}BiTNode,*BiTree;
void GreateBiTree(BiTree *root)
{
char ch;
ch = getchar();
if(ch==‘#‘)
*root = NULL;
else
{
*root = (BiTree)malloc(sizeof(BiTNode));
(*root)->data = ch;
GreateBiTree(&((*root)->LChild));
GreateBiTree(&((*root)->RChild));
}
}
void PreOrder(BiTree root)
{
if(root==NULL)
return;
else
{
printf("%c",root->data);
PreOrder(root->LChild);
PreOrder(root->RChild);
}
}
void InOrder(BiTree root)
{
if(root == NULL)
return;
else
{
InOrder(root->LChild);
printf("%c",root->data);
InOrder(root->RChild);
}
}
void PostOrder(BiTree root)
{
if(root == NULL)
return;
else
{
PostOrder(root->LChild);
PostOrder(root->RChild);
printf("%c",root->data);
}
}
void PreLeaf(BiTree root)
{
if (root!=NULL)
{
if (root->LChild==NULL && root->RChild==NULL)
printf("%c",root->data);
PreLeaf(root->LChild);
PreLeaf(root->RChild);
}
}
void InLeaf(BiTree root)
{
if (root!=NULL)
{
InLeaf(root->LChild);
if (root->LChild==NULL && root->RChild==NULL)
printf("%c",root->data);
InLeaf(root->RChild);
}
}
void PostLeaf(BiTree root)
{
if (root!=NULL)
{
PostLeaf(root->LChild);
PostLeaf(root->RChild);
if (root->LChild==NULL && root->RChild==NULL)
printf("%c",root->data);
}
}
void LeafCount(BiTree root)
{
if (root!=NULL)
{
LeafCount(root->LChild);
LeafCount(root->RChild);
if (root->LChild==NULL && root->RChild==NULL)
count++;
}
}
int LeafCountB(BiTree root)
{
int Count;
if (root==NULL)
Count = 0;
else if(root->LChild==NULL && root->RChild==NULL)
Count = 1;
else
Count = LeafCountB(root->LChild) + LeafCountB(root->RChild);
return (Count);
}
int PostTreeDepth(BiTree root)
{
int left,right,max;
if (root!=NULL)
{
left = PostTreeDepth(root->LChild);
right = PostTreeDepth(root->RChild);
max = left>right?left:right;
return(max+1);
}
else
return(0);
}
int PreTreeDepth(BiTree root)
{
int depth=0,h=1;
h++;
if(root != NULL)
{
if (h>depth) depth = h;
PreTreeDepth(root->LChild);
PreTreeDepth(root->RChild);
}
return(h);
}
int main()
{
BiTree root;
GreateBiTree(&root);
printf("先序遍历是:");
PreOrder(root);
printf("
中序遍历是:");
InOrder(root);
printf("
后序遍历是:");
PostOrder(root);
printf("
前序遍历打印叶子节点:");
PreLeaf(root);
printf("
中序遍历打印叶子节点:");
InLeaf(root);
printf("
后序遍历打印叶子节点:");
PostLeaf(root);
LeafCount(root);
printf("
全局变量统计叶子节点个数为:");
printf("%d",count);
printf("
分治算法统计叶子节点个数为:");
printf("%d",LeafCountB(root));
printf("
后续遍历求二叉树的高度为:");
printf("%d",PostTreeDepth(root));
printf("
先序遍历求二叉树高度的递归算法:");
printf("%d ",PreTreeDepth(root));
return 0;
}
数据结构小结
以上代码题目分别是:
(1)按照课本中二叉树的链式存储的定义,实现二叉树的链式定义,实现以下二叉树的创建(二叉树中存储的数据为字符类型数据,输入数据时候空子树用“#”):root指向根结点
(2)分别采用先序、中序、后序输出上述二叉树的结点数据信息。
(3)-(7)选作
(3)假设二叉树采用链接存储结构进行存储,root指向根结点,输出二叉树中的叶子结点。(先序,中序,后序)
(4)假设二叉树采用链接存储结构进行存储,root指向根结点,统计叶子结点数目(全局变量,分治算法)。
(5)假设二叉树采用链接存储结构进行存储,root指向根结点,求二叉树的高度。
递归算法的理解始终是有些难度,需要一层一层的慢慢通过栈的方式出栈入栈.
Python篇
#4.11遍历整个列表
magicians = [‘alice‘,‘david‘,‘carolian‘]
for magician in magicians:
print(magician)
#4.1.2 在for循环中执行更多的操作
magicians = [‘alice‘,‘david‘,‘carolian‘]
for magician in magicians:
print(magician.title()+",that was a great trick!")
print("I can‘t wait to see your next trick, "+magician.title()+".
")
#4.1.3在for循环之后执行一些操作
magicians = [‘alice‘,‘david‘,‘carolian‘]
for magician in magicians:
print(magician.title()+",that was a great trick!")
print("I can‘t wait to see your next trick, "+magician.title()+".
")
print("Thank you,everyone.That was a great magic show!")
#4.2.1 忘记缩进
# magicians = [‘alice‘,‘david‘,‘carolian‘]
# for maician in magicians:
# print("I can‘t wait to see your next trick, "+magician.title()+".
")
#IndentationError: expected an indented block 这个提示就是没有缩进
#4.2.2 忘记缩进额外的代码行
magicians = [‘alice‘,‘david‘,‘carolian‘]
for magician in magicians:
print(magician.title()+",taht was a great trick!")
print("I can‘t wait to see your next trick, "+magician.title()+".
")
#4.2.3 不必要的缩进
# message = "Hello python world!"
# print(message)
#IndentationError: unexpected indent 这个提示是有不必要的缩进
#4.2.5 遗漏了冒号
# magicians = [‘alice‘,‘david‘,‘carolian‘]
# for magician in magicians
# print(magician)
#SyntaxError: invalid syntax 语法错误,这里是for后面没有加封号
#4-1 披萨
pizzas = [‘durian pizza‘,‘apple pizza‘,‘banana pizza‘]
for pizza in pizzas:
print("I like "+pizza+".")
print("I really love banana pozza!")
#4-2 动物
dogs = [‘teddy‘,‘husky‘,‘chinese rural dog‘]
for dog in dogs :
print(dog.title()+" would like a great pet.")
print("Any of these animals would make a great pet!")
#4.3.1 使用range()函数
for value in range(1,5):
print(value)
for value in range(1,6):
print(value," ",end="")
print()
#4.3.2 用range()创建数字列表
numbers = list(range(1,6))
print(numbers)
#设置步长
even_numbers = (list(range(1,11,2)))
print(even_numbers)
squares = []
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)
#4.3.3 对数字列表执行简单的统计计算
digits = [10,2,3,4,5,6,65]
print(sum(digits),max(digits),min(digits))
#4.3.4 列表解析
squares = [value**2 for value in range(1,21)]
print(squares)
#4-3 数到二十
for value in range(1,21):
print(value,end=" ")
print()
#4-4 一百万
# for value in range(1,1000000):
# print(value)
#4-5 计算1-1000000
#
#4-6 奇数
for value in range(1,20,2):
print(value,end=" ")
print()
#4-7 三的倍数
number = [value for value in range(3,30,3)]
for num in number:
print(num,end=" ")
print()
number = [value for value in range(1,30)]
for value in number:
if value % 3 == 0:
print(value,end=" ")
print()
#4-8 立方
number = []
for num in range(1,11):
number.append(num**3)
for value in number:
print(value,end=" ")
#4-9 立方解析
print()
number = [value**3 for value in range(1,11)]
print(number)
#4.4.1 切片
players = [‘charles‘,‘martina‘,‘michael‘,‘florence‘,‘eli‘]
print(players[0:3])
print(players[1:4])
#如果没有指定第一个索引,python将自动从头开始
print(players[:4])
#同样的如果没有指定后一个索引,Python将自动从第一个索引开始访问后面的所有列表值
print(players[2:])
#如果想要输出最后三名队员的名字
print(players[-3:])
#如果想要输出倒数第三个队员之前的名字
print(players[:-3])
#4.4.2 遍历切片
players = [‘charles‘,‘martina‘,‘michael‘,‘florence‘,‘eli‘]
print(‘Here are the first three players on my team.‘)
for player in players:
print(player.title(),end=",")
#4.4.2 复制列表
my_foods = [‘pizza‘,‘falafel‘,‘carrot cake‘]
friend_foods = my_foods[:]
print("
my favorite food are:")
print(my_foods)
print("my friend‘s favorite food are:")
print(friend_foods)
#这是从my_food里面提取了一个切片
#用切片的方式复制才能
my_foods = [‘pizza‘,‘falafel‘,‘carrot cake‘]
friend_foods = my_foods[:]
my_foods.append(‘cannoli‘)
friend_foods.append(‘ice cream‘)
print("my favorite food are:")
print(my_foods)
print("my friend favorite food are:")
print(friend_foods)
print()
#用赋值的方式得到的效果只是两次都一样
my_foods = [‘pizza‘,‘falafel‘,‘carrot cake‘]
friend_foods = my_foods
#python只是把两个变量关联起来了,而不是向上面一样做成一个副本
my_foods.append(‘cannoli‘)
friend_foods.append(‘ice cream‘)
print("my favorite food are:")
print(my_foods)
print("my friend favorite food are:")
print(friend_foods)
#4-10 切片
fruits = [‘banana‘,‘apple‘,‘pear‘,‘tomato‘,‘watermelon‘]
print("The first there items in the list are:")
print(fruits[:3])
print(fruits[1:4])
print(fruits[-3:])
#4-11 披萨
my_pizzas = [‘durian pizza‘,‘apple pizza‘,‘banana pizza‘]
friend_pizzas = my_pizzas[:]
my_pizzas.append("pear pizza")
friend_pizzas.append("tomato pizza")
print("My favorite pizzas are:")
for pizza in my_pizzas:
print(pizza,end=",")
print()
print("My friend‘s favorite pizzas are:")
for pizza in friend_pizzas:
print(pizza,end=",")
print()
#4.5.1 定义元组
dimensions = (200,50)
print(dimensions[0])
print(dimensions[1])
#修改元组的操作事被禁止的,python指出不能给元组的元素赋值
#dimensions[0] = 250
#4.5.2 遍历元组中的所有值
dimensions = (200,50)
for dimension in dimensions:
print(dimension)
#4.5.3 修改元组变量
dimensions = (200,50)
print("Original dimensions:",end=" ")
for dimension in dimensions:
print(dimension,end = " ")
print()
dimensions = (400,100)
print("modified dimensions:",end=" ")
for dimension in dimensions:
print(dimension,end=" ")
#不能单独改变元组中的元素,但是可以使用元组的变量重新定义
#4-13 自助餐
print()
foods = (‘milk‘,‘bread‘,‘egg‘,‘rice‘,‘apple‘)
for food in foods:
print(food,end=" ")
#foods[0] = ‘banana‘
print()
foods = (‘milk‘,‘bread‘,‘meat‘,‘rice‘,‘banana‘)
for food in foods:
print(food,end=" ")
Python小结
在本章中,学习了:如何高效的处理列表中的元素;如何使用for循环遍历列表,python如何根据缩进来确定程序的结构以及如何避免一些常见的缩进错误;如何创建简单的数字列表,以及可对数字列表执行的一些操作;如何通过切片来使用列表的一部分和复制列表。还学习了元组(它对不应变化的值提供了一定程度的保护),以及在代码变的越来越复杂时如何设置格式,使其易于阅读。
以上是关于每周专业学习周记-1的主要内容,如果未能解决你的问题,请参考以下文章