📜  二叉树 |第 1 套(介绍)

📅  最后修改于: 2022-05-13 01:57:17.767000             🧑  作者: Mango

二叉树 |第 1 套(介绍)

树:与线性数据结构的数组、链表、堆栈和队列不同,树是分层数据结构。
树词汇:最顶层的节点称为树的根。直接位于元素之下的元素称为其子元素。直接位于某物上方的元素称为其父元素。例如,“a”是“f”的子代,“f”是“a”的父代。最后,没有子元素的元素称为叶子。

tree
      ----
       j    <-- root
     /   \
    f      k  
  /   \      \
 a     h      z    <-- leaves

为什么是树?
1.使用树的一个原因可能是因为您想要存储自然形成层次结构的信息。例如计算机上的文件系统:

file system
-----------
     /    <-- root
  /      \
...       home
      /          \
   ugrad        course
    /       /      |     \
  ...      cs101  cs112  cs113

2.树(有一些排序,例如 BST)提供适度的访问/搜索(比链表快,比数组慢)。
3.树提供适度的插入/删除(比数组快,比无序链表慢)。
4.与链表一样,与数组不同,树对节点的数量没有上限,因为节点是使用指针链接的。
树的主要应用包括:
1.操作分层数据。
2.使信息易于搜索(参见树遍历)。
3.操作排序的数据列表。
4.作为合成数字图像以获得视觉效果的工作流程。
5.路由器算法
6.多阶段决策形式(见商棋)。
二叉树:元素最多有2个孩子的树称为二叉树。由于二叉树中的每个元素只能有 2 个孩子,我们通常将它们命名为左孩子和右孩子。
C 中的二叉树表示:一棵树由指向树中最顶层节点的指针表示。如果树为空,则根的值为 NULL。
树节点包含以下部分。
1. 数据
2. 指向左孩子的指针
3. 指向右孩子的指针
在 C 中,我们可以使用结构来表示树节点。下面是具有整数数据的树节点的示例。

C++
struct node
{
    int data;
    struct node* left;
    struct node* right;
};


Python
# A Python class that represents an individual node
# in a Binary Tree
class Node:
    def __init__(self,key):
        self.left = None
        self.right = None
        self.val = key


Java
/* Class containing left and right child of current
   node and key value*/
class Node
{
    int key;
    Node left, right;
 
    public Node(int item)
    {
        key = item;
        left = right = null;
    }
}


C#
/* Class containing left and right child
of current node and key value*/
class Node
{
    int key;
    Node left, right;
 
    public Node(int item)
    {
        key = item;
        left = right = null;
    }
}


Javascript


C++
#include 
using namespace std;
 
struct Node {
    int data;
    struct Node* left;
    struct Node* right;
 
    // val is the key or the value that
    // has to be added to the data part
    Node(int val)
    {
        data = val;
 
        // Left and right child for node
        // will be initialized to null
        left = NULL;
        right = NULL;
    }
};
 
int main()
{
 
    /*create root*/
    struct Node* root = new Node(1);
    /* following is the tree after above statement
 
             1
            / \
          NULL NULL
    */
 
    root->left = new Node(2);
    root->right = new Node(3);
    /* 2 and 3 become left and right children of 1
                    1
                  /    \
                 2       3
               /  \     /  \
            NULL NULL  NULL NULL
    */
 
    root->left->left = new Node(4);
    /* 4 becomes left child of 2
               1
            /     \
           2       3
          / \     / \
         4  NULL NULL NULL
        / \
     NULL NULL
    */
 
    return 0;
}


C
#include 
#include 
struct node {
    int data;
    struct node* left;
    struct node* right;
};
 
/* newNode() allocates a new node
with the given data and NULL left
and right pointers. */
struct node* newNode(int data)
{
    // Allocate memory for new node
    struct node* node
        = (struct node*)malloc(sizeof(struct node));
 
    // Assign data to this node
    node->data = data;
 
    // Initialize left and
    // right children as NULL
    node->left = NULL;
    node->right = NULL;
    return (node);
}
 
int main()
{
    /*create root*/
    struct node* root = newNode(1);
    /* following is the tree after above statement
         1
        / \
      NULL NULL
    */
 
    root->left = newNode(2);
    root->right = newNode(3);
    /* 2 and 3 become left and right children of 1
            1
         /    \
        2      3
      /  \    /  \
   NULL NULL NULL NULL
    */
 
    root->left->left = newNode(4);
    /* 4 becomes left child of 2
             1
         /    \
        2      3
      /  \    /  \
     4 NULL NULL NULL
    / \
 NULL NULL
    */
 
    getchar();
    return 0;
}


Java
/* Class containing left and right child of current
   node and key value*/
class Node
{
    int key;
    Node left, right;
 
    public Node(int item)
    {
        key = item;
        left = right = null;
    }
}
 
// A Java program to introduce Binary Tree
class BinaryTree
{
    // Root of Binary Tree
    Node root;
 
    // Constructors
    BinaryTree(int key)
    {
        root = new Node(key);
    }
 
    BinaryTree()
    {
        root = null;
    }
 
    public static void main(String[] args)
    {
        BinaryTree tree = new BinaryTree();
 
        /*create root*/
        tree.root = new Node(1);
 
        /* following is the tree after above statement
 
              1
            /   \
          null  null     */
 
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
 
        /* 2 and 3 become left and right children of 1
               1
            /     \
          2        3
        /   \     /  \
      null null null null  */
 
 
        tree.root.left.left = new Node(4);
        /* 4 becomes left child of 2
                    1
                /       \
               2          3
             /   \       /  \
            4    null  null  null
           /   \
          null null
         */
    }
}


Python
# Python program to introduce Binary Tree
 
# A class that represents an individual node in a
# Binary Tree
class Node:
    def __init__(self,key):
        self.left = None
        self.right = None
        self.val = key
 
 
# create root
root = Node(1)
''' following is the tree after above statement
        1
      /   \
     None  None'''
 
root.left      = Node(2);
root.right     = Node(3);
   
''' 2 and 3 become left and right children of 1
           1
         /   \
        2      3
     /    \    /  \
   None None None None'''
 
 
root.left.left  = Node(4);
'''4 becomes left child of 2
           1
       /       \
      2          3
    /   \       /  \
   4    None  None  None
  /  \
None None'''


C#
// A C# program to introduce Binary Tree
using System;
 
/* Class containing left and right child
of current node and key value*/
public class Node
{
    public int key;
    public Node left, right;
 
    public Node(int item)
    {
        key = item;
        left = right = null;
    }
}
 
public class BinaryTree
{
    // Root of Binary Tree
    Node root;
 
    // Constructors
    BinaryTree(int key)
    {
        root = new Node(key);
    }
 
    BinaryTree()
    {
        root = null;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        BinaryTree tree = new BinaryTree();
 
        /*create root*/
        tree.root = new Node(1);
 
        /* following is the tree after above statement
 
             1
            / \
         null null     */
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
 
        /* 2 and 3 become left and right children of 1
                1
             /     \
           2        3
         /  \     /   \
       null null null null */
        tree.root.left.left = new Node(4);
         
        /* 4 becomes left child of 2
                1
             /     \
           2        3
         /  \     /   \
         4 null null null
        / \
     null null
        */
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


C语言中的第一个简单树
让我们在 C 中创建一个具有 4 个节点的简单树。创建的树如下所示。

tree
      ----
       1    <-- root
     /   \
    2     3  
   /   
  4

C++

#include 
using namespace std;
 
struct Node {
    int data;
    struct Node* left;
    struct Node* right;
 
    // val is the key or the value that
    // has to be added to the data part
    Node(int val)
    {
        data = val;
 
        // Left and right child for node
        // will be initialized to null
        left = NULL;
        right = NULL;
    }
};
 
int main()
{
 
    /*create root*/
    struct Node* root = new Node(1);
    /* following is the tree after above statement
 
             1
            / \
          NULL NULL
    */
 
    root->left = new Node(2);
    root->right = new Node(3);
    /* 2 and 3 become left and right children of 1
                    1
                  /    \
                 2       3
               /  \     /  \
            NULL NULL  NULL NULL
    */
 
    root->left->left = new Node(4);
    /* 4 becomes left child of 2
               1
            /     \
           2       3
          / \     / \
         4  NULL NULL NULL
        / \
     NULL NULL
    */
 
    return 0;
}

C

#include 
#include 
struct node {
    int data;
    struct node* left;
    struct node* right;
};
 
/* newNode() allocates a new node
with the given data and NULL left
and right pointers. */
struct node* newNode(int data)
{
    // Allocate memory for new node
    struct node* node
        = (struct node*)malloc(sizeof(struct node));
 
    // Assign data to this node
    node->data = data;
 
    // Initialize left and
    // right children as NULL
    node->left = NULL;
    node->right = NULL;
    return (node);
}
 
int main()
{
    /*create root*/
    struct node* root = newNode(1);
    /* following is the tree after above statement
         1
        / \
      NULL NULL
    */
 
    root->left = newNode(2);
    root->right = newNode(3);
    /* 2 and 3 become left and right children of 1
            1
         /    \
        2      3
      /  \    /  \
   NULL NULL NULL NULL
    */
 
    root->left->left = newNode(4);
    /* 4 becomes left child of 2
             1
         /    \
        2      3
      /  \    /  \
     4 NULL NULL NULL
    / \
 NULL NULL
    */
 
    getchar();
    return 0;
}

Java

/* Class containing left and right child of current
   node and key value*/
class Node
{
    int key;
    Node left, right;
 
    public Node(int item)
    {
        key = item;
        left = right = null;
    }
}
 
// A Java program to introduce Binary Tree
class BinaryTree
{
    // Root of Binary Tree
    Node root;
 
    // Constructors
    BinaryTree(int key)
    {
        root = new Node(key);
    }
 
    BinaryTree()
    {
        root = null;
    }
 
    public static void main(String[] args)
    {
        BinaryTree tree = new BinaryTree();
 
        /*create root*/
        tree.root = new Node(1);
 
        /* following is the tree after above statement
 
              1
            /   \
          null  null     */
 
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
 
        /* 2 and 3 become left and right children of 1
               1
            /     \
          2        3
        /   \     /  \
      null null null null  */
 
 
        tree.root.left.left = new Node(4);
        /* 4 becomes left child of 2
                    1
                /       \
               2          3
             /   \       /  \
            4    null  null  null
           /   \
          null null
         */
    }
}

Python

# Python program to introduce Binary Tree
 
# A class that represents an individual node in a
# Binary Tree
class Node:
    def __init__(self,key):
        self.left = None
        self.right = None
        self.val = key
 
 
# create root
root = Node(1)
''' following is the tree after above statement
        1
      /   \
     None  None'''
 
root.left      = Node(2);
root.right     = Node(3);
   
''' 2 and 3 become left and right children of 1
           1
         /   \
        2      3
     /    \    /  \
   None None None None'''
 
 
root.left.left  = Node(4);
'''4 becomes left child of 2
           1
       /       \
      2          3
    /   \       /  \
   4    None  None  None
  /  \
None None'''

C#

// A C# program to introduce Binary Tree
using System;
 
/* Class containing left and right child
of current node and key value*/
public class Node
{
    public int key;
    public Node left, right;
 
    public Node(int item)
    {
        key = item;
        left = right = null;
    }
}
 
public class BinaryTree
{
    // Root of Binary Tree
    Node root;
 
    // Constructors
    BinaryTree(int key)
    {
        root = new Node(key);
    }
 
    BinaryTree()
    {
        root = null;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        BinaryTree tree = new BinaryTree();
 
        /*create root*/
        tree.root = new Node(1);
 
        /* following is the tree after above statement
 
             1
            / \
         null null     */
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
 
        /* 2 and 3 become left and right children of 1
                1
             /     \
           2        3
         /  \     /   \
       null null null null */
        tree.root.left.left = new Node(4);
         
        /* 4 becomes left child of 2
                1
             /     \
           2        3
         /  \     /   \
         4 null null null
        / \
     null null
        */
    }
}
 
// This code is contributed by PrinciRaj1992

Javascript