How do you implement a binary tree using a linked list?

Construct Complete Binary Tree from its Linked List Representation

Given Linked List Representation of Complete Binary Tree, construct the Binary tree. A complete binary tree can be represented in an array in the following approach.
If root node is stored at index i, its left, and right children are stored at indices 2*i+1, 2*i+2 respectively.
Suppose tree is represented by a linked list in same way, how do we convert this into normal linked representation of binary tree where every node has data, left and right pointers? In the linked list representation, we cannot directly access the children of the current node unless we traverse the list.

How do you implement a binary tree using a linked list?

C++


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

#include <iostream>

#include <queue>

using namespace std;

// A Binary Tree Node

struct TreeNode

{

int data;

TreeNode *left, *right;

TreeNode(int data)

{

this->data = data;

this->left = this->right = nullptr;

}

};

// A Linked List Node

struct ListNode

{

int data;

ListNode* next;

ListNode(int data)

{

this->data = data;

this->next = nullptr;

}

};

// Utility function to create a new node with the given data and

// pushes it onto the list's front

ListNode* push(ListNode* head, int data)

{

ListNode* node = new ListNode(data);

node->next = head;

head = node;

return head;

}

// Function to perform preorder traversal on a given binary tree.

void preorder(TreeNode* root)

{

if (root == nullptr) {

return;

}

cout << root->data << ' ';

preorder(root->left);

preorder(root->right);

}

// Function to construct a complete binary tree from a given linked list

TreeNode* convertListToBinaryTree(ListNode* head)

{

// base case

if (head == nullptr) {

return nullptr;

}

// create the root using the first node of the linked list

TreeNode* root = new TreeNode(head->data);

// move the head pointer to the next node

head = head->next;

// create a queue to store tree pointers and enqueue the root node

queue<TreeNode*> q;

q.push(root);

// loop till the end of the linked list is reached

while (head)

{

// dequeue front node

TreeNode* front = q.front();

q.pop();

// create a left child from the next node in the linked list and enqueue it

front->left = new TreeNode(head->data);

q.push(front->left);

// move the head pointer to the next node

head = head->next;

// if the linked list did not reach its end

if (head != nullptr)

{

// create the right child from the next node in the linked list and

// enqueue it

front->right = new TreeNode(head->data);

q.push(front->right);

// move the head pointer to the next node

head = head->next;

}

}

// return root of the constructed binary tree

return root;

}

int main()

{

ListNode* head = nullptr;

int n = 6;

// construct a linked list

for (int i = n; i > 0; i--) {

head = push(head, i);

}

TreeNode* root = convertListToBinaryTree(head);

preorder(root);

return 0;

}

DownloadRun Code

Output:

1 2 4 5 3 6