turtle二叉树

import turtle as t
def tree(deep,long,size):#画一个二叉树:deep深度 long第一根树干的长度  size第一根树干的粗细
    if deep == 0:
        return
    elif deep == 1:
        t.pencolor('green')
    else:
        t.pencolor('brown')
    t.pensize(size)
    t.down()
    t.forward(long)
    t.right(30)
    #画右子树
    tree(deep-1,long*0.75,size*0.75)
    t.left(60)
    #画左子树
    tree(deep-1,long*0.75,size*0.75)
    #回去
    t.right(30)#以上转的度数:左转的度数之和等于右转的度数之和。30+30=60
    t.bk(long)
    t.up()
t.speed(9)
t.setup(800,600,0,0)
t.up()
t.goto(0,-200)
t.setheading(90)
tree(4,100,10)

1.本身是一个有序树——’树‘字相当于’数‘字
2.树中包含的各个节点的度不能超过2(也就是分叉),即只能是0、1或2.
3.满二叉树:除了最后一层的节点没有任何节点外,每层上的所有节点都有两个节点。
4.完全二叉树:深度为h,除了第h层外,其他各层的节点都有两个节点,且第h层的所有节点都集中在最左边
5.满二叉树一定是完全二叉树,但完全二叉树不一定是满二叉树。
6.子数:左子树、右子树
    1.叶子节点:也称为终端节点,没有子树的节点或度为零的节点。


7.节点:2.分支节点:也称为非终端节点,度不为零的节点成为非终端节点。


    3.根节点:第一个节点

    A.节点的度:一个节点所拥有子树数目称为节点的度

    
8.度:    B.树的度:树中所有的节点的度的最大值


    C.宽度:每层最多的节点数

    (1)节点的层次:从根节点开始,假设根节点为第一层,根节点的子节点为2层,以此类推,如果某一个节点位于第x层,则其子节点位于x+1层。
9.层次:

    (2)树的深度:也就是树的高度。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python中的turtle来可视化二叉树的中序遍历。具体实现步骤如下: 1. 首先,导入turtle和random,并定义一个Node类表示二叉树的节点。 ```python import turtle import random class Node: def __init__(self, value): self.left = None self.right = None self.value = value ``` 2. 定义一个函数draw_node,用于在窗口上绘制一个二叉树节点。该函数接受四个参数:x、y表示节点的坐标,size表示节点的大小,color表示节点的颜色。 ```python def draw_node(x, y, size, color): turtle.penup() turtle.goto(x, y) turtle.pendown() turtle.fillcolor(color) turtle.begin_fill() turtle.circle(size) turtle.end_fill() turtle.penup() turtle.goto(x, y - size * 1.2) turtle.write(str(value), align="center", font=("Arial", 12, "normal")) ``` 3. 定义一个函数inorder,用于进行二叉树的中序遍历,并在遍历的过程中绘制节点。该函数接受两个参数:root表示二叉树的根节点,x表示根节点的横坐标。 ```python def inorder(root, x): if root is not None: inorder(root.left, x - 50) draw_node(x, 0, 20, "blue") turtle.penup() turtle.goto(x, 50) turtle.pendown() turtle.goto(x, 20) turtle.penup() turtle.goto(x - 20, -20) turtle.write(str(root.value), align="center", font=("Arial", 12, "normal")) inorder(root.right, x + 50) ``` 4. 最后,生成一个随机的二叉树,并调用inorder函数进行中序遍历。设置turtle窗口的大小,并隐藏turtle的箭头。 ```python root = Node(random.randint(0, 100)) for i in range(20): node = Node(random.randint(0, 100)) current = root while True: if node.value < current.value: if current.left is None: current.left = node break else: current = current.left else: if current.right is None: current.right = node break else: current = current.right turtle.setup(width=800, height=600) turtle.hideturtle() inorder(root, 400) ``` 完整代码如下: ```python import turtle import random class Node: def __init__(self, value): self.left = None self.right = None self.value = value def draw_node(x, y, size, color): turtle.penup() turtle.goto(x, y) turtle.pendown() turtle.fillcolor(color) turtle.begin_fill() turtle.circle(size) turtle.end_fill() turtle.penup() turtle.goto(x, y - size * 1.2) turtle.write(str(value), align="center", font=("Arial", 12, "normal")) def inorder(root, x): if root is not None: inorder(root.left, x - 50) draw_node(x, 0, 20, "blue") turtle.penup() turtle.goto(x, 50) turtle.pendown() turtle.goto(x, 20) turtle.penup() turtle.goto(x - 20, -20) turtle.write(str(root.value), align="center", font=("Arial", 12, "normal")) inorder(root.right, x + 50) root = Node(random.randint(0, 100)) for i in range(20): node = Node(random.randint(0, 100)) current = root while True: if node.value < current.value: if current.left is None: current.left = node break else: current = current.left else: if current.right is None: current.right = node break else: current = current.right turtle.setup(width=800, height=600) turtle.hideturtle() inorder(root, 400) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值