algoritmauzmanı
Sign in
TREN
6.3Binary Tree with Linked Nodes
Lesson 6.3.1

A tree of nodes: left and right

In a tree in an array a formula told you where a child is. With linked nodes there is no formula: every keeps the addresses of its children itself. The single next field of a linked list is replaced here by two fields, left and right. In this lesson you see the node's layout and build by hand the sample tree used in every lesson of this subtopic.

What you will learn
  • The three fields of struct TreeNode and why a missing child is NULL
  • How a new node is allocated and prepared
  • Why you always enter a tree at its
6 minTime O(1)Memory O(1)C · Java · Python

1Idea

A binary tree node carries a value and two . left points to the left child and right to the right child. The pointer on a side with no child is . A node whose two pointers are both NULL is a . Compare it with a linked list: a list node points to the next one with a single next; a tree node branches downward with two pointers.

newNode allocates room for one node, writes the value and sets both children to NULL. Setting them to NULL is a must: memory from in C holds random values, and those would be read as a child's address. In Java and Python the fields start as null and None on their own.

The whole tree is kept through one pointer: the address of the node. Every operation starts at the root and goes down through left and right. In an empty tree the root is NULL. Lose the root and you lose the whole tree; in C that means no node can ever be freed.

The sample tree is the same in every lesson of this subtopic: root 10, its children 20 and 30, the children of 20 are 40 and 50, and the left child of 30 is 60. You also get this tree by adding 10, 20, 30, 40, 50, 60 level by level; the insertion lesson does that in code. The same tree is used in the array lessons, so you can read the two structures side by side.

To keep it short, sampleTree does not check whether newNode returned NULL. In a real program you should check after every allocation.

2Step by step

The drawing builds the sample tree with sampleTree, one step at a time. The fields on either side of a node are left and right; a slash means that child is NULL. The text under a node is its address.

100x2a0200x2c8300x2f0400x318500x340600x368root
  1. newNode(10) creates the root. Both children are NULL. The root pointer points to this node.
  2. root->left = newNode(20): the new node's address is written into the root's left field. 20 is now the root's left child.
  3. root->right = newNode(30): 30 is now the root's right child. Both of the root's fields are in use.
  4. root->left->left: first go left from the root to 20, then link 40 into its left field.
  5. Along the same path, 50 is linked into the right field of 20. 20 now has two children.
  6. Go right from the root to 30 and link 60 into its left field. The right side of 30 stays NULL.
  7. The tree is ready: six nodes and three leaves (40, 50, 60). The function returns root; from now on you always enter the tree at this address.

struct TreeNode {    int data;    struct TreeNode* left;   // left child    struct TreeNode* right;  // right child}; struct TreeNode* newNode(int value) {    struct TreeNode* n = malloc(sizeof(struct TreeNode));    if (n == NULL) return NULL;  // out of memory    n->data = value;    n->left = NULL;              // no children yet    n->right = NULL;    return n;} struct TreeNode* sampleTree(void) {    struct TreeNode* root = newNode(10);    root->left = newNode(20);    root->right = newNode(30);    root->left->left = newNode(40);    root->left->right = newNode(50);    root->right->left = newNode(60);    return root;}
class TreeNode {    int data;    TreeNode left, right;  // children, null at first    TreeNode(int data) { this.data = data; }} TreeNode sampleTree() {    TreeNode root = new TreeNode(10);    root.left = new TreeNode(20);    root.right = new TreeNode(30);    root.left.left = new TreeNode(40);    root.left.right = new TreeNode(50);    root.right.left = new TreeNode(60);    return root;}
class TreeNode:    def __init__(self, data):        self.data = data        self.left = None   # left child        self.right = None  # right child def sample_tree():    root = TreeNode(10)    root.left = TreeNode(20)    root.right = TreeNode(30)    root.left.left = TreeNode(40)    root.left.right = TreeNode(50)    root.right.left = TreeNode(60)    return root

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1)

Allocating a node and setting its two fields to NULL takes constant time. Building the tree by hand is that much work per node.

Memory
O(1)

Each node takes room for one value and two pointers. The tree needs no extra array and has no empty cells.

On a 64-bit system an int and two pointers take 24 bytes with alignment. In an array tree a node takes only the room of its value, but empty cells take room too.

4Common mistake

In C the most common mistake is not setting the new node's children to NULL:

struct TreeNode* n = malloc(sizeof(struct TreeNode));n->data = value;return n;   // left and right hold random values

The first function that walks this node takes the random value in left for an address and goes there. The program either crashes or prints nonsense. Another mistake is assigning a new node to the variable that holds the root and losing the old root: after root = newNode(20), 10 and everything below it can no longer be reached.

5Try it

In the sample tree, what is in the right field of node 30?

Show the answer

The answer is B. 60 is the left child of 30. Node 30 has no right child, so its right field is NULL.

Which piece of information is enough to reach a tree kept with linked nodes?

Show the answer

The answer is A. Every node keeps its children's addresses, so starting at the root you reach every node through left and right.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.