用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(一)
2013-02-22 01:26
274 查看
用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(二)
用二叉链表实现完全二叉树 (Linked Complete Binary Tree)
通常情况下,我们是用数组来实现完全二叉树的。如果parent node的下标是 i,那么左孩子、右孩子的下标分别是 2*i+1、2*i+2。但是这篇文章将介绍如何用二叉链表实现完全二叉树。
However, it may be an interesting programming question to created a Complete Binary Tree using linked representation. Here Linked mean a non-array representation where left and right pointers(or references) are used to refer left and right children respectively.
How to write an insert function that always adds a new node in the last level and at the leftmost available position?
To create a linked complete binary tree, we need to keep track of the nodes in a level order fashion such that the next node to be inserted lies in the leftmost position. A queue data structure can be used to keep track of the inserted nodes.
Following are steps to insert a new node in Complete Binary Tree.
1. If the tree is empty, initialize the root with new node.
2. Else, get the front node of the queue.
…….If the left child of this front node doesn’t exist, set the left child as the new node.
…….else if the right child of this front node doesn’t exist, set the right child as the new node.
3. If the front node has both the left child and right child, Dequeue() it.
4. Enqueue() the new node.
Below is the implementation:
Output:
用二叉链表实现完全二叉树 (Linked Complete Binary Tree)
通常情况下,我们是用数组来实现完全二叉树的。如果parent node的下标是 i,那么左孩子、右孩子的下标分别是 2*i+1、2*i+2。但是这篇文章将介绍如何用二叉链表实现完全二叉树。
However, it may be an interesting programming question to created a Complete Binary Tree using linked representation. Here Linked mean a non-array representation where left and right pointers(or references) are used to refer left and right children respectively.
How to write an insert function that always adds a new node in the last level and at the leftmost available position?
To create a linked complete binary tree, we need to keep track of the nodes in a level order fashion such that the next node to be inserted lies in the leftmost position. A queue data structure can be used to keep track of the inserted nodes.
Following are steps to insert a new node in Complete Binary Tree.
1. If the tree is empty, initialize the root with new node.
2. Else, get the front node of the queue.
…….If the left child of this front node doesn’t exist, set the left child as the new node.
…….else if the right child of this front node doesn’t exist, set the right child as the new node.
3. If the front node has both the left child and right child, Dequeue() it.
4. Enqueue() the new node.
Below is the implementation:
// Program for linked implementation of complete binary tree #include <stdio.h> #include <stdlib.h> // For Queue Size #define SIZE 50 // A tree node struct node { int data; struct node *right,*left; }; // A queue node struct Queue { int front, rear; int size; struct node* *array; }; // A utility function to create a new tree node struct node* newNode(int data) { struct node* temp = (struct node*) malloc(sizeof( struct node )); temp->data = data; temp->left = temp->right = NULL; return temp; } // A utility function to create a new Queue struct Queue* createQueue(int size) { struct Queue* queue = (struct Queue*) malloc(sizeof( struct Queue )); queue->front = queue->rear = -1; queue->size = size; queue->array = (struct node**) malloc(queue->size * sizeof( struct node* )); int i; for (i = 0; i < size; ++i) queue->array[i] = NULL; return queue; } // Standard Queue Functions int isEmpty(struct Queue* queue) { return queue->front == -1; } int isFull(struct Queue* queue) { return queue->rear == queue->size - 1; } int hasOnlyOneItem(struct Queue* queue) { return queue->front == queue->rear; } void Enqueue(struct node *root, struct Queue* queue) { if (isFull(queue)) return; queue->array[++queue->rear] = root; if (isEmpty(queue)) ++queue->front; } struct node* Dequeue(struct Queue* queue) { if (isEmpty(queue)) return NULL; struct node* temp = queue->array[queue->front]; if (hasOnlyOneItem(queue)) queue->front = queue->rear = -1; else ++queue->front; return temp; } struct node* getFront(struct Queue* queue) { return queue->array[queue->front]; } // A utility function to check if a tree node has both left and right children int hasBothChild(struct node* temp) { return temp && temp->left && temp->right; } // Function to insert a new node in complete binary tree void insert(struct node **root, int data, struct Queue* queue) { // Create a new node for given data struct node *temp = newNode(data); // If the tree is empty, initialize the root with new node. if (!*root) *root = temp; else { // get the front node of the queue. struct node* front = getFront(queue); // If the left child of this front node doesn’t exist, set the // left child as the new node if (!front->left) front->left = temp; // If the right child of this front node doesn’t exist, set the // right child as the new node else if (!front->right) front->right = temp; // If the front node has both the left child and right child, // Dequeue() it. if (hasBothChild(front)) Dequeue(queue); } // Enqueue() the new node for later insertions Enqueue(temp, queue); } // Standard level order traversal to test above function void levelOrder(struct node* root) { struct Queue* queue = createQueue(SIZE); Enqueue(root, queue); while (!isEmpty(queue)) { struct node* temp = Dequeue(queue); printf("%d ", temp->data); if (temp->left) Enqueue(temp->left, queue); if (temp->right) Enqueue(temp->right, queue); } } // Driver program to test above functions int main() { struct node* root = NULL; struct Queue* queue = createQueue(SIZE); int i; for(i = 1; i <= 12; ++i) insert(&root, i, queue); levelOrder(root); return 0; }
Output:
1 2 3 4 5 6 7 8 9 10 11 12
相关文章推荐
- 用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(二)
- 满二叉树(Full Binary Tree)&&完全二叉树(Complete Binary Tree)
- 用C++实现的完全二叉树,实现前序遍历,中序遍历,后序遍历。
- 【LeetCode-面试算法经典-Java实现】【206-Reverse Linked List(反转一个单链表)】
- LeetCode 之 Linked List Cycle I II — C++ 实现
- ArrayList,LinkedList,Vector的实现及区别
- LinkedList集合类实现队列
- [Java]ArrayList与LinkedList的模拟实现
- 使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender实现自动完成功能(上) 【转自http://dflying.cnblogs.com】
- 看数据结构写代码(31)树的二叉链表的实现
- 自己动手系列——实现一个简单的LinkedLis
- Java中使用LinkedList实现栈与队列
- LinkedList双链表简单分析,及单链表的实现代码
- jQuery.Autocomplete实现自动完成功能(详解)
- 用LinkedList实现栈
- ArrayList、LinkedList、HashMap底层实现
- 自己动手系列——实现一个简单的LinkedLis
- android AutoCompleteTextView 实现输入提示,类似百度支持输入拼音提示中文(gray)
- 【LeetCode】222. Count Complete Tree Nodes-完全二叉树的结点个数
- AutoCompleteTextView控件的应用-实现记录提示