Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Data Structures - Tree

Data Structures - Tree

A tree is a typical non-linear data structure, composed of nodes and edges connecting them, used to represent the "one-to-many" hierarchical relationships among elements.


Basic Terminology of Trees

Tree Terminology Diagram

A

Root Node
Depth 0

B

Depth 1

C

Depth 1

D

Leaf Node
Depth 2

E

Leaf Node

F

Leaf Node

Root Node

No parent node, unique

Child node

Direct child

Leaf Node

No child node

Depth

Number of edges from root to this node

Height

Number of edges from this node to the deepest leaf

Binary Tree

At most 2 children per node

To understand tree structures you first need to master a few basic terms:

Term Meaning
Root Node (Root) The starting node of the tree, with no parent
Child Node (Child) The next-level node directly connected to a given node
Parent node (Parent) The parent level node directly connected above a given node
Leaf node (Leaf) A node with no children, i.e. the end of the tree
Depth (Depth) Number of edges from the root to this node; the root's depth is 0
Height (Height) Number of edges from this node to the deepest leaf

Binary tree traversal

Four traversal methods of binary trees — illustrated with the same tree

1

2

3

4

5

Preorder (root -> left -> right)

1 2 4 5 3

Inorder (left -> root -> right)

4 2 5 1 3

Postorder (left -> right -> root)

4 5 2 3 1

Level-order (layer by layer)

1 2 3 4 5

The four common traversal methods are as follows:

Traversal method Visit order Typical application
Preorder traversal Root -> left -> right Copying a tree, prefix expression generation
Inorder traversal Left -> root -> right BST outputs a sorted sequence
Postorder traversal Left -> right -> root Deleting a tree, postfix expression generation
Level-order traversal Layer by layer, left to right BFS, printing by layer

Example

#include

#include

struct

TreeNode

{

   

int

data

;

   

struct

TreeNode

left

;

   

struct

TreeNode

right

;

}

;

struct

TreeNode

createNode

(

int

value

)

{

   

struct

TreeNode

node

=

(

struct

TreeNode

)

malloc

(

sizeof

(

struct

TreeNode

)

)

;

    node

->

data

=

value

;

    node

->

left

=

NULL

;

    node

->

right

=

NULL

;

   

return

node

;

}

/* Preorder traversal: root -> left -> right */

void

preOrder

(

struct

TreeNode

root

)

{

   

if

(

root

==

NULL

)

return

;

   

printf

(

"%d "

,

root

->

data

)

;

 

/* Visit the root first */

    preOrder

(

root

->

left

)

;

     

/* Then traverse the left subtree */

    preOrder

(

root

->

right

)

;

     

/* Finally traverse the right subtree */

}

/* Inorder traversal: left -> root -> right */

void

inOrder

(

struct

TreeNode

root

)

{

   

if

(

root

==

NULL

)

return

;

    inOrder

(

root

->

left

)

;

       

/* Traverse the left subtree first */

   

printf

(

"%d "

,

root

->

data

)

;

 

/* Then visit the root */

    inOrder

(

root

->

right

)

;

     

/* Finally traverse the right subtree */

}

/* Postorder traversal: left -> right -> root */

void

postOrder

(

struct

TreeNode

root

)

{

   

if

(

root

==

NULL

)

return

;

    postOrder

(

root

->

left

)

;

     

/* Traverse the left subtree first */

    postOrder

(

root

->

right

)

;

   

/* Then traverse the right subtree */

   

printf

(

"%d "

,

root

->

data

)

;

 

/* Finally visit the root */

}

int

main

(

)

{

   

/* Build the binary tree below:
           1
          / \
         2   3
        / \
       4   5   */

   

struct

TreeNode

root

=

createNode

(

1

)

;

    root

->

left

=

createNode

(

2

)

;

    root

->

right

=

createNode

(

3

)

;

    root

->

left

->

left

=

createNode

(

4

)

;

    root

->

left

->

right

=

createNode

(

5

)

;

   

printf

(

"Preorder: "

)

;

preOrder

(

root

)

;

 

printf

(

"\n"

)

;

 

/* 1 2 4 5 3 */

   

printf

(

"Inorder: "

)

;

inOrder

(

root

)

;

 

printf

(

"\n"

)

;

 

/* 4 2 5 1 3 */

   

printf

(

"Postorder: "

)

;

postOrder

(

root

)

;

printf

(

"\n"

)

;

 

/* 4 5 2 3 1 */

   

return

0

;

}


Binary Search Tree (BST)

Binary Search Tree (BST) — left smaller, right larger

50

30

70

20

40

60

80

BST properties and operations

Property: all values in the left subtree < Root < all values in the right subtree

Insert/search: go left if smaller, right if larger; O(log n) when balanced

Inorder traversal outputs an ascending sequence: 20 30 40 50 60 70 80

Note: inserting in order 1,2,3... degrades into a linked list O(n); AVL trees solve this

A Binary Search Tree (BST) requires that for every node, all values in its left subtree are smaller, and all in its right subtree are larger.

Example

#include

#include

struct

TreeNode

{

   

int

data

;

   

struct

TreeNode

left

;

   

struct

TreeNode

right

;

}

;

/* BST insert: recursively locate the proper position based on value magnitude
   Time complexity: O(h), where h is tree height; O(log n) when balanced */

struct

TreeNode

insert

(

struct

TreeNode

root

,

int

value

)

{

   

if

(

root

==

NULL

)

{

       

struct

TreeNode

node

=

(

struct

TreeNode

)

malloc

(

sizeof

(

struct

TreeNode

)

)

;

        node

->

data

=

value

;

        node

->

left

=

node

->

right

=

NULL

;

       

return

node

;

   

}

   

if

(

value

<

root

->

data

)

{

root

->

left

=

insert

(

root

->

left

,

value

)

;

 

/* Smaller than root, insert into left subtree */

   

}

else

if

(

value

root

->

data

)

{

        root

->

right

=

insert

(

root

->

right

,

value

)

;

/* Larger than root, insert into right subtree */

   

}

   

return

root

;

 

/* If equal, ignore (BSTs usually do not store duplicates) */

}

/* BST search: compare with the current node to decide to search left or right */

struct

TreeNode

search

(

struct

TreeNode

root

,

int

target

)

{

   

if

(

root

==

NULL

||

root

->

data

==

target

)

{

       

return

root

;

 

/* Found the target or reached a leaf node */

   

}

   

if

(

target

<

root

->

data

)

{

return

search

(

root

->

left

,

target

)

;

 

/* Target is smaller, go to the left subtree */

   

}

   

return

search

(

root

->

right

,

target

)

;

     

/* Target is larger, go to the right subtree */

}

/* Inorder traversal (verify the BST's ordering) */

void

inOrder

(

struct

TreeNode

root

)

{

   

if

(

root

==

NULL

)

return

;

    inOrder

(

root

->

left

)

;

   

printf

(

"%d "

,

root

->

data

)

;

    inOrder

(

root

->

right

)

;

}

int

main

(

)

{

   

struct

TreeNode

root

=

NULL

;

   

int

values

[

]

=

{

50

,

30

,

70

,

20

,

40

,

60

,

80

}

;

   

/* Insert sequentially to build the BST */

   

for

(

int

i

=

0

;

i

<

7

;

i

++

)

{

        root

=

insert

(

root

,

values

[

i

]

)

;

   

}

   

printf

(

"BST inorder traversal (ascending): "

)

;

    inOrder

(

root

)

;

 

/* Output: 20 30 40 50 60 70 80 */

   

printf

(

"\n"

)

;

   

/* Search test */

   

int

target

=

40

;

   

struct

TreeNode

found

=

search

(

root

,

target

)

;

   

printf

(

"%d %s\n"

,

target

,

found

?

"Found it!"

:

"Not found"

)

;

   

/* Output: 40 Found it! */

   

return

0

;

}

Under ideal conditions (a balanced tree), BST insert, delete, and search all reach O(log n). But if the data is ordered (e.g. inserted as 1, 2, 3, 4...), the BST degrades into a linked list with efficiency dropping to O(n). To solve this, computer science developed AVL treeand other self-balancing binary search trees.