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.