# 创建二叉树/遍历

```python
class Node(object):
    def __init__(self,data,left,right):
        self.data=data
        self.left=left
        self.right=right

class Tree(object):
    def __init__(self,data):
        self.root=Node(data,None,None)
        self.size=1
        self.i=0

    def getsize(self):
        stack=[self.root]
        self.size=0
        while stack:
            temp=stack.pop(0)
            self.size+=1
            if temp.left:
                stack.append(temp.left)
            if temp.right:
                stack.append(temp.right)
        return self.size

    def print(self):
        stack=[self.root]
        while stack:
            temp=stack.pop(0)
            print(str(temp.data)+'\t',end='\t')
            if temp.left:
                stack.append(temp.left)
            if temp.right:
                stack.append(temp.right)

    def qianxuDG(self,root):
        # 递归，前序遍历
        if root:
            print(root.data,end=',')
            self.qianxuDG(root.left)
            self.qianxuDG(root.right)

    def zhongxuDG(self,root):
        # 递归，中序遍历
        if root:
            self.qianxuDG(root.left)
            print(root.data)
            self.qianxuDG(root.right)

    def houxuDG(self,root):
        # 递归，后序遍历
        if root:
            self.qianxuDG(root.left)
            self.qianxuDG(root.right)
            print(root.data)

    def height(self,root):
        if not root:
            return 0
        ldeepth=self.height(root.left)
        rdeepth=self.height(right)
        return max(ldeepth+1,rdeepth+1)

    def qianxu(self):
        # 前序，非递归
        if self.root is None:
            return
        else:
            stack=[self.root]
            while stack:
                current=stack.pop()
                print(current.data,end=',')
                if current.right:
                    stack.append(current.right)
                if current.left:
                    stack.append(current.left)
    def zhongxu(self):
        # 中序，非递归
        if self.root is None:
            return
        else:
            stack=[]
            current=self.root
            while len(stack)!=0 or current:

                if current:
                    stack.append(current)
                    current=current.left
                else:
                    temp=stack.pop()
                    print(temp.data)
                    current=current.right

    def houxu(self):
        if self.root is None:
            return
        else:
            stack1=[]
            stack2=[]
            stack1.append(self.root)
            while stack1:
                current=stack1.pop()
                stack2.append(current)
                if current.left:
                    stack1.append(current.left)

                if current.right:
                    stack1.append(current.right)
            while stack2:
                print(stack2.pop().data)
if __name__ == '__main__':
    print('二叉树创建：')
    tree=Tree(1)
    tree.root.left=Node(2,None,None)
    tree.root.right=Node(3,None,None)
    tree.root.left.left=Node(4,None,None)
    tree.root.left.right=Node(5,None,None)
    tree.root.right.left=Node(6,None,None)
    tree.root.right.right=Node(7,None,None)
    tree.root.left.left.left=Node(8,None,None)
    tree.root.left.left.right=Node(9,None,None)
    tree.root.left.right.left=Node(10,None,None)
    tree.root.left.right.right=Node(11,None,None)

    print('-'*50)
    tree.print()
    print()
    print('-'*50)
    tree.qianxuDG(tree.root)
    print()
    print('-'*50)
    tree.qianxu()
```

```
二叉树创建：
--------------------------------------------------
1        2        3        4        5        6        7        8        9        10        11        
--------------------------------------------------
1,2,4,8,9,5,10,11,3,6,7,
--------------------------------------------------
1,2,4,8,9,5,10,11,3,6,7,
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/create-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
