I am trying to perform binary tree insertion and in order traversal in System Verilog using OOPs concepts. I am getting error that the object is being used before creating it. Please take a look at the code and help me if someone find any mistakes
class node;
byte data;
node left;
node right;
function new();
this.data = data;
this.left = null;
this.right = null;
endfunction
endclass
class bin_search extends node;
node newNode;
node nd,root,current,parent;
byte in_data;
function new();
super.new();
this.in_data = in_data;
endfunction
function automatic insert(in_data);
newNode.data = nd.data;
if(root.data == null) begin
root = newNode;
return;
end
else begin
current = root;
parent = null;
end
forever begin
parent = current;
if(in_data < current.data) begin
current = current.left;
if(current.left == null) begin
parent.left = newNode;
return;
end
end
else begin
current = current.right;
if(current.right == null) begin
parent.right = newNode;
return;
end
end
end
endfunction
function automatic inorder_traverse(node node_tr);
//using nodes here
endfunction
endclass
module binary;
node NODE;
bin_search bs;
byte ins;
initial begin
NODE = new;
bs = new;
bs.insert(50);
$display("Binary search tree after insertion:");
bs.inorder_traverse(bs.root);
end
endmodule
Error message:
Error-[NOA] Null object access
binary.sv, 28
The object at dereference depth 1 is being used before it was
constructed/allocated.
Please make sure that the object is allocated before using it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…