Create a binary tree in Python exploration, and explore a binary tree in python

Source: Internet
Author: User

Create a binary tree in Python exploration, and explore a binary tree in python

Problem

Create a binary tree

A set of multiple nodes in a binary tree. This set may be:

Empty set

It is composed of a root node and a binary tree that does not conflict with each other and is called the left subtree and the right subtree respectively.

Create a binary tree, create nodes, and then create relationships between nodes

Python sample code

#! /Usr/bin/env python #-*-encoding: UTF-8-*-# author: LiYanwei # version: 0.1 class TreeNode (object): def _ init _ (self, data, left = None, right = None): self. data = data self. left = left self. right = right def _ str _ (self): return str (self. data) A = TreeNode ('A') B = TreeNode ('B') C = TreeNode ('C') D = TreeNode ('D'). left = BA. right = CB. right = Dprint B. right

Summary

The above is all about how to create a binary tree in Python exploration. I hope it will be helpful to you. If you are interested, please refer to this site: how to modify the Python search path, the differences between copy and deepcopy in Python, and the details of the python implementation interface (itchat, you are welcome to leave a message for discussion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.