Static Implementation of Binary Tree

Posted by kathas on Wed, 14 Aug 2019 09:23:50 +0200

The static implementation of the binary tree can satisfy the requirement of not using pointers at all, but simply using arrays to complete all operations of the binary tree.

When defining binary tree, the structure of binary linked list is adopted, as follows:

struct node{
	typename data;//Data Domain
	node *lchild;//Pointer to the root node of the left subtree
	node *rchild;//Pointer to the root node of the right subtree 
};

In this definition, in order to control the number of newly generated nodes in real time, pointers are used in the left and right pointer fields of the structure node. A static binary list can be implemented without needing pointers.

 

Definition of static binary linked list:

 

The left and right pointer fields of the nodes are replaced by int type, which is used to represent the subscripts of the root nodes of the left and right subtrees in the array. For this reason, we need to build an array of node type whose size is the upper number of nodes. All dynamically generated nodes directly use the nodes in the array, and all operations on pointers are changed to access the subscripts of the array. Thus, the definition of node is changed as follows:

struct node{
	typedefname data;//Data Domain
	int lchild;//Pointer field pointing to left subtree
	int rchild;//Pointer field pointing to right subtree 
}Node[maxn];//A node array, maxn being the upper number of nodes 

Under such a definition, the dynamic generation of nodes can be transformed into the following static designation:

int newNode(int v){
	//Assign a node in a Node array to a new node with index as its subscript
	Node[index].data=v;
	Node[index].lchild=-1;//Represents null in - 1 or maxn, because the array range is 0-maxn-1 
	Node[index].rchild=-1;
	return index++; 
}

The code for finding, inserting and establishing binary trees is given below, which is different from the code for using pointers.

//Find, root is the subscript of the root node in the array
void search(int root,int x,int newdata){
	if(root==-1){
		//Replace null with - 1
		return;//Recursive boundaries 
	}
	if(Node[root]==x){
		//Find the node whose data domain is x and change the new data domain
		Node[root]=newdata; 
	}
	search(Node[root].lchild,x,newdata);//Search for x (recursive) in the left subtree
	search(Node[root].rchild,x,newdata);
} 
//Insert, root is the subscript of the root node in the array, remember to add a reference
void insert(int &root,int x){
	if(root==-1){
		//Finding failures, boundaries
		root=newNode(x);//Give root a new node
		return; 
	}
	if(Properties of Binary Trees x The left subtree should be inserted){
		insert(Node[root].lchild,x);
	}else{
		insert(Node[root].rchild,x);
	}
} 

//Establishment of binary tree, function returns the subscript of root node
int Create(int data[],int n){
	int root=-1;//Establishing Root Node
	for(int i=0;i<n;i++){
		insert(root,data[i]);
	} 
	return root;//Returns the subscript of the root node of the binary tree in the array 
} 

The corresponding transformation should also be made for the binary tree's first traversal, middle traversal, post-order traversal and hierarchical traversal.

//Preorder traversal
void preorder(int root){
	if(root==-1){
		return;//Reaching empty trees, recursive boundaries 
	}
	//Access the root node, for example, to output its data
	printf("%d\n",Node[root].data);
	//Accessing the left subtree
	preorder(Node[root].lchild);
	//Accessing the right subtree
	preorder(Node[root].rchild); 
} 

//Intermediate traversal
void inorder(int root){
	if(root==-1){
		return;//Reaching empty trees, recursive boundaries 
	}
	//Accessing the left subtree
	preorder(Node[root].lchild);
	//Access the root node, for example, to output its data
	printf("%d\n",Node[root].data);
	//Accessing the right subtree
	preorder(Node[root].rchild); 
} 

//Postorder traversal
void postorder(int root){
	if(root==-1){
		return;//Reaching empty trees, recursive boundaries 
	}
	//Accessing the left subtree
	preorder(Node[root].lchild);
	//Accessing the right subtree
	preorder(Node[root].rchild); 
	//Access the root node, for example, to output its data
	printf("%d\n",Node[root].data);
} 

//level traversal
void LayerOrder(int root){
	queue<int>q;//The subscripts of the nodes are stored in the queue here.
	q.push(root);//Queuing the root node address
	while(!q.empty()){
		int now=q.front();//Remove the team leader element
		q.pop();
		printf("%d",Node[now].data);
		if(Node[now].lchild!=-1) q.push(Node[now].lchild);
		if(Node[now].rchild!=-1) q.push(Node[now].rchild);//Right subtree is not empty 
		
	} 
} 


Binary tree implements complete static transformation