Open In App

Python Program to Print Christmas Tree Star Pattern

Last Updated : 24 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The “Python program to print Tree pattern” is a piece of code that creates a tree pattern out of asterisks (*) using Python. The indentation and amount of asterisks per row are controlled via loops and string manipulation to produce the tree pattern, which is presented on the console.

Print Christmas Tree Star Pattern using Loops

This program uses nested loops to print out the tree pattern. The outer loop controls the rows of the pattern, and the inner loops control the columns.

The program consists of four sections, each printing a different triangular pattern. Here’s the algorithmic breakdown:

Step 1: Setting the Tree Size

  • num = 3: Defines the base height of each leaf level. Increasing num will make the tree taller and wider.

Step 2: First Level of Leaves

  • Loop (for i in range(1, num+1)): Iterates through each row of the first leaf level. Printing Spaces (” “*(2*num – i + 3)) to centers the stars by printing a specific number of spaces before the stars.
  • Formula: 2*num – i + 3 ensures proper alignment based on the current row.
  • Printing Stars (for j in range(1, i+1)): Prints stars (*) separated by spaces, with the number of stars increasing by one each row.

Step 3: Second Level of Leaves

  • Loop (for i in range(1, num+3)): Adds additional rows to create a fuller second leaf level. Printing Spaces (” “*(2*num – i + 1)) to Adjusts spacing to center the stars for the second level.
  • Formula: 2*num – i + 1 decreases the number of spaces as the row number increases.

Step 4: Third Level of Leaves

  • Loop (for i in range(1, num+5)): Adds more rows for an additional triangular layer, making the tree larger. Printing Spaces (” “*(2*num – i)) which centers the stars by reducing the number of spaces as rows increase.
  • Formula: 2*num – i further decreases spaces for a wider base.

Step 5: Trunk of the Tree

  • Loop (for i in range(1, num+3)): Determines the height of the trunk based on the tree’s size.
  • Printing Spaces (” “*(2*num)): Centers the trunk beneath the leaves by printing a fixed number of spaces.

Example:

Python
# Number of levels for the Christmas tree
num = 3

# First Level of Leaves
for i in range(1, num + 1):
    # Print spaces to center the stars
    print(" " * (2 * num - i + 3), end="")
    # Print stars with a space in between
    for j in range(1, i + 1):
        print("*", end=" ")
    # Move to the next line after printing stars
    print()

# Second Level of Leaves
for i in range(1, num + 3):
    print(" " * (2 * num - i + 1), end="")
    for j in range(-1, i + 1):
        print("*", end=" ")
    print()

# Third Level of Leaves (Optional for larger trees)
for i in range(1, num + 5):
    print(" " * (2 * num - i), end="")
    for j in range(-2, i + 1):
        print("*", end=" ")
    print()

# Trunk of the Tree
for i in range(1, num + 3):
    # Print spaces to center the trunk
    print(" " * (2 * num), end="")
    # Print the trunk (3 stars)
    print("* " * 3)

Output:

xmastree

Tree

Print Christmas Tree Star Pattern using List Comprehension

The code uses list comprehension to generate the string of asterisks for each row of the tree pattern and then joins them with spaces using the Python join() method. The print() function is used to print each row with the appropriate indentation.

Explanation:

  • Initialization and First Level of Leaves: The variable num is set to 3, determining the number of main levels for the Christmas tree. The first for loop runs from 1 to num (1 to 3), printing the initial tier of the tree. For each iteration, it calculates the necessary spaces to center the stars and prints an increasing number of asterisks (*) separated by spaces, forming a triangular shape.
  • Second Level of Leaves: The second for loop extends the tree by running from 1 to num + 2 (1 to 5). This loop adds a wider tier by adjusting the spacing to maintain symmetry and printing more asterisks on each line. Specifically, it starts generating stars from j = -1 to j = i + 1, resulting in i + 2 stars per line, which makes the second level broader than the first.
  • Third Level of Leaves: The third for loop further enhances the tree’s fullness by iterating from 1 to num + 4 (1 to 7). It continues to decrease the leading spaces to keep the tree centered and prints an even larger number of asterisks on each line. By generating stars from j = -2 to j = i + 1, each line in this tier has i + 3 stars, making the third level the widest part of the tree.
  • Trunk of the Tree: The final for loop constructs the trunk by running from 1 to num + 2 (1 to 5). It centers the trunk beneath the foliage by adding consistent spaces and prints three asterisks (* * *) on each line to represent the trunk. Repeating this multiple times gives the trunk an appropriate height, providing a stable base to complete the Christmas tree’s appearance.

Example:

Python
num = 3  # Define the number of levels for the Christmas tree

# First Level of Leaves
for i in range(1, num + 1):
    # Print spaces to center the stars and then print stars separated by spaces
    print(" " * (2 * num - i + 3) + " ".join("*" for j in range(1, i + 1)))

# Second Level of Leaves
for i in range(1, num + 3):

    print(" " * (2 * num - i + 1) + " ".join("*" for j in range(-1, i + 1)))

# Third Level of Leaves
for i in range(1, num + 5):

    print(" " * (2 * num - i) + " ".join("*" for j in range(-2, i + 1)))

# Trunk of the Tree
for i in range(1, num + 3):

    print(" " * (2 * num) + " ".join("*" for j in range(3)))

Output :

christtree

Tree

Time complexity: The time complexity of the given code is O(n^2), where n is the value of the variable ‘num’. This is because the code contains nested loops, each iterating from 1 to num.

Space complexity: The space complexity of the given code is O(1) as it does not use any additional data structures that consume memory.

Python Program to Print Christmas Tree Star Pattern using recursion

To print a Christmas tree star pattern using recursion in Python, we can define a recursive function that handles leaves and trunks of the tree and then call that function recursively to build the entire tree. The star pattern of the Christmas tree will be made up of ‘*’ characters. Here’s a Python program to achieve this:

Python
def leaves(l, maxl, offset, start, end):
    if l > maxl:
        return
    print(" " * (offset - level) + " ".join("*" for _ in range(start, level + end)))
    leaves(l + 1, maxl, offset, start, end)

def trunk(h, offset, w=3):
    if h == 0:
        return
    print(" " * offset + " ".join("*" for _ in range(w)))
    trunk(h - 1, offset, w)

n = 3  # Define the number of levels for the Christmas tree

# Print First Level of Leaves
leaves(1, n, 2 * n + 3, 1, 1)

# Print Second Level of Leaves
leaves(1, n + 3, 2 * n + 1, -1, 1)

# Print Third Level of Leaves
leaves(1, n + 5, 2 * n, -2, 1)

# Print Trunk of the Tree
trunk(n + 3, 2 * n)

Output:

christtree

Tree


Explanation:

  • leaves is a recursive function that prints each level of the tree’s leaves.
    • l: current level of leaves being printed.
    • maxl: maximum level of leaves to be printed.
    • offset: base offset to center the leaves.
    • start: starting value for the range in the join function.
    • end: ending value for the range in the join function.
  • trunk is a recursive function that prints the trunk of the tree.
    • h: height of the trunk.
    • offset: base offset to center the trunk.
    • w: number of stars in the trunk (default is 3).


Next Article

Similar Reads